4

I have an array of objects like below

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" }
]

What I want is to get some of the age from this array of objects which has age property value in [30,31]

So basically the input will be an array of integers like var ageArray=[30,31];

Example :

Input : [30,31]

Output : Sum of the age of the below objects

 { "name":"John", "age":30, "city":"New York1"},
 { "name":"John1", "age":31, "city":"New York2"},
 { "name":"John3", "age":31, "city":"New York3"}

So here it will be

92

I have tried to use filter for this but not sure how can i use filter with contains

What I have tried is

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

Can someone help me to resolve this ?

7
  • you want elements or addition of age ? Commented Dec 31, 2018 at 13:16
  • @CodeManiac addition of age is enough now Commented Dec 31, 2018 at 13:17
  • its sum or some Commented Dec 31, 2018 at 13:21
  • Mhmm where are the attempts you tried tho? Cuz you're basically just asking for someone to do it fully for you, which isn't how SO works, but I'm sure you're aware of that ;-) Commented Dec 31, 2018 at 13:23
  • @IslamElshobokshy I have tried using filters, I have mentioned it in the code , i didnt include the code that i have tried thats all Commented Dec 31, 2018 at 13:26

5 Answers 5

6

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

Modified for names:

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

Sign up to request clarification or add additional context in comments.

4 Comments

one doubt how can i do the same if the inputs are name example like ["john","john1"]?
@ArunprasanthKV yes, simply change sum += age to sum += 1 ... if you wish to count the occurences of the names.
not exactly. the inputs will be array of names and based on that i need to filter and get the sum of age
so reduce((sum, {age,name})if(ages.includes(name)){ sum += age; } this is enough right ?
3

You can do it with 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 Comment

one doubt how can i do the same if the inputs are name example like ["john","john1"]?
3

You could use three steps

  1. get only the ages
  2. filter the ages with a Set
  3. add remaining values

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

A slightly different approach for different keys to filter and to add.

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

4 Comments

one doubt how can i do the same if the inputs are name example like ["john","john1"]?
@ArunprasanthKV, why not?
like i need to get the sum of age and the input will be array of names. can you please guide me to do the same too ?
oh, i see, you want some johns and the sum of ages of them, right?
2

You can use reduce to do this in one line:

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)

2 Comments

one doubt how can i do the same if the inputs are name example like ["john","john1"]?
item.reduce((sum, {name, age})=> sum += ageArray.includes(name) ? age : 0, 0)
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))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.