Home > front end >  Dealing with looping through nested arrays
Dealing with looping through nested arrays

Time:10-15

I have data array, which has nested arrays inside (level1arr, leve21arr ...)

const data = [
  {
    level1arr: [
      {
        level2arr: [{ id: 1, isValid: true }, { id: 2, isValid: true }, { id: 3, isValid: true }],
      },
      {
        level2arr: [{ id: 4, isValid: true }, { id: 5, isValid: true }, { id: 6, isValid: true }],
      },
    ],
  },
  {
    level1arr: [
      {
        level2arr: [{ id: 7, isValid: true }, { id: 8, isValid: true }, { id: 9, isValid: true }],
      },
      {
        level2arr: [{ id: 10, isValid: true }, { id: 11, isValid: true }, { id: 12, isValid: true }],
      },
    ],
  },
];

I also have another array:

const invalidIds = [2,5]

I want to find elements with apecyfic id and change isValid property to false. Is it better way than iteratinf over multiple nested arrays, like that:

data.forEach(lvl1 => {
  lvl1.level1arr.forEach(lvl2 => {
    lvl2.level2arr.forEach(element => {
      // further nesting
    });
  });
}) 

Such iterating over multiple arrays is not good for performance. What is the best way to handle such case with nested arrays?

CodePudding user response:

If it were nested arrays, you could use Array.prototype.flat(). However, you have a mix of nested objects and arrays. You will have to write a custom "flattener" for this data structure. Check this answer for details: how to convert this nested object into a flat object?

CodePudding user response:

You can use recursion until you reach the level you need. Here's one way to do it.

const data = [{
    level1arr: [{
        level2arr: [{
          id: 1,
          isValid: true
        }, {
          id: 2,
          isValid: true
        }, {
          id: 3,
          isValid: true
        }],
      },
      {
        level2arr: [{
          id: 4,
          isValid: true
        }, {
          id: 5,
          isValid: true
        }, {
          id: 6,
          isValid: true
        }],
      },
    ],
  },
  {
    level1arr: [{
        level2arr: [{
          id: 7,
          isValid: true
        }, {
          id: 8,
          isValid: true
        }, {
          id: 9,
          isValid: true
        }],
      },
      {
        level2arr: [{
          id: 10,
          isValid: true
        }, {
          id: 11,
          isValid: true
        }, {
          id: 12,
          isValid: true
        }],
      },
    ],
  },
];

const invalidIds =[2,5]

const findId = (object, key, value) => {
  if (Array.isArray(object)) {
    for (const obj of object) {
      findId(obj, key, value);
    }
  } else {
    if (object.hasOwnProperty(key) && object[key] === value) {
      object.isValid = false;
      return object
    }
    for (const k of Object.keys(object)) {
      if (typeof object[k] === "object") {
        findId(object[k], key, value);
      }
    }
  }
}

invalidIds.forEach(id => findId(data, "id", id))
console.log(data)

  • Related