How do I filter an array in an array?

  • 0
    You need to filter the array from null, which is in another array.
    array: [
            {
                "id": "1",
                "name": "test",
                "array2": [
                    null,
                    "666",
                    "99999"
                ]
            },
            {
                "id": "1",
                "name": "test",
                "array2": [
                   null,
                    null,
                    "2222",
                    "5555"
                ]
            }
    ]
    JavaScript Gabriel McCoy, Sep 7, 2020

  • 1 Answers
  • 0
    arr.map(n => ({
    ...n,
    array2: n.array2.filter(n => n !== null),
    }))


    or, if you don't want to create a new array:



    arr.forEach(n => n.array2.reduceRight((_, n, i, a) => n === null && a.splice(i, 1), 0))
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!