I have an array object in javascript. I would to select a particular field from all the rows of the object.
I have an object like
var sample = {
[Name:"a",Age:1],
[Name:"b",Age:2],
[Name:"c",Age:3]
}
I would like to get an output of only Names as ["a","b","c"]
without looping over sample object.
How can I select one or two fields using jlinq? or any other plugin?
Thanks a lot.
Use the reduce method to return a single value from an array of values.
Use filter if you want to find all items in an array that meet a specific condition. Use find if you want to check if that at least one item meets a specific condition. Use includes if you want to check if an array contains a particular value. Use indexOf if you want to find the index of a particular item in an array.
One can use filter() function in JavaScript to filter the object array based on attributes. The filter() function will return a new array containing all the array elements that pass the given condition. If no elements pass the condition it returns an empty array.
let names = [];
this.dtoArray.forEach(arr => names.push(arr.name));
You can try this:
var sample = [{Name:"a", Age:1}, {Name:"b", Age:2}, {Name:"c", Age:3}];
var Names = sample.map(function(item){return item.Name;});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With