Array.prototype.map can be used to map one array to another.
var names = employees.map(function(i) {
return i.name;
});
names is now an array containing the name-properties of the objects.
Array.prototype.map can be used to map one array to another.
var names = employees.map(function(i) {
return i.name;
});
names is now an array containing the name-properties of the objects.
Array.prototype.map maps one array to another:
var names = employees.map(function (val) {
return val.name;
});
// ['George', 'Edward', 'Christine', 'Sarah']
Get single object from array using JavaScript functions - Stack Overflow
Find a single value in array of objects in JavaScript - Stack Overflow
javascript - A simpler way of getting single key and single value in array of objects? - Stack Overflow
How to get only one value in Javascript array of objects using for of and for in statements? - Stack Overflow
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
If your id's are unique you can use find()
var frequencies = [{"id":124,"name":"qqq"},{"id":589,"name":"www"},{"id":45,"name":"eee"},{"id":567,"name":"rrr"}];
var result = frequencies.find(function(e) {
return e.id == 45;
});
console.log(result)
You need filter() not map()
var t = frequencies.filter(function (obj) {
return obj.id==45;
})[0];
Simple add ?.age to your found variable.
var obj = [
{ name: 'Max', age: 23 },
{ name: 'John', age: 20 },
{ name: 'Caley', age: 18 }
];
var found = obj.find(e => e.name === 'John')?.age; // Using ?. incase value isnt found.
console.log(found);
You also mention you need it as a string, you can do achieve by wrapping the found var in the String function like so...
var obj = [
{ name: 'Max', age: 23 },
{ name: 'John', age: 20 },
{ name: 'Caley', age: 18 }
];
var found = String(obj.find(e => e.name === 'John')?.age);
console.log(found);
.find function return you an object so you can just do something like this
var age = obj.find(e => e.name === "John").age
You don't need multiple for loop for this. You can do this using one forEach() loop and template literal like:
var customerData = [{ customerName: "Jay", Purchased: "phone", Price: "€200" },
{ customerName: "Leo", Purchased: "car", Price: "€2000" },
{ customerName: "Luk", Purchased: "Xbox", Price: "€400" },
];
function getValue() {
customerData.forEach(x => {
console.log(`Dear ${x.customerName} thank you for purchase of a ${x.Purchased} for the price of ${x.Price}`)
})
}
getValue();
var customerData = [{ customerName: "Jay", Purchased: "phone", Price: "€200" },
{ customerName: "Leo", Purchased: "car", Price: "€2000" },
{ customerName: "Luk", Purchased: "Xbox", Price: "€400" },
]
function getValue(){
for(let key of customerData){
for(let value in key){
console.log(key[value]) //I get all values
break;
//It Work
}
}
}
getValue();
I know this question is old, but no one has mentioned a native solution yet. If you're not trying to support archaic browsers (which you shouldn't be at this point), you can use array.filter:
var arr = [];
arr.push({name:"k1", value:"abc"});
arr.push({name:"k2", value:"hi"});
arr.push({name:"k3", value:"oa"});
var found = arr.filter(function(item) { return item.name === 'k1'; });
console.log('found', found[0]);
Check the console.
Run code snippetEdit code snippet Hide Results Copy to answer Expand
You can see a list of supported browsers here.
In the future with ES6, you'll be able to use array.find.
Arrays are normally accessed via numeric indexes, so in your example arr[0] == {name:"k1", value:"abc"}. If you know that the name property of each object will be unique you can store them in an object instead of an array, as follows:
var obj = {};
obj["k1"] = "abc";
obj["k2"] = "hi";
obj["k3"] = "oa";
alert(obj["k2"]); // displays "hi"
If you actually want an array of objects like in your post you can loop through the array and return when you find an element with an object having the property you want:
function findElement(arr, propName, propValue) {
for (var i=0; i < arr.length; i++)
if (arr[i][propName] == propValue)
return arr[i];
// will return undefined if not found; you could return a default instead
}
// Using the array from the question
var x = findElement(arr, "name", "k2"); // x is {"name":"k2", "value":"hi"}
alert(x["value"]); // displays "hi"
var y = findElement(arr, "name", "k9"); // y is undefined
alert(y["value"]); // error because y is undefined
alert(findElement(arr, "name", "k2")["value"]); // displays "hi";
alert(findElement(arr, "name", "zzz")["value"]); // gives an error because the function returned undefined which won't have a "value" property