Here is a shorter way of achieving it:
let result = objArray.map(a => a.foo);
OR
let result = objArray.map(({ foo }) => foo)
You can also check Array.prototype.map().
Here is a shorter way of achieving it:
let result = objArray.map(a => a.foo);
OR
let result = objArray.map(({ foo }) => foo)
You can also check Array.prototype.map().
Yes, but it relies on an ES5 feature of JavaScript. This means it will not work in IE8 or older.
var result = objArray.map(function(a) {return a.foo;});
On ES6 compatible JS interpreters you can use an arrow function for brevity:
var result = objArray.map(a => a.foo);
Array.prototype.map documentation
javascript - Get array of property values from array of objects with jquery - Stack Overflow
javascript - Get values from an array of objects - Stack Overflow
javascript - How to obtain the object values from an array with one object and create a new array with just the object values - Stack Overflow
Get all the values from an array of objects
Q8. How can I get object property values as an array in JavaScript?
Q7. How does JavaScript get array of specific property values from an object collection?
Q9. How to extract values in JavaScript efficiently from an object or array?
You can use the $.map function from jQuery :
var value = $.map(example, function () {
return this.name;
});
This will return an array of the items.
You can iterate through the object keys and save the name in the array using push:
example = [{
name: "someone1",
city: "somewhere1",
state: "someplace1"
}, {
name: "someone2",
city: "somewhere2",
state: "someplace2"
}];
var arrNames = [];
//iterate through object keys
Object.keys(example).forEach(function(key) {
//get the value of name
var val = example[key]["name"];
//push the name string in the array
arrNames.push(val);
});
console.log(arrNames);//prints ["someone1", "someone2"]
After @Felix suggestion(with which I agree) there is no need to use Object.keys:
example = [{
name: "someone1",
city: "somewhere1",
state: "someplace1"
}, {
name: "someone2",
city: "somewhere2",
state: "someplace2"
}];
var arrNames = [];
//iterate through object keys
example.forEach(function(item) {
//get the value of name
var val = item.name
//push the name string in the array
arrNames.push(val);
});
console.log(arrNames); //prints ["someone1", "someone2"]
References
Object.keys()
Array.prototype.forEach()
Array.prototype.push()
Simply use find
([{name: 'joe', age: 'nine'}, {name: 'tom', age: 'ten'}].find( (s)=> s.name=="tom" )||{}).age //10
([{name: 'joe', age: 'nine'}, {name: 'tom', age: 'ten'}].find( (s)=> s.name=="tom2" )||{}).age //undefined
Demo
var names = [{
name: 'joe',
age: 'nine'
}, {
name: 'tom',
age: 'ten'
}];
function getAge(names, name) {
return (names.find((s) => s.name == name) || {}).age;
}
console.log(getAge(names, "tom"))
console.log(getAge(names, "tom2"))
You can use array.prototype.find:
var names = [{name: 'joe', age: 'nine'}, {name: 'tom', age: 'ten'}];
var someFun = (nm) => {
var found = names.find(el => el.name === nm);
return found && found.age || null;
}
console.log(someFun('tom'));
If you know for sure that it will only have one object then all you need to do is:
Object.values(myVar[0])
see docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values
Note: If you need to support older browsers like internet explorer you will have to use Object.keys paired with map
Try This, You get the result you want.
let newArr = [];
for (var key in myVar[0]) {
if (myVar[0].hasOwnProperty(key)) {
newArr.push(myVar[0][key]);
}
}
console.log('New Array: ',newArr);
Output: [ 'A', 'B', 'C', 'D' ]