-
Smart猫小萌
您可以尝试使用Array.prototype.map():该map()方法创建一个新数组,其中填充了对调用数组中的每个元素调用提供的函数的结果。const time = ['00:00', '00:30', '01:00', '01:30'];const cost = [1.40, 5.00, 2.00, 3.00];var result = time.map((t, i)=>({time: t, cost: cost[i]}));console.log(result);
-
当年话下
const time = ['00:00', '00:30', '01:00', '01:30']; const nums = [1.99, 5.11, 2.99, 3.45 ]; const newArray = []; time.forEach((element, index) => { newArray.push({ time: element, cost: nums[index] }) }) console.log(newArray)
-
猛跑小猪
可以通过以下方式完成:-const time = ['00:00', '00:30', '01:00', '01:30']const cost = [1.40, 5.00, 2.00, 3.00]let array = []for(let i=0; i<4; i++){ let obj = {} obj.time = time[i] obj.cost = cost[i] array.push(obj)}console.log(array)输出 -[ { time: '00:00', cost: 1.4 }, { time: '00:30', cost: 5 }, { time: '01:00', cost: 2 }, { time: '01:30', cost: 3 }]
-
互换的青春
您可以遍历两个数组之一,然后将对象填充到声明的数组中,如下所示。const time = ['00:00', '00:30', '01:00', '01:30'];const cost = [1.4, 5.0, 2.0, 3.0];let objArr = [];time.forEach((t, i) => { objArr[i] = { time: t, cost: cost[i], };});console.log(objArr);