2

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" ?

1
  • 1
    Please put some effort into your question! Include a minimal reproducible example of what you have tried to solve the problem! Commented Aug 2, 2018 at 10:37

5 Answers 5

8

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);

Sign up to request clarification or add additional context in comments.

10 Comments

He could use find instead of filter, which seems more suited (same code, different output)
Note: Not IE11 compatible
find returns first item only, maybe there are more than one item
@SurenSrapyan that's an assumption, in this case there is only one item
@SurenSrapyan because If I were to assume, I would assume this is only an example and that he actually wants to return the name property. Do you see how problematic it is ?
|
3

This snippet might be what you are looking for

arrayobjects.filter( ele => ele.description.includes("two")).map(ele => ele.id)

output :[1]

Comments

1

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)
}
})

Comments

1

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.

Comments

0

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, [])

);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.