首页 > 解决方案 > 如何用vue制作对象数组?

问题描述

我有一个这样的数组array : [ 50, 140, 60, 160 ]

我有一个像这样的对象

getcontract: {
  id: 1,
  supplier_a: "Supplier A"
}

我想制作对象数组,所以它会变成这样

array : [
{ id:1, supplier_a: "Supplier A", price: 50 },
{ id:1, supplier_a: "Supplier A", price: 140 },
{ id:1, supplier_a: "Supplier A", price: 60 },
{ id:1, supplier_a: "Supplier A", price: 160 }
]

怎么做?这是我的 jsfiddle https://jsfiddle.net/damakuro221/h6tvzrdf/15/

标签: javascriptvue.js

解决方案


一张地图就够了

const array = [50, 140, 60, 160];
const getcontract = {
  id: 1,
  supplier_a: "Supplier A"
}

const result = array.map(price => ({ ...getcontract,
  price
}));


console.log(result);


推荐阅读