0

Based on an array like this:

 var p = [
           {x: [
                 {x1: 1}, 
                 {x1: 2},
               ]
           },
         ];

How can I get something like this:

// Result of filtering an object where key is 'x1' and value is greater than 1
p === [
        {x: [
              {x1: 2},
            ]
        },
      ];

5 Answers 5

1
p = p.map(v => {
    v.x = v.x.filter(({ x1 }) => x1 > 1)
    return v
})

or (shorter syntax)

p = p.map(v => (v.x = v.x.filter(({ x1 }) => x1 > 1), v))
Sign up to request clarification or add additional context in comments.

Comments

0

to create a new array q with the desired properties

var q = [
    {x: p[0].x.filter( (x) => x.x1 > 1 ) }
]

or if you wish to change the array p in place

p[0].x = p[0].x.filter( (x) => x.x1 > 1 )

Comments

0

This code will return an array of objects with property 'x1' greater than 1.

var p = [{x: [{x1: 1}, {x1: 2}]}];

let filtered = p.map(obj => obj.x.filter(y => y.x1 > 1));
console.log(filtered);

Comments

0

var result = p[0].x.find(x = x.x1 > 1)

1 Comment

It is a very short answer but you could replace [0] for a generic response. Thanks anyway.
0

use map and filter

var p = [
  {x: [{x1: 1}, {x1: 2}]},
];
result=p.map((o)=>({...o.x.filter(y=>y.x1>1)}))
console.log(result)

Comments

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.