undefinedfix
Sign in

JS array nested array

Stelian edited in Mon, 24 Oct 2022

var a = [{ a: 1, b: false }, { a: 2, b: false }, { a: 3, b: false }];

  var b = [1, 2, 3];

  a.forEach((item, index) => {
    b.forEach(el => {
      if (el == item.a) {
        el.b = true;
      }
    });
  });
  

Then the array B is changed manually when B = [1,2]; the {A: 3, B: true} of a needs to change back to {A: 3, B: false}

Big boys! How to write this

1 Replies
aleonjhon
commented on Mon, 24 Oct 2022
a.forEach(item => item.b = b.includes(item.a));