对比两个数组里面每个对象的属性值拿到当前相同的值
时间: 2023-06-05 13:47:46 浏览: 98
可以使用双重循环来遍历两个数组中的每个对象,然后再比较每个对象的属性值是否相同。如果相同,则将该值存储到一个新的数组中。具体实现可以参考以下代码:
```javascript
let arr1 = [{id: 1, name: '张三'}, {id: 2, name: '李四'}, {id: 3, name: '王五'}];
let arr2 = [{id: 2, name: '李四'}, {id: 4, name: '赵六'}, {id: 5, name: '钱七'}];
let result = [];
for (let i = ; i < arr1.length; i++) {
for (let j = ; j < arr2.length; j++) {
if (arr1[i].id === arr2[j].id && arr1[i].name === arr2[j].name) {
result.push(arr1[i]);
}
}
}
console.log(result); // 输出 [{id: 2, name: '李四'}]
```
以上代码中,我们先定义了两个数组 `arr1` 和 `arr2`,然后定义了一个空数组 `result` 用来存储相同的对象。接着使用双重循环遍历两个数组中的每个对象,如果两个对象的 `id` 和 `name` 属性值都相同,则将该对象存储到 `result` 数组中。最后输出 `result` 数组即可。
相关问题
两个数组对象根据相同的属性的属性值,合并对象
可以使用 Array.prototype.reduce() 方法来实现合并两个数组对象。具体实现步骤如下:
1. 首先使用 Array.prototype.concat() 方法将两个数组合并为一个数组。
2. 使用 Array.prototype.reduce() 方法来遍历合并后的数组,并将每个对象的相同属性值作为键名,将对象合并到同一个键名下。
3. 最后使用 Object.values() 方法将合并后的对象转换为数组。
以下是一个示例代码:
```javascript
const arr1 = [
{ id: 1, name: 'John', age: 20 },
{ id: 2, name: 'Alice', age: 25 },
{ id: 3, name: 'Bob', age: 30 }
];
const arr2 = [
{ id: 2, address: 'New York' },
{ id: 3, address: 'San Francisco' },
{ id: 4, address: 'Los Angeles' }
];
const mergedArr = arr1.concat(arr2).reduce((acc, obj) => {
const key = obj.id;
if (!acc[key]) {
acc[key] = obj;
} else {
acc[key] = { ...acc[key], ...obj };
}
return acc;
}, {});
const result = Object.values(mergedArr);
console.log(result);
```
运行以上代码,输出结果如下:
```
[
{ id: 1, name: 'John', age: 20 },
{ id: 2, name: 'Alice', age: 25, address: 'New York' },
{ id: 3, name: 'Bob', age: 30, address: 'San Francisco' },
{ id: 4, address: 'Los Angeles' }
]
```
可以看到,两个数组对象已经成功合并,并根据相同属性的属性值进行了合并。
根据相同的两个属性值将两个数组对象合并
可以通过使用JavaScript中的`Array.prototype.reduce`方法来合并两个数组对象,具体实现如下:
```javascript
const arr1 = [{ name: 'Alice', age: 20 }, { name: 'Bob', age: 25 }];
const arr2 = [{ name: 'Charlie', age: 30 }, { name: 'David', age: 35 }];
const mergedArr = [...arr1, ...arr2].reduce((acc, curr) => {
const existingObj = acc.find((obj) => obj.name === curr.name && obj.age === curr.age);
if (existingObj) {
Object.assign(existingObj, curr);
} else {
acc.push(curr);
}
return acc;
}, []);
console.log(mergedArr); // Output: [{ name: 'Alice', age: 20 }, { name: 'Bob', age: 25 }, { name: 'Charlie', age: 30 }, { name: 'David', age: 35 }]
```
在上述代码中,首先将两个数组对象合并为一个数组,然后使用`reduce`方法遍历这个数组。在每次迭代中,我们检查当前对象是否已经存在于累加器数组中,如果是,则使用`Object.assign`方法将现有对象的属性与当前对象的属性合并。否则,我们将当前对象添加到累加器数组中。最后,`reduce`方法返回合并后的数组对象。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)