初始数据有:

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]
	});
});

Logo

前往低代码交流专区

更多推荐