Vue 将两个数组中的值分别添加进同一个对象里然后生成一个对象数组
初始数据有:var title = ["颜色", "叶形", "叶面", "脉相"]var field = ['color_value', 'leaf_surface', 'leaf_shape', 'leaf_vein']想要实现的对象数组arr=[{field: "color_value",title: "颜色"},{field: "leaf_surface",title: "叶形"},{fi
·
初始数据有:
var title = ["颜色", "叶形", "叶面", "脉相"]
var field = ['color_value', 'leaf_surface', 'leaf_shape', 'leaf_vein']
想要实现的对象数组
arr=[
{field: "color_value",title: "颜色"},
{field: "leaf_surface",title: "叶形"},
{field: "leaf_shape",title: "叶面"},
{field: "leaf_vein",title: "脉相"}
]
实现方法
var title = ["颜色", "叶形", "叶面", "脉相"]
var field = ['color_value', 'leaf_surface', 'leaf_shape', 'leaf_vein']
var arr = []
title.forEach(item => {
arr.push({
title: item,
field: ''
})
})
field.forEach((item, i) => {
arr[i].field = item
})
console.log(arr)
//优化写法
title.forEach((item, i) => {
arr.push({
title: item,
field: filed[i]
});
});
更多推荐
已为社区贡献21条内容
所有评论(0)