vue 数组1 {"typeid":1,2,"typeid":2} 数组2 [{"typeid":1,"name":"A"},{"typeid":2,"name":"B"} ] 将数组1里面的1,2 替换成A,B
时间: 2023-10-12 12:06:22 浏览: 145
数组查找及替换1
在Vue中,可以使用`map`方法来遍历数组1,并根据每个对象的`typeid`属性来替换成对应的名称。以下是一个示例代码:
```javascript
const arr1 = [{"typeid":1},{"typeid":2}];
const arr2 = [{"typeid":1,"name":"A"},{"typeid":2,"name":"B"}];
const result = arr1.map(item => {
const { typeid } = item;
const replacement = arr2.find(obj => obj.typeid === typeid);
return { ...item, ...replacement };
});
console.log(result); // 输出:[{"typeid":1,"name":"A"},{"typeid":2,"name":"B"}]
```
阅读全文