1

This is my data

[{name:'james',grade:'A'},
{name:'john',grade:'B'},
{name:'iris',,grade:'A'},
{name:'ivan',,grade:'C'}]

I want to keep object that has grade A and C, it's easy I can just do filter like

person.filter(obj => obj.grade === 'A' || obj.grade === 'C')

but now I have an array of object.

[{grade:'A'},{grade:'C'}]

any clue how can I do filtering now? do I need nested loop?

10
  • You mean you have an array of arrays? Commented May 25, 2017 at 6:29
  • @AndrewLi no, array of object Commented May 25, 2017 at 6:30
  • Can you give an example of the data you're dealing with? Your question is unclear Commented May 25, 2017 at 6:30
  • @AndrewLi 'C' and 'A' is not string or value stored in any variable but array of object. Commented May 25, 2017 at 6:31
  • What's wrong with your current code? The way you've phrased your question implies it should work. It's confusing. Commented May 25, 2017 at 6:33

5 Answers 5

1

Use Array.prototype.some:

let person = [{name:'james', grade:'A'},
              {name:'john', grade:'B'},
              {name:'iris', grade:'A'},
              {name:'ivan', grade:'C'}];

let predicate = [{grade:'A'},{grade:'C'}];
        
let result = person.filter(obj => predicate.some(p => p.grade == obj.grade))

console.log('result:', result)

If your predicate is more dynamic than that, compare all object properties instead of just p.grade.

person.filter(obj => predicate.some(p => {
    return Object.keys(p).every(k => obj[k] == p[k]);
}));
Sign up to request clarification or add additional context in comments.

6 Comments

Your predicate is wrong, it doesn't include names. OP's output is weird, because it discards names for some reason, keeping only the grades.
No, predicate is OP's "but now I have an array of object". person is the first array which includes names. Edited to clarify.
Oh damn, I think I just understood the question. I thought his filter was wrong, but actually he wants to reapply a second filter. Sorry
I can see where you fell off the wagon there… :)
some is enough to do the job although I don't understand the second block of ur code.
|
0

Using underscore lib eg -

    var bbb = [
        {id: 839},
        {id: 854}
    ];
     var ids = {};
    _.each(bbb, function (bb) { ids[bb.id] = true; });

var data = [{grade:'A'},{grade:'C'}];
var value = {};
_.each(data , function (d) { value[data.garde] === 'A' | value[data.garde] === 'C' ; });

3 Comments

Code-only answers aren't helpful. What is the OP's issue? How does your code fix it?
@JeremyThille i guess using libraries is makes the code cleaner i feel it purely optionaly i feel
Yes sorry, I misunderstood OP's question. I de-downvoted your answer :)
0

Objects having grades A and C should be filtered as (classic js syntax),

var a = [
  {name:'james',grade:'A'},
  {name:'john',grade:'B'},
  {name:'iris',grade:'A'},
  {name:'ivan',grade:'C'}
];

a.filter(function(e) { 
  return (e.grade == 'A') || (e.grade == 'C');
}); 

6 Comments

what's wrong with that answer? -)) missing semicolon? -))
I made the same mistake, I misunderstood OP's question. His first filter works, but now he wants to apply a second filter based on predicate [{grade:'A'},{grade:'C'}]. His question is very misleading indeed. (I didn't downvote)
i dont really care about downvotes but I really didn't understand what he wants -))
Well you don't come and help people on Stackoverflow to get downvoted, do you? :)
sure, but "sapere aude" outweighs such situations -))
|
0

If you have many grades to check (I don't know like all of them ;) ). You could first convert array into Set

const grades = new Set([{grade:'A'},{grade:'C'}].map(({grade}) => grade))

const persons = [{name:'james',grade:'A'},
  {name:'john',grade:'B'},
  {name:'iris',grade:'A'},
  {name:'ivan',grade:'C'}]

And then filter persons array using has

const filtered = persons.filter(({grade}) => grades.has(grade))

1 Comment

surely this can be more generic, so that if user wants to also add a filter item {name:'john'} you wouldn't have to rewrite it :p (good answer though)
0

You could use a Set for the predicates and filter accordingly

let person = [{ name: 'james', grade: 'A' }, { name: 'john', grade: 'B' }, { name: 'iris', grade: 'A' }, { name: 'ivan', grade: 'C' }],
    predicate = [{ grade: 'A' }, { grade: 'C' }],
    result = person.filter((s => p => s.has(p.grade))(new Set(predicate.map(p => p.grade))));

console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }

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.