【发布时间】:2021-07-31 05:54:06
【问题描述】:
对不起,如果重复了一些问题,但我搜索了很多,没有找到任何东西。 我必须在js中填写一个表格。为此,我获取对象数组(中心)的值并应用地图。 一切正常。
centers = [
{ 'center': 'Center 1',
'datos': { "key1":4.67,
"key2":3.56
}
},
{ 'center': 'Center 2',
'datos': { "key1":0.34,
"key2":5}
},
{ 'center': 'Center 3',
'datos': { "key1":3.7,
"key2":2.5}
}
]
let result = centers.map(res => {
let columns = {'column1':res.datos.key1, 'column2':res.datos.key2}
return columns;
})
/* result = [
[
{ column1: 4.67, column2: 3.56 },
{ column1: 0.34, column2: 5 },
{ column1: 3.7, column2: 2.5 }
]
]
*/
但是,“key1 ...... key1000”的项目很多,我想像这样在地图函数中实现它们
var cc = ['key1','key2','key3','key4','key5'];
let result = centers.map(res => {
let columns = {'column1':res.datos.cc[0], 'column2':res.datos.cc[1], ...........}
return columns;
})
但它不取变量的值。 地图可以吗?谢谢。
【问题讨论】:
-
要从动态键访问对象值,请尝试
res.datos[cc[0]]
标签: javascript arrays dictionary object iterator