我如何动态地从对象中提取所有元素
How can i extract all the element from object dynamically
我如何从一个对象中提取特定的列,我有一个列数组,我想从一个对象中提取这些字段,该对象将由 map 循环函数构造,这是项目。
现在在这里,如何动态检查我的字段。我不想要这样的 item[col[0]]。
请告诉我一个捷径。
const person = [{
firstName: "Nick",
lastName: "Anderson",
age: 35,
sex: "M"
},
{
firstName: "yopm",
lastName: "Geyun",
age: 36,
sex: "M"
}]
const col=['firstName' , 'age']
return person.map(item=>{
var i=0;
return [
//How can i confgure here , that i show stop increment or not.
item[col[i]],
item[col[i+1]]
//here i want to fetch my colums['firstName' , 'age] from item {
//{firstName: "Nick",lastName: "Anderson",age: 35,sex: "M"} dynamically.
]
})
}
console.log(func())
我如何在这里配置,是否显示停止增量。
项目[col[i]],
项目[col[i+1]]
我想要动态的
只需遍历 col
属性和 .map
到一个新数组。您还可以考虑将 person
变量名称更改为 people
(或类似名称),因为它是一个 collection 人,而不是单个人,减少了混淆的机会:
const people = [{
firstName: "Nick",
lastName: "Anderson",
age: 35,
sex: "M"
},
{
firstName: "yopm",
lastName: "Geyun",
age: 36,
sex: "M"
}
]
const col = ['firstName', 'age']
console.log(
people.map(
person => col.map(prop => person[prop])
)
);
我如何从一个对象中提取特定的列,我有一个列数组,我想从一个对象中提取这些字段,该对象将由 map 循环函数构造,这是项目。 现在在这里,如何动态检查我的字段。我不想要这样的 item[col[0]]。 请告诉我一个捷径。
const person = [{
firstName: "Nick",
lastName: "Anderson",
age: 35,
sex: "M"
},
{
firstName: "yopm",
lastName: "Geyun",
age: 36,
sex: "M"
}]
const col=['firstName' , 'age']
return person.map(item=>{
var i=0;
return [
//How can i confgure here , that i show stop increment or not.
item[col[i]],
item[col[i+1]]
//here i want to fetch my colums['firstName' , 'age] from item {
//{firstName: "Nick",lastName: "Anderson",age: 35,sex: "M"} dynamically.
]
})
}
console.log(func())
我如何在这里配置,是否显示停止增量。 项目[col[i]], 项目[col[i+1]] 我想要动态的
只需遍历 col
属性和 .map
到一个新数组。您还可以考虑将 person
变量名称更改为 people
(或类似名称),因为它是一个 collection 人,而不是单个人,减少了混淆的机会:
const people = [{
firstName: "Nick",
lastName: "Anderson",
age: 35,
sex: "M"
},
{
firstName: "yopm",
lastName: "Geyun",
age: 36,
sex: "M"
}
]
const col = ['firstName', 'age']
console.log(
people.map(
person => col.map(prop => person[prop])
)
);