基于输入值的数组,如何从对象数组中返回匹配的结果

4
我有一个如下的对象数组
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

我已经尝试使用过滤器来解决这个问题,但是不确定如何使用filtercontains一起使用。

我尝试过的方法是:

var result = item .filter(obj => {
                    return obj.age..includes(ages);
                })

有人能帮我解决这个问题吗?


你想要元素还是年龄的加法? - Code Maniac
嗯,你尝试过哪些方法呢?因为你基本上只是在请求别人完全替你完成,这不是 SO 的工作方式,但我相信你已经意识到了;-) - Alexandre Elshobokshy
@IslamElshobokshy 我已经尝试使用过滤器,并在代码中提到了它,我没有包含我尝试过的代码,仅此而已。 - Arunprasanth K V
你应该这样做,这样人们会尝试修复你尝试的代码,并帮助指出你所犯的错误。干杯! - Alexandre Elshobokshy
@IslamElshobokshy 确定,我会在问题中加入我的努力。 - Arunprasanth K V
显示剩余2条评论
5个回答

6

使用Array#reduce

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);


一个疑问,如果输入是类似于 ["john","john1"] 这样的名称示例,我该如何做到同样的效果? - Arunprasanth K V
@ArunprasanthKV 是的,只需将 sum += age 更改为 sum += 1 ... 如果您想要计算名称的出现次数。 - kemicofa ghost
不是很准确。输入将是名称数组,根据此需要进行过滤并获取年龄总和。 - Arunprasanth K V
所以,reduce((sum, {age,name})if(ages.includes(name)){ sum += age; } 这样就足够了吗? - Arunprasanth K V

3
你可以使用reduce函数实现这个功能。详细信息请参考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)


一个疑问,如果输入是像["john","john1"]这样的名字例子,我该如何做到相同的效果? - Arunprasanth K V

3

您可以采用三个步骤:

  1. 仅获取年龄
  2. 使用Set筛选年龄
  3. 添加剩余值

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);


一个问题,如果输入是像 ["john", "john1"] 这样的名字示例,我该如何做到相同的效果? - Arunprasanth K V
@ArunprasanthKV,为什么不呢? - Nina Scholz
我需要计算年龄总和,输入将是姓名数组。你能指导我如何做吗? - Arunprasanth K V
哦,我明白了,你想要一些约翰的人和他们年龄的总和,对吗? - Nina Scholz

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)


一个疑问,如果输入是像["john","john1"]这样的名字,我该如何做到相同的效果? - Arunprasanth K V
1
item.reduce((sum, {name, age})=> sum += ageArray.includes(name) ? age : 0, 0) - adiga

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))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接