我有一个像下面这样的对象数组
var item = [
{ "name": "John", "age": 30, "city": "New York1" },
{ "name": "John1", "age": 31, "city": "New York2" },
{ "name": "John2", "age": 32, "city": "New York3" },
{ "name": "John3", "age": 31, "city": "New York3" }
]
我想要的是从具有年龄属性值的对象数组中获得一些年龄
在[30,31]
因此,基本上,输入将是一个整数数组,例如var ageArray=[30,31];
示例:
输入:[30,31]
输出:以下对象age
的总和
{ "name":"John", "age":30, "city":"New York1"},
{ "name":"John1", "age":31, "city":"New York2"},
{ "name":"John3", "age":31, "city":"New York3"}
所以这里是
92
我尝试为此使用过滤器,但不确定如何将filter
与包含
我尝试过的是
var result = item .filter(obj => {
return obj.age..includes(ages);
})
有人可以帮我解决这个问题吗?
答案 0 :(得分:6)
const items = [
{ "name":"John", "age":30, "city":"New York1"},
{ "name":"John1", "age":31, "city":"New York2"},
{ "name":"John2", "age":32, "city":"New York3"},
{ "name":"John3", "age":31, "city":"New York3"}]
const ages = [30, 31];
const res = items.reduce((sum, {age})=>{
return sum + (ages.includes(age) ? age : 0);
}, 0);
console.log(res);
为名称修改:
const items = [
{ "name":"John", "age":30, "city":"New York1"},
{ "name":"John1", "age":31, "city":"New York2"},
{ "name":"John2", "age":32, "city":"New York3"},
{ "name":"John3", "age":31, "city":"New York3"}]
const names = ["John", "john1"].map(n=>n.toLowerCase());
const res = items.reduce((sum, {name, age})=>{
return sum + (names.includes(name.toLowerCase()) ? age : 0);
}, 0);
console.log(res);
答案 1 :(得分:3)
您可以使用reduce
var item= [{ "name":"John", "age":30, "city":"New York1"},{ "name":"John1", "age":31, "city":"New York2"},{ "name":"John2", "age":32, "city":"New York3"},{ "name":"John3", "age":31, "city":"New York3"}]
var ageArray=[30,31];
let op = item.reduce((o,c)=>{
if( ageArray.includes(c.age) )
{ o+=c.age }
return o;
},0)
console.log(op)
答案 2 :(得分:3)
您可以使用三个步骤
Set
var items = [{ name: "John", age: 30, city:" New York1" }, { name: "John1", age: 31, city: "New York2" }, { name: "John2", age: 32, city: "New York3" }, { name: "John3", age: 31, city: "New York3" }],
ageArray = [30, 31],
result = items
.map(({ age }) => age)
.filter(Set.prototype.has, new Set(ageArray))
.reduce((a, b) => a + b, 0);
console.log(result);
对于不同的键进行过滤和添加的方法略有不同。
var items = [{ name: "John", age: 30, city:" New York1" }, { name: "John1", age: 31, city: "New York2" }, { name: "John2", age: 32, city: "New York3" }, { name: "John3", age: 31, city: "New York3" }],
names = ['John', 'John2'],
result = items
.filter((s => ({ name }) => s.has(name))(new Set(names)))
.map(({ age }) => age)
.reduce((a, b) => a + b, 0);
console.log(result);
答案 3 :(得分:2)
您可以使用reduce
一行完成此操作:
const item = [{name:"John",age:30,city:"New York1"},{name:"John1",age:31,city:"New York2"},{name:"John2",age:32,city:"New York3"},{name:"John3",age:31,city:"New York3"}]
,ageArray = [30,31]
,total = item.reduce((sum, {age})=> sum += ageArray.includes(age) ? age : 0, 0);
console.log(total)
答案 4 :(得分:1)
const items = [
{ "name":"John", "age":30, "city":"New York1"},
{ "name":"John1", "age":31, "city":"New York2"},
{ "name":"John2", "age":32, "city":"New York3"},
{ "name":"John3", "age":31, "city":"New York3"}
];
const ages = [30,31];
const result = items.filter(o => ages.find(o2 => o.age === o2)).map(o3 => o3.age);
console.log(result)
const reducer = (accumulator, currentValue) => accumulator + currentValue;
console.log(result.reduce(reducer))
答案 5 :(得分:0)
function getSum(total, { age }) {
if(age===30 ||age===31) return total + age
return total
}
var item = [
{ "name": "John", "age": 30, "city": "New York1" },
{ "name": "John1", "age": 31, "city": "New York2" },
{ "name": "John2", "age": 32, "city": "New York3" },
{ "name": "John3", "age": 31, "city": "New York3" }
];
let sumItem = item.reduce(getSum,0)
console.log(sumItem);