Lian Nivin
Lian Nivin

Reputation: 35

Filter object for a nested array (Javascript)

Based on an object like this:

var p = [
           {x: [
                 {x1: "John"}, 
               ]
           },
           {x: [
                 {x1: "Louis"},
               ]
           }
        ];

I need to filter p objects when x1 is different from any of those values:

var p = [
           {x: [
                 {x1: "Louis"}, 
               ]
           },
        ];

Thanks all of you for your help.

Upvotes: 0

Views: 71

Answers (2)

Siva Kondapi Venkata
Siva Kondapi Venkata

Reputation: 11001

Use filter method and destructuring. Check for condition in filter method.

var p = [{ x: [{ x1: "John" }] }, { x: [{ x1: "Louis" }] }];

const filter = (arr, item) => arr.filter(({ x: [{ x1 }] }) => x1 !== item);

console.log(filter(p, "John"));
console.log(filter(p, "Louis"));

Upvotes: 1

Adrian Brand
Adrian Brand

Reputation: 21628

It is exactly the same as your question with the numbers.

var p = [
           {x: [
                 {x1: 'John'}, 
               ]
           },
           {x: [
                 {x1: 'Louis'},
               ]
           }
        ];

const results = p.filter(val => !val.x.some(v => v.x1 === 'John'));

console.log(results);

Upvotes: 2

Related Questions