1

I am working with an Angular 2 Ionic app -

Is there a way I can filter all persons by age in my personsArr and then find if any key in my personObj matches a personArr name and return a boolean?

I have an example array like:

personsArr = [{
        "name": "Ram",
        "email": "[email protected]",
        "age": 23
    },
    {
        "name": "peter",
        "email": "[email protected]",
        "age": 23
    },
    {
        "name": "John",
        "email": "[email protected]",
        "age": 33
    },
    {
        "name": "Bob",
        "email": "[email protected]",
        "age": 41
    }
]

and an object like this

personObj = {
    "peter": "helper",
    "Ram": "helper",
    "Bob": "helper"
}

How can I filter personsArr by age and then check if any key from personObj matches person name?

Pretty sure I can just do this with a loop, but is there a better way?

Appreciate any help or advise.

2
  • What are expected results to store the boolean? Please also show us what you tried with your loop approach Commented Nov 16, 2018 at 8:31
  • What do you mean with "filter by age"? Commented Nov 16, 2018 at 8:34

5 Answers 5

3

You can try this, with filter and some. You filter by your condition and then check for matches on the object. Hope this helps.

const personsArr = [{
    "name": "Ram",
    "email": "[email protected]",
    "age": 23
    }, {
        "name": "peter",
        "email": "[email protected]",
        "age": 23
    }, {
        "name": "John",
        "email": "[email protected]",
        "age": 33
    }, {
        "name": "Bob",
        "email": "[email protected]",
        "age": 41
    }];

const personObj = { "Ram": "helper", "Bob": "helper" };

const personObj2 = { "Ram2": "helper", "Bob2": "helper" };

const checkArr = (age, arr, obj) => arr.filter((f) => f.age > age).some((d) => obj[d.name]);

console.log('Should be true');
console.log(checkArr(30, personsArr, personObj));
console.log('Should be false');
console.log(checkArr(30, personsArr, personObj2));

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

Comments

2

Here you are

// Filter by age
const legalAge = 18 // for example
const boolean = personsArr
    // Actual filtering 
    .filter(p => p.age > legalAge)
    // Check if there is any `name` in personsArr matching any key in personObj
    .some(p =>
        Object.keys(personObj).some(pk => pk === p.name)
    )

// Sort by age (maybe you meant this instead of filtering)
personsArr.sort((a, b) => a.age > b.age ? 1 : -1)
const boolean2 = personsArr.some(p =>
    Object.keys(personObj).some(pk => pk === p.name)
)

Comments

1

If I understand you correctly then you need something like this:

const isFound = personsArr.some((item) => item.age > 30 && personObj.includes(item.name));

Of course item.age > 30 should be replaced with you filtering criteria.

Comments

1

Whether you loop yourself or not, there is looping involved. The simplest answer is probably to use Array.filter.

personsArr.filter((val) => val.age === age && Object.keys(roles).indexOf(val.name) > -1);

Here is a complete example:

const personsArr = [
    { "name": "Ram", "email": "[email protected]", "age": 23 },
    { "name": "peter", "email": "[email protected]", "age": 23 },
    { "name": "John", "email": "[email protected]", "age": 33 },
    { "name": "Bob", "email": "[email protected]", "age": 41 }
];

const personObj = { "peter": "helper", "Ram": "helper", "Bob": "helper" };

function filter(age: number, roles: { [name: string]: string }) {
    const keys = Object.keys(roles);
    return personsArr.filter((val) => val.age === age && keys.indexOf(val.name) > -1);
}

// 0: Object { name: "Ram", email: "[email protected]", age: 23 }
​// 1: Object { name: "peter", email: "[email protected]", age: 23 }
console.log(filter(23, personObj));

// 0: Object { name: "Bob", email: "[email protected]", age: 41 }
console.log(filter(41, personObj));

Comments

1

var personsArr = [    
{"name":"Ram", "email":"[email protected]", "age":23},    
{"name":"peter", "email":"[email protected]", "age":23},  
{"name":"John", "email":"[email protected]", "age":33},    
{"name":"Bob", "email":"[email protected]", "age":41}   
];

var personObj = {"peter":"helper","Ram":"helper", "Bob":"helper"}

var newPersonsArr = personsArr.filter((person) => person.age<30);
var check = false;
for(var key in personObj) {
  newPersonsArr.map((person) => {
  if(person.name === key){
   check = true
  }
  });
}


console.log(check);

2 Comments

As you are already using Array.filter for age you could check the name condition at the same time, thus reducing the amount of looping.
This works - Is there anyway to break the loop after the first match? When I console log I am getting many additional loops of true.

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.