簡體   English   中英

如何根據輸入值數組從對象數組返回匹配結果

[英]How to return the matching results from Array of object based on an array of input values

我有一個像下面這樣的對象數組

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

有人可以幫我解決這個問題嗎?

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

你可以用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) 

您可以使用三個步驟

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

您可以使用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) 

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

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

你可以用過濾器來減少##

  • 首先你需要過濾數組
  • 然后減少數組以獲得30,31,31的總和

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM