javascript - 如何用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/
解决方案
一张地图就够了
const array = [50, 140, 60, 160];
const getcontract = {
id: 1,
supplier_a: "Supplier A"
}
const result = array.map(price => ({ ...getcontract,
price
}));
console.log(result);
推荐阅读
- c - 在使用诸如 C 之类的编程语言的某些软件中是否有间接寻址的实际应用?
- numpy - 如何根据值标记 numpy 数组?
- python-2.7 - 使用“with”方法在 Python 中保存文件
- android - Android InputType.TYPE_NUMBER_VARIATION_PASSWORD 到可见数字密码(仅限数字)
- r - 是否有一个 R 函数可以替换使用多个数据帧的几列的循环?
- c++ - 在opengl矩形上使用着色器会导致它消失
- php - 使用未定义的常量字符串 - 从变量调用类名时假定为“字符串”
- php - 在 MYSQL 数据库中存储和查询 hastags 的最有效方法是什么
- node.js - 从打字稿导入类型定义(d.ts)
- laravel - 从 lumen api 到 laravel 应用程序的数据表不起作用