作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
例如,我有一组属性(超过 3 个)
var propertyName = ["name", "esteira", "horse"];
我有数据值(数组数组):
var data = [["1", "2", "3"], ["3", "4", "5"], ["6", "7", "8"]];
如何将属性键设置为数据?
[
{ name: "1", esteira: "2", horse: "3" },
{ name: "3", esteira: "4", horse: "5" },
{ name: "6", esteira: "7", horse: "8" }
]
最佳答案
您可以使用 map 和 reduce 方法来获得结果。使用 reduce 方法创建对象并映射结果数组。
const propertyName = ['name', 'esteira', 'horse'];
const data = [
['1', '2', '3'],
['3', '4', '5'],
['6', '7', '8'],
];
const ret = data.map((x) => {
return x.reduce((prev, c, i) => {
const p = prev;
p[propertyName[i]] = c;
return p;
}, {});
});
console.log(ret);
关于javascript - 如何将数组数组转换为对象数组,从其他数组中选择键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65214667/
我是一名优秀的程序员,十分优秀!