let a=[{name:1,age:2}{name:2,age:3}] let b =[{msg:3,gge:4}{list:4,arr:5}] 使用js方法将数组对象里的每个属性都加到数组a对象里去
时间: 2024-02-16 11:03:48 浏览: 116
es6中let和const的使用方法详解
可以使用 JavaScript 中的 for 循环和 Object.keys() 方法来遍历对象并将属性添加到数组对象中。具体实现如下:
```javascript
let a = [{name:1,age:2}, {name:2,age:3}];
let b = [{msg:3,gge:4}, {list:4,arr:5}];
for(let i = 0; i < a.length; i++) {
let obj = a[i];
let keys = Object.keys(obj);
for(let j = 0; j < keys.length; j++) {
let key = keys[j];
let value = obj[key];
a[i][key] = value;
}
}
for(let i = 0; i < b.length; i++) {
let obj = b[i];
let keys = Object.keys(obj);
for(let j = 0; j < keys.length; j++) {
let key = keys[j];
let value = obj[key];
a[i][key] = value;
}
}
console.log(a);
```
输出结果为:
```javascript
[
{name: 1, age: 2, msg: 3, gge: 4},
{name: 2, age: 3, list: 4, arr: 5}
]
```
阅读全文