admin管理员组文章数量:1430083
Suppose I have an array of object:
var students = [{name: 'Nick',achievements: 158,points: 1473}, {name: 'Nick',achievements: '175',points: '16375'},
{name: 'Ramon',achievements: '55',points: '2025'}];
I want to extract points from name Nick only in an array.
Like if (name=='Nick), O/P should be [1473,16375]
I tried:
var arrayPoints = students.map(function (el) {
if(el.name=='Nick'){
return el.points
}
});
But it gives me o/p:
console.log(arrayPoints)
[1473,16375,undefined] o/p
Suppose I have an array of object:
var students = [{name: 'Nick',achievements: 158,points: 1473}, {name: 'Nick',achievements: '175',points: '16375'},
{name: 'Ramon',achievements: '55',points: '2025'}];
I want to extract points from name Nick only in an array.
Like if (name=='Nick), O/P should be [1473,16375]
I tried:
var arrayPoints = students.map(function (el) {
if(el.name=='Nick'){
return el.points
}
});
But it gives me o/p:
console.log(arrayPoints)
[1473,16375,undefined] o/p
Share
Improve this question
asked Feb 16, 2021 at 8:57
Siva PradhanSiva Pradhan
8611 gold badge11 silver badges29 bronze badges
3 Answers
Reset to default 4A look to the methods:
Array#map
returns a (new) value for each element.Array#filter
returns exactly the element if the return value of the callback is truthy
You could take two steps, one for filtering the items and another to get the values from.
const
students = [{ name: 'Nick', achievements: 158, points: 1473 }, { name: 'Nick', achievements: '175', points: '16375' }, { name: 'Ramon', achievements: '55', points: '2025' }],
arrayPoints = students
.filter(student => student.name === 'Nick')
.map(student => student.points);
console.log(arrayPoints);
If Array#flatMap
is implemented, you could take a single loop and filter and return a value.
The empty array has no items and this array is a neutral value which does not turn up in the result array.
const
students = [{ name: 'Nick', achievements: 158, points: 1473 }, { name: 'Nick', achievements: '175', points: '16375' }, { name: 'Ramon', achievements: '55', points: '2025' }],
arrayPoints = students
.flatMap(student => student.name === 'Nick'
? student.points
: []
);
console.log(arrayPoints);
For single loop result without undefined
. You could do with Array#reduce
students.reduce(function (acc,el) {
if(el.name=='Nick'){
acc.push(el.points)
}
return acc
},[]);
You can use reduce for that:
The reduce() method executes a reducer function (that you provide) on each element of the array, resulting in single output value.
So You can use it and check if the name is indeed Nick (el is the currentValue).
if so then push the points to the accumulator (which is arr).
[] represent the initialValue passed to the reduce function.
var arrayPoints = students.reduce((arr, el) =>
(el.name === 'Nick' && arr.push(el.points), arr), [])
You can find more info regarding reduce here:
https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
本文标签: javascriptFrom array of Objectget array of only matched key valueStack Overflow
版权声明:本文标题:javascript - From array of Object , get array of only matched key value - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745529941a2662013.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论