Deji James
Deji James

Reputation: 437

How do I Map one array value agaisnt another to produce a result

Let's say there are two arrays

Array 1 is my Firebase JSON array of Objects as follows:

"players" : [
       {"name" : "YB", 
        "Team" : "Industry",
        "id": 1, 
        "position": "forward",
       {"name" : "Harry",
        "Team" : "Industry", 
        "id": 2, 
        "position": "defender",
        {"name" : "Honey",
        "Team" : "Industry", 
        "id": 3, 
        "position": "defender",
        {"name" : "abel",
        "Team" : "Industry", 
        "id": 4, 
        "position": "defender",
        {"name" : "Shaka",
        "Team" : "Industry", 
        "id": 5, 
        "position": "defender",
        {"name" : "ben",
        "Team" : "Industry", 
        "id": 6, 
        "position": "defender",
        {"name" : "randy",
        "Team" : "Industry", 
        "id": 7, 
        "position": "defender",
        {"name" : "john",
        "Team" : "Industry", 
        "id":8 , 
        "position": "defender"
         }
       ]

Array 2 is

[1,2,5,4,8,6]

Is there a way to loop through array 1, specifically the object property id , in search of the values of array 2 and then producing a resulting array of only the objects that have the ids 1,2,5,4,8,6 (i.e. an array of only the objects that have the id property value that corresponds with the values in array 2)

I'm very new to javascript/firebase cloud functions and was wondering how to achieve that. I tried a mapping function below:

var produce = array1.reduce (function(acc,next) {acc [next.id] = next; retrun acc;}, {});

var combine = array1.map(function(num){return produce[num];});

But it keeps returning an error message that array1.map is not a function. There has to be a simpler way to achieve this goal using the map function.

Upvotes: 0

Views: 68

Answers (2)

Parthipan Natkunam
Parthipan Natkunam

Reputation: 784

From the error it could be deduced that the variable you are trying to map is not an array.
Let's assume that the firebase response is of the following format:

var result = {"players":[
                           {"name" : "YB","Team" : "Industry", 
                            "id":1,"position":"forward"},
                           {"name" : "Harry","Team" : "Industry", 
                            "id": 2,"position": "defender"}
                          ]
              }

And, let the second array be as follows:

  var array2 = [1,2,5,4,8,6];

So the "result" variable is actually an object here and not an array. In this case, we might try something like the following to prevent the error mentioned in the question from occuring:

var finalResult = result.players.filter (function(player) {
                  return array2.indexOf(player.id) > -1;
              });

Upvotes: 1

Mohammad Usman
Mohammad Usman

Reputation: 39322

You can use filter your records by using .filter() and checking id in second array using .includes():

let data = [
  {"name" : "YB",    "Team" : "Industry", "id": 1, "position": "forward"},
  {"name" : "Harry", "Team" : "Industry", "id": 2, "position": "defender"},
  {"name" : "Honey", "Team" : "Industry", "id": 3, "position": "defender"},
  {"name" : "abel",  "Team" : "Industry", "id": 4, "position": "defender"},
  {"name" : "Shaka", "Team" : "Industry", "id": 5, "position": "defender"},
  {"name" : "ben",   "Team" : "Industry", "id": 6, "position": "defender"},
  {"name" : "randy", "Team" : "Industry", "id": 7, "position": "defender"},
  {"name" : "john",  "Team" : "Industry", "id": 8, "position": "defender"}
];

let matchIds = [1, 2, 5, 4, 8, 6];

let result = data.filter(({ id }) => matchIds.includes(id));

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

Upvotes: 2

Related Questions