简体   繁体   中英

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM