0

I have an array of nested object, I am trying to remove undefined value deep down the array

let partialMatch = [
  {
    lines: [{ id: 1}, {id: 2}]
  },
  {
    lines: [{ id: 10}, {id: 25}, undefined ]
  }
]

So far I have

   partialMatch.forEach((x) => {
      x.lines.forEach((y, index) => {
        if (!y) {
          x.lines.splice(0, index);
        }
      });
   });
teestunna
  • 197
  • 4
  • 17

1 Answers1

1

You can filter and then assign the result back to x.lines:

let partialMatch = [
  {
    lines: [{ id: 1}, {id: 2}]
  },
  {
    lines: [{ id: 10}, {id: 25}, undefined ]
  }
];

partialMatch.forEach((x) => {
    x.lines = x.lines.filter((v) => v !== undefined);
});

console.log(partialMatch);
kelsny
  • 23,009
  • 3
  • 19
  • 48