js 找到同一个数组中某个对象中相同的值
时间: 2023-02-15 20:35:07 浏览: 18
你可以使用 JavaScript 的 `Array.prototype.filter()` 方法来实现这个功能。这个方法会创建一个新数组,新数组中的元素是所有通过测试函数的原数组中的元素。
例如,如果你想在数组 `arr` 中找到所有对象的 `name` 属性等于 'john' 的对象,你可以这样写:
```
const johns = arr.filter(obj => obj.name === 'john');
```
这样,`johns` 数组就包含了所有 `name` 属性等于 'john' 的对象。
另外,如果你想在数组中找到所有具有相同值的对象,你可以使用 JavaScript 的 `Array.prototype.reduce()` 方法来实现。这个方法接受一个函数作为参数,这个函数会对数组中的每个元素执行一次,并返回一个单值。
例如,如果你想在数组 `arr` 中找到所有对象的 `name` 属性值相同的对象,你可以这样写:
```
const sameName = arr.reduce((acc, obj) => {
if (acc[obj.name]) {
acc[obj.name].push(obj);
} else {
acc[obj.name] = [obj];
}
return acc;
}, {});
```
这样,`sameName` 对象就包含了所有具有相同 `name` 属性值的对象,以 `name` 属性值为键,对应的对象为值。
希望这些信息对你有所帮助!
相关问题
js 找到同一个数组中某个对象中相同的值并分别返回
可以使用 Array.prototype.filter() 方法来实现。例如:
```
const arr = [
{ name: 'Alice', age: 21 },
{ name: 'Bob', age: 22 },
{ name: 'Charlie', age: 23 },
{ name: 'Alice', age: 24 },
];
const result = arr.filter(obj => obj.name === 'Alice');
console.log(result);
// Output: [{ name: 'Alice', age: 21 }, { name: 'Alice', age: 24 }]
```
使用 filter() 方法时,会对数组中的每个元素调用给定的回调函数。回调函数只有在回调函数返回值为 true 时,数组中的元素才会被包含在返回的结果数组中。在上面的例子中,我们使用了箭头函数作为回调函数,比较了每个对象的 name 属性,如果属性值为 'Alice',则返回 true。
如果要找出每种值出现的次数,可以使用 Array.prototype.reduce() 方法来实现。例如:
```
const counts = arr.reduce((counts, obj) => {
counts[obj.name] = (counts[obj.name] || 0) + 1;
return counts;
}, {});
console.log(counts);
// Output: { Alice: 2, Bob: 1, Charlie: 1 }
```
使用 reduce() 方法时,会对数组中的每个元素调用给定的回调函数。回调函数接收两个参数:一个累加器和当前元素。在这个例子中,我们使用了箭头函数作为回调函数,使用累加器来跟踪每个 name 出现的次数。
希望这个回答能帮到你!
JS 数组对象中某个属性值相同归类成一个数组
可以使用 Array.prototype.reduce() 方法来实现这个功能。具体步骤如下:
1. 遍历数组,对于每个元素,获取需要归类的属性值。
2. 在 reduce() 方法中创建一个对象,用于保存归类后的结果。
3. 对于每个元素,检查对象中是否已经存在该属性值对应的数组,如果不存在,则创建一个新数组,并将该元素添加到数组中;如果已经存在,则将该元素添加到对应的数组中。
4. 最后,将对象中的值转换为数组,即可得到归类后的结果。
下面是一个示例代码:
```javascript
const arr = [
{ name: 'apple', type: 'fruit' },
{ name: 'banana', type: 'fruit' },
{ name: 'carrot', type: 'vegetable' },
{ name: 'orange', type: 'fruit' },
{ name: 'broccoli', type: 'vegetable' },
{ name: 'pear', type: 'fruit' },
{ name: 'tomato', type: 'vegetable' },
{ name: 'pepper', type: 'vegetable' },
{ name: 'grape', type: 'fruit' },
{ name: 'potato', type: 'vegetable' },
];
const result = arr.reduce((obj, item) => {
if (obj[item.type]) {
obj[item.type].push(item);
} else {
obj[item.type] = [item];
}
return obj;
}, {});
console.log(result);
```
输出结果为:
```javascript
{
fruit: [
{ name: 'apple', type: 'fruit' },
{ name: 'banana', type: 'fruit' },
{ name: 'orange', type: 'fruit' },
{ name: 'pear', type: 'fruit' },
{ name: 'grape', type: 'fruit' }
],
vegetable: [
{ name: 'carrot', type: 'vegetable' },
{ name: 'broccoli', type: 'vegetable' },
{ name: 'tomato', type: 'vegetable' },
{ name: 'pepper', type: 'vegetable' },
{ name: 'potato', type: 'vegetable' }
]
}
```
在这个示例中,我们将 `arr` 数组中的元素按照 `type` 属性进行归类,得到一个包含 `fruit` 和 `vegetable` 两个属性的对象。每个属性对应一个数组,包含相同 `type` 属性值的元素。
相关推荐
![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)