How can I get to the value from 'age'?
I already have 'record.data.items', but I can't get any further. I want the value of age. Can anyone help me?
`Ext.util.MixedCollection
...
items: Array[3]
0: c
data: Object
age: "4"
id: "1"
name: "sam"
1: c
2: c
length: 3
__proto__: Array[0]
keys: Array[3]
length: 3
...`
Check out this link:
http://docs.sencha.com/ext-js/4-0/#!/api/Ext.util.MixedCollection
You can get at the items in the collection with the getAt() method.
To iterate through the list, loop 0 to getCount()
for (var i = 0 ; i < record.getCount() ; i ++){
console.log(record.getAt(i).data.age);
}
Related
What is the easiest way to search an ObjectArray in Kotlin by a property belonging to that Object?
For example I have a data class
data class Cat(
var name: String,
var age: Int,
var type: String,
)
and I have a Array<Cat> and would like to find the first occurrence of a cat with age == 4.
You can use find function to find the element with age == 4:
val cats = arrayOf(
Cat("Name1", 2, "Type1"),
Cat("Name2", 4, "Type2")
)
val cat: Cat? = cats.find { it.age == 4 }
find function returns the first element matching the given predicate, or null if no such element was found.
I have an array of type "User" and I'd like to delete all users that are 10 years old. My code :
struct User: Identifiable {
var id = UUID()
var name: String
var age: String
}
var array: User = [
User[name: "AZE", age: "10"]
User[name: "QSD", age: "37"]
]
What is the function for deleting an object from an element of that object? I hope you understood my problem and thank you for your answer.
You can use filter to keep only the elements of the array whose age property doesn't equal 10.
let filtered = array.filter { $0.age != "10" }
Unrelated to your question, but why is age a String? It should be an Int instead, since it represents a numeric value. Also, you should always make properties immutable (let) by default and only make them mutable (var) if they really need to be mutable.
i struggle my had now sine several days with a way to do conditional filter of an object array with another object array.
lack on capabilities to properly abstract here... maybe you ahve some ideas.
I have a given Object Array A but more complex
var ArrA = [{
number: 1,
name: "A"
}, {
number: 2,
name: "C"
}]
And i want to filer for all results matiching id of Object Array B
var ArrB = [{
id: 1,
categorie: "wine"
}, {
id: 3,
categorie: "beer"
}, {
id: 10,
categorie: "juice"
}]
And in the best case moving this directly also together with an if condition.... but i was not able to handle it ... here is where i am now ... which is not working....
let newArray = ArrA.filter{$0.number == ArrB.... }.
if (newArray.count != 0){
// Do something
}
is there a lean way to compare one attribute of every object in an array with one attribute of another every object in an array ?
Lets break this down: You need all arrA objects that matches arrB ids, so first thing first you need to map your arrB to a list of id (because you dont need the other infos)
let arrBid = Set(arrB.map({ $0.id })) // [1, 3, 10]
As commented below, casting it to Set will give you better results for huge arrays but is not mandatory though
Then you just need to filter your first arrA by only keeping object that id is contained into arrBid :
let arrAFilter = arrA.filter({ arrBid.contains($0.number) })
[(number: 1, name: "A")]
and voila
This is my json object adminDocs in angularJS.
[] 0: {rowno: 1, docTypeDesc: "Passport photocopy", …} 1: {rowno: 2, docTypeDesc: "Birth Certificate", …} 2: {rowno: 3, docTypeDesc: "Admission Doc Literature", …} 3: {rowno: 4, docTypeDesc: "Transcript", …} length: 4 __proto__: Array(0)
Checking $scope.adminDocs.length is returning zero.
Try something like this
var count = Object.keys(adminDocs).length;
Try to parse the object and then use:
var obj = JSON.parse(*your JSON*);
var length = Object.keys(obj).length;
My issue is not with $scope.adminDocs.length returning zero. The object's value is not returned untill it is completely loaded. So, it is returning zero.
This question already has answers here:
How to get the first element of an array?
(35 answers)
Closed 4 years ago.
I am trying to get the first object from an array of objects. Fetched data in componentDidMount and return:
var array1 = [
{
Id: 1,
Categories: 200
},
{
Id: 2,
Categories: 100
}
]
(real data is much more complicated but in the same format)
What I want is this:
array2 = [
Id: 1,
Categories: 200
]
I was using
var iterator1 = array1.entries();
let array2 = iterator1.next().value
and when I console.log(array2) it shows the array data (all key and value is in index 1 of array), then when I console.log(array2[1].Id)
it shows undefined right inside the render method in class component. I use the same code in other IDE but not using react the code work. Could someone explain to me please? Appreciate for any help.
Edit:
At the end I found that I asked the wrong question. I failed to get the data because I declared the data in my reducer to object instead of array. I mark the answer by larz since you just need to put index 0 to get the first object from the array of objects.
Grab the first item in your array using [0], which returns an object in your case. Then you can call .Id on it to get the id.
var array1 = [
{
Id: 1,
Categories: 200
},
{
Id: 2,
Categories: 100
}
];
var first = array1[0];
console.log(first, first.Id);