help please, i have this array of object and must to create a new array that contains all the objects from the original array of object that have a numerical value greater than 15. how can i do this ? which method a must to use ?
const names = [
{ name: 'Anna' },
{ num: 27 },
{ name: 'Valeria', age: 20},
{ secondname: 'Wilson' },
{ age: 12, name: 'Max' },
{ weight:'50kg', height: '172cm', name: 'Nick' }
]
i try to use filter,but it should see any field that has a number value not just age or num
const number = arr.filter(a => a.num || a.age > 15);
console.log(number);
CodePudding user response:
If you like to get only numbers, you chould check the values of the object with Array#some
and typeof
operator.
const
names = [{ name: 'Anna' }, { num: 27 }, { name: 'Valeria', age: 20 }, { secondname: 'Wilson' }, { age: 12, name: 'Max' }, { weight: '50kg', height: '172cm', name: 'Nick' }],
result = names.filter(o => Object
.values(o)
.some(v => typeof v === 'number' && v > 15)
);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }