0

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.

2 Answers 2

2

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

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

Comments

1

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

3 Comments

Excellent. This is a very nice solution. Thanks a lot.
This will only work if x1 only has a single item in the array.
@AdrianBrand, You mean to x only has a single item right? Yes. you are right. It will work only for given data format. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.