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

1 Answer 1

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

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.