How to loop over all elements of an array inside an object in array of objects?

javascript loop through array of objects es6
javascript iterate object key, value
loop through two arrays javascript
javascript iterate array
js iterate over an array of objects
loop through object javascript
javascript foreach object
for each loop for array of objects

I have an array of objects

myArrayOfObjects = [
  0: {
    title: 'title 1',
    ids: ['90b7f1804642', 'ffa339e9aed7']
  },
  1: {
    title: 'test 2',
    ids: ['99472a9cb432', 'ec3d70ea6ef8']
  }
];

I want to check each of the values in ids array if they match an id I have outside. This is what I came up with:

myArrayOfObjects.forEach((item, index) => {
    if(item.ids[index] === this.myId) {
      console.log('SUCCESS');
    }
})

But it does not go through all of the values in ids array. What is the appropriate way to solve this?

The index you're using there is the index of the current item that the forEach is on, not an index in the ids array. You could do another forEach on that ids array, or just a .includes looks nicer, I think:

myArrayOfObjects.forEach((item, index) => {
    if(item.ids.includes(this.myId)) {
      console.log('SUCCESS');
    }
})

JavaScript - The Arrays Object, How do I iterate an array of objects in Node JS? As you can see the for loop statement uses three expressions: the initialization, the condition, and the final expression. The final expression is executed at the end of each loop execution. It is commonly used to increment the index. The following is an example of using JavaScript to loop through an array.

You need to loop over the array inside the array

myArrayOfObjects.forEach((item) => {
    item.ids.forEach((id) => {
        if(id === this.myId) {
          console.log('SUCCESS');
        }
    });
})

as a collection of variables of the same type. In front of a loop, a label allows you to break or continue that loop even from a loop nested inside of it. In front of a block, you can break out of that block. In both cases the name of the label becomes an argument of break or continue .

Please try:

myArrayOfObjects.forEach((item, index) => {

  if(item.ids.includes(this.myId)) {
  //or if(item.ids.indexOf(this.myId) > -1) {
    console.log('SUCCESS');
  }
})

Some use cases of looping through an array in the functional programming way a new array with the results of calling a provided function on every element in items) { console.log(item); // Will display contents of the object inside the array }​. For-of loop lets you iterate array elements. The syntax is for (const element of array) (we can replace const with var or let, but it's better to use const if we don't intend to modify element). Destructuring assignment lets you extract values from an array or an object and assign them to variables.

Non-standard (Firefox only), iterates over the values of an object. Don't use it. Array methods for iteration. Iterate. Iterate over the elements in an  Step 4 — Reformatting Array Objects.map() can be used to iterate through objects in an array and, in a similar fashion to traditional arrays, modify the content of each individual object and return a new array. This modification is done based on what is returned in the callback function.

All languages have some implementation of the for loop and forEach and JavaScript is As the language has matured so have our options to loop over arrays and objects. JavaScript for loops iterate over each item in an array. Modern JavaScript has added a forEach method to the native array object. for-in [ MDN, spec] — A loop structure that loops through the names of an object's enumerable properties, including inherited ones, whose names are strings. Object.keys [ MDN, spec] — A function providing an array of the names of an object's own, enumerable properties whose names are strings.

forEach() in JavaScript calls the provided function on each array item with best practices (correct handling of this , how to iterate array-like objects, The first argument of forEach() is the callback function called for every item in the array. this inside iterate() equals to window , which is the global object in  One is an array of simple strings (i.e. A1), while the other is an array of objects (i.e. A2). I need to pluck only those objects from A2 , that have relevant keys present in A1 . Here is my implementation using a double for loop.

Comments
  • let myArrayOfObjects = [0: 'value'] is not how you create arrays (you're also missing some quotes)
  • 1. the "ArrayObject" haven't a correct syntax.
  • Problem is OP's myArrayOfObjects is not a valid array
  • Yeah, I see what you're saying, but I'm assuming he copy pasted it from a browser console that shows the indexes as though they were keys