[英]How can i extract all the element from object dynamically
How can I extract the specific column from an object, I have a column array and I want these fields extracted from an object which will be constructed by map loop function which is the item. 我如何从一个对象中提取特定的列,我有一个列数组,我想从一个对象中提取这些字段,该对象将由作为对象的map loop函数构造。 Now here, how can check my fields dynamically.
现在在这里,如何动态检查我的字段。 i don't want item[col[0]] like this.
我不想要item [col [0]]这样。 please tell me a short cut.
请告诉我捷径。
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())
How can I configure here, that I show stop increment or not. 如何在此处配置是否显示停止增量。 item[col[i]], item[col[i+1]] i want it dynamically
item [col [i]],item [col [i + 1]]我想要动态
Just iterate over the col
properties and .map
to a new array. 只需遍历
col
属性和.map
到新数组即可。 You could also consider changing the person
variable name to people
(or something like that), because it's a collection of persons, not a singular person, reducing the chance of confusion: 你也可以考虑改变
person
变量名people
(或类似的东西),因为它是人,而不是一个单一的人,减少混乱的机会的集合 :
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]) ) );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.