【问题标题】:How to return the matching results from Array of object based on an array of input values如何根据输入值数组从对象数组返回匹配结果
【发布时间】:2019-05-28 00:21:01
【问题描述】:

我有一个像下面这样的对象数组

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 和 contains

我试过的是

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

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

【问题讨论】:

  • 你想要元素或添加年龄?
  • @CodeManiac 现在加年龄就够了
  • 它的sum 或一些
  • 嗯,您尝试过的尝试在哪里?因为你基本上只是要求有人为你做这件事,这不是 SO 的工作方式,但我相信你知道 ;-)
  • @IslamElshobokshy 我尝试过使用过滤器,我在代码中提到过,我没有包含我尝试过的代码

标签: javascript arrays json


【解决方案1】:

使用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"] 之类的名称示例,我该怎么做?
  • @ArunprasanthKV 是的,只需将 sum += age 更改为 sum += 1 ...如果您想计算名称的出现次数。
  • 不完全是。输入将是名称数组,并基于此我需要过滤并获得年龄的总和
  • so reduce((sum, {age,name})if(ages.includes(name)){ sum += age; } 这样就够了吧?
【解决方案2】:

你可以用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"] 之类的名称示例,我该怎么做?
【解决方案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"] 之类的名称示例,我该如何做同样的事情?
  • @ArunprasanthKV,为什么不呢?
  • 就像我需要得到年龄的总和,输入将是名称数组。你能指导我也这样做吗?
  • 哦,我明白了,你想要一些约翰和他们的年龄总和,对吧?
【解决方案4】:

您可以使用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"] 之类的名称示例,我该怎么做?
  • item.reduce((sum, {name, age})=> sum += ageArray.includes(name) ? age : 0, 0)
【解决方案5】:
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))

【讨论】:

    猜你喜欢
    • 2021-04-09
    • 2017-11-03
    • 1970-01-01
    • 2011-07-10
    • 1970-01-01
    • 2018-10-17
    • 2021-01-09
    • 2023-01-01
    • 2021-06-13
    相关资源
    最近更新 更多