I have some compllex component which contains array
this.SomeComponent.subArray
SomeComponent:model1;
model1 definition:
someString1:string;
someString2:string;
someString3:string;
subArray[]
model2 definition:
someString22:string;
someString23:string;
someString24:string;
Now if I console.log(JSON.stringify(this.SomeComponent.subArray));
I get :
SomeComponent[{"someString22":"one","someString23":"yellow","someString24":"water",},
{"someString22":"two","someString23":"red","someString24":"ground",}
{"someString22":"three","someString23":"green","someString24":"air",}]
Now my task is to delete subArray part which has someString23==red.
So When I console log next time I must get only this
SomeComponent[
{"someString22":"two","someString23":"red","someString24":"ground",}
{"someString22":"three","someString23":"green","someString24":"air",}]
How can I do that?
Use filter method like below :-
this.subArray = subArray.filter((item) => item["someString23"]!==red);
Related
I have an array of objects called modelArray these objects have a property which is called timestamp which is of type FIRTimestamp.
Knowing that we have another timestamp which we are gonna call comparingTimestamp
I'm trying to figure out how I can find the first index in the array where the property timestamp is > of the comparingTimestamp.
If you are not practical with firebase, the object Timestamp has a numerical property called seconds which we can use for making the comparison.
Until now I tried using the function where but without any good result.
You can use collection's firstIndex(where:) method:
struct Item {
let timestamp: FIRTimestamp
}
let modelArray = [item1, item2, item3, item4]
let firstIndex = modelArray.firstIndex { item in
item.timestamp.seconds > comparingTimestamp.seconds
}
print(firstIndex!)
I am trying to count the number of elements that are equal to a certain value but im struggling to get to the element that i want to compare, when i run:
{{dd($numberofnotifications)}}
I get the following:
But the value i need to compare is under 'attributes'
so how do i get to the values under attributes?
When i print out each element of the array i get the following format:
{"id":"96a40ebb-a2d1-44d8-9600-e94dd026f152","type":"App\\Notifications\\CommentCreated","notifiable_type":"App\\User","notifiable_id":1,"data":{"comment_id":9,"data":{"name":"John","date":"2020-12-30T08:37:47.000000Z","email":"John#gmail.com","post":2,"body":"test"},"message":"John commented on your post"},"read_at":null,"created_at":"2020-12-30T08:37:47.000000Z","updated_at":"2020-12-30T08:37:47.000000Z"}
It seems that the items is an array of DatabaseNotifications. So, you can get each element manually by its index:
$numberofnotifications[0]->id;
// or
$numberofnotifications[0]->type
or you can use foreach loop:
foreach ($numberofnotifications as $notification) {
$notification->id;
}
As the result is a collection, you can convert them using
$numberofnotifications->toArray();
you'll need to use foreach to iterate through the collection:
foreach($numberofnotification as $number)
{
$number->name; //name here is the "name" of the attribute you want to access
}
if you want to store them in an array then:
foreach($numberofnotification as $number)
{
$attributes[]=$number->name; //name here is the "name" of the attribute you want to access
}
Did you try this
//consider that $username is the logged in user name
$filtered_count = $numberofnotifications->where('name', $username)->count();
and if you want the record
$filtered = $numberofnotifications->where('name', $username)->all();
I am pushing an object into an array but cannot do it?
I'm doing it like this
this.passData = this.tribeForm.value;
var id = {"tribe_id": 1}
this.passData.push(id)
This is the value in the tribeForm
I also tried
var id = {tribe_id: 1}
and
this.passData.splice(0,0, id)
and
this.passData = Array.prototype.slice(id)
and
this.passData.concat(id)
but it all ends up with
TypeError: this.passData.push/splice/concat is not a function
The question is not that clear, But I understood you are manipulating form data, value of form data returns an Object, Not an array. Objects in JavaScript are represented as key-value pairs, (or attribute-value) pairs.
Example :
var object = {
name : "Jhon",
grade : 12,
gpa : 8.12
}
It is just a collection of key-value pairs, push(), concat() and other methods are supported only for Arrays not for Objects. You can achieve whatever you want simply by creating a new key/attribute and assigning the value to it.
this.passData = this.tribeForm.value
this.passData['tribe_id'] = 1
//or, Objects can also contain nested object
this.passData['someKey'] = {'tribe_id' : 1}
You can create an empty array and push objects to it
Example :
var exampleArray = []
exampleArray.push({'tribe_id' : 1})
Now, it works because exampleArray is an Array not JS object.
Thanks for A2A
First, you need to understand the error:
TypeError: this.passData.push/splice/concat is not a function
Push/splice/concat is functions for Array and because of that the console is yelling at you that the passData is not an Array.
Make sure your passData is an Array and you will able to do so.
I have a state "Bill" and I want to delete one of item in "Product" with "ProductID". So how can I do it?
Bill = {
"Bill_ID" : "a00231",
"Products" : [
{
"ProductID" : "P0203",
"ProductName" : "ABCD"
},
{
"ProductID" : "P023243",
"ProductName" : "ZYZ"
}
]
}
How can I update the state after delete product item?
I think you could use thepop() method. You can pop the last element from your products array and look at the popped element. Something like:
var poppedElement = Bill.Products.pop();
console.log(Bill) //Bill withoug last Products element
console.log(poppedElement) //just the last element of Products
Not sure if this will work completely off the top of my head, but hopefully will give you a starting place at least :)
Check out this link for more info:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop
Let me know how it goes!
You can use filter on bill.products array, like following (note it must be != not ==)
bill.products.filter(x => x != yourDesiredToBeDeletedId)
Treat this.state as if it were immutable
Since your state contains an array within an object, I suggest to use lodash's cloneDeep.
let newBill = _.cloneDeep(this.bill);
newBill.products = newBill.products.filter(x => x != yourDesiredToBeDeletedId);
this.setState(bill: newBill);
I have an array of subscription instances #subscription_valids and an array of subscribed player that look like that :
array_subscribed_players = [{"name0" => "link1"}, {"name1"=>"link2"}, {"name2"=>"link3"}....]
What I need to do is : for each subscription in #subscription_valids :
#subscription_valids.each do |subscription|
I need to check if subscription.user.full_name or if subscription.user.full_name_inversed matches a key in one of the hashes of array_subscribed_player (in the exemple "name0", "name1" or "name2").
If it matches then I should store the relevant subscription as key in a hash in a new array and extract the relevant link as value of this hash. My final outpout should be an array that looks like this :
[{subscription1 => "link1"}, {subscription2 => "link2}, ...]
else if the subscription.user.full_name doesnt match i'll just store the subscription in a failure array.
How can I achieve this result ?
See http://ruby-doc.org/core-2.2.3/Hash.html
A user-defined class may be used as a hash key if the hash and eql?
methods are overridden to provide meaningful behavior. By default,
separate instances refer to separate hash keys.
so I think you should override your .eql? method in #subscription_valids to something meaningful (like a unique string)
I can't think for a Array method so you can go like:
demo
results = []
failures = []
#subscription_valids.each do |subscription|
array_subscribed_players.each do |player|
if player.keys.first == subscription.user.full_name || player.keys.first == subscription.user.full_name_inversed
results << { subscription => player[player.keys.first] }
else
failures << subscription
end
end
end
You can try the following:
valid = array_subscribed_players.select{|x| #subscription_valids.map(&:name).include?(x.keys.first)}
Demo
If you need to store both valid and invalid values somewhere:
valid, invalid = array_subscribed_players.partition{|x| #subscription_valids.map(&:name).include?(x.keys.first)}