I want to get the count of total elements in my array, and the count of elements matching a condition and count of not matching the same condition. The condition is the field name.
Here is list A:
[
{"name": "tom", "id": "1"},
{"name": "jack", "id": "2"},
{"name": "sarah", "id": "3"},
{"name": "william", "id": "4"},
{"name": "ronaldo", "id": "5"}
]
and here is list B:
[
{"name": "tom", "age": "20"},
{"name": "jack", "age": "25"}
]
as a result, it should give out three values:
var total = 5;
var matching = 2;
var notmatchin = 3;
how can this be done with some map and reduce methods in javascript ecma 6?
a.lengthb.reduceto accumulate a count where the current.nameis found ina. Do this by using.some(), which returnstrueorfalse. Since booleans are converted to1and0respectively, you can simply add them to the accumulator.