Reputation: 33
How to find id in an array that is inside of an array of objects
Example:
let arrayobjects = [{
id: 1,
name: "oinoin",
description: ["one", "two", "three"]
}, {
id: 2,
name: "zatata",
description: ["four", "five", "six"]
}];
How can I find the id of the word "two" ?
Upvotes: 2
Views: 481
Reputation: 1087
For examples sake, here is a version with old javascript.
var arrayobjects = [
{
id: 1,
name: "oinoin",
description: ["one", "two", "three"]
}, {
id: 2,
name: "zatata",
description: ["four", "five", "six"]
}
];
function findId (desc, arr) {
var idx, arrObjLen = arr.length, results = [];
// Iterate through the first array
for (idx = 0; idx < arrObjLen; idx ++) {
// If the value exists in the 'description' array
if (arr[idx].description.indexOf(desc) > -1) {
// Add it to the results array
results.push(arr[idx].id)
}
}
return results;
}
console.log('Results:', findId('five', arrayobjects));
You could also use Array#reduce
if you want to minimise the amount of code you're using:
const arrayobjects = [
{
id: 1,
name: "oinoin",
description: ["one", "two", "three"]
}, {
id: 2,
name: "zatata",
description: ["four", "five", "six"]
}
];
console.log(
arrayobjects.reduce((col, { id, description }) =>
// If description contains the desired value
description.includes('two')
// Add the id to the collector array
? [ ...col, id ] : col, [])
);
Upvotes: 0
Reputation: 68635
If you need more than one item, you can filter the array via Array#filter, check if property description
for each item contains word two
via Array#includes (ES7), then map the result to get only id
of those items using Array#map.
let arrayobjects = [
{ id: 1, name: "oinoin", description: ["one", "two", "three"] },
{ id: 2, name: "zatata", description: ["four", "five", "six"] }
];
const ids = arrayobjects.filter(item => item.description.includes('two'))
.map(item => item.id);
console.log(ids);
If you have only one item, you can just use Array#find and do same checking
let arrayobjects = [
{ id: 1, name: "oinoin", description: ["one", "two", "three"] },
{ id: 2, name: "zatata", description: ["four", "five", "six"] }
];
const item = arrayobjects.find(item => item.description.includes('two'));
console.log(item.id);
Upvotes: 8
Reputation: 113
This snippet might be what you are looking for
arrayobjects.filter( ele => ele.description.includes("two")).map(ele => ele.id)
output :[1]
Upvotes: 3
Reputation: 6587
You can try with indexOf too
arrayobjects.filter(f=>f.description.indexOf('two')>-1)[0].id
let arrayobjects = [
{
id: 1,
name: "oinoin",
description: ["one", "two", "three"]
}, {
id: 2,
name: "zatata",
description: ["four", "five", "six"]
}
];
console.log(arrayobjects.filter(f=>f.description.indexOf('two')>-1)[0].id);
Note
.filter
returns an array. So if multiple items are found with two
in description then you do not use index [0]
. This is just an example of one item in array.
Upvotes: 1
Reputation: 6130
There may be more efficient way, but i some how find a solution. iterate through the array and match the array item.
var arrayobjects = [{ id: 1, name: "oinoin", description: ["one", "two", "three"]}, { id: 2, name: "zatata", description: ["four", "five", "six"]}];
arrayobjects.forEach(item => {
if(item.description.indexOf('two') != -1 ){
console.log(item.id)
}
})
Upvotes: 1