admin管理员组文章数量:1415684
I need to pick few keys and values (only name and age) from below array of objects.
const studentList = [{"id": "1", "name": "s1". "age": 10, "gender" : "m", "subject": "Maths"},{"id": "2", "name": "s2". "age": 11, "gender" : "m", "subject": "Maths"}]
I can achieve that using map and lodash pick as in below.
let nameAndAgeList = studentList.map(student => pick(student, ["name", "age"]));
But is there any more easy way with only using map. I know we can retrieve only one property as in below.
let nameArr = (studentList).map(({ name }) => name);
But how can we retrieve both name and age using map? Or any more easy & best ways with es6+
I need to pick few keys and values (only name and age) from below array of objects.
const studentList = [{"id": "1", "name": "s1". "age": 10, "gender" : "m", "subject": "Maths"},{"id": "2", "name": "s2". "age": 11, "gender" : "m", "subject": "Maths"}]
I can achieve that using map and lodash pick as in below.
let nameAndAgeList = studentList.map(student => pick(student, ["name", "age"]));
But is there any more easy way with only using map. I know we can retrieve only one property as in below.
let nameArr = (studentList).map(({ name }) => name);
But how can we retrieve both name and age using map? Or any more easy & best ways with es6+
Share Improve this question asked Mar 27, 2022 at 11:42 JackJack 3391 gold badge5 silver badges22 bronze badges 4-
Could you share the expected output? Is it an array of objects with
name
andage
props? If yes,const nameAgeList = studentList.map(({name, age, ...rest}) => ({name, age}));
<-- this may be helpful. Please try. – jsN00b Commented Mar 27, 2022 at 11:46 -
yes, which is exactly return by
nameAndAgeList
in above. – Jack Commented Mar 27, 2022 at 11:47 -
1
Please fix the first example as it does not enpass valid syntax. The
.
should be replaced by,
. – Ovidijus Parsiunas Commented Mar 27, 2022 at 11:51 - This is essentially the same question as How to get a subset of a javascript object's properties. – Dan Dascalescu Commented Apr 7, 2022 at 11:55
2 Answers
Reset to default 2The studentList
assignment is not valid (it contains dots instead of ma). Corrected in the snippet. You can map name
and age
in one go using:
const studentList = [
{"id": "1", "name": "s1", "age": 10, "gender" : "m", "subject": "Maths"},
{"id": "2", "name": "s2", "age": 11, "gender" : "m", "subject": "Maths"}];
console.log( studentList.map( ({name, age}) => ({name, age}) ) );
You can retrieve multiple properties, just like in the example below.
const studentList = [{"id": "1", "name": "s1", "age": 10, "gender" : "m", "subject": "Maths"},{"id": "2", "name": "s2", "age": 11, "gender" : "m", "subject": "Maths"}]
let nameArr = (studentList).map(({ name, age }) => {
return {'name': name, 'age': age}
});
console.log(nameArr)
本文标签: javascriptpick multiple keys and values from array of objectsStack Overflow
版权声明:本文标题:javascript - pick multiple keys and values from array of objects - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745202763a2647472.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论