How to reduce an array of object for unique key value pairs using an another array?

  javascript, json

I have an array of object that contains different key value pairs and I’m trying to reduce this array of object using an another array.

I can refer the array that need to be reduced as the "Reducible Array" and the array I’m using to reduce as "Key array".

Reducible Array:

[
  {
    "movie": {
      "title": "Avengers",
      "universe": "MARVEL",
      "owned": "Walt Disney"
    }
  },
  {
    "movie": {
      "title": "Captain America",
      "universe": "MARVEL",
      "owned": "Walt Disney"
    }
  },
  {
    "movie": {
      "title": "Justice League",
      "universe": "DC",
      "owned": "Warner Bros"
    }
  }
]

Key Array:

[DC, MARVEL]

The reduced array of object I’m expecting after the operation is as follows.

Expected Result:

[
  {
    "Universe": "Marvel",
    "OwnedBy": "Walt Disney"
  },
  {
    "Universe": "DC",
    "OwnedBy": "Warner Bros"
  }
]

What are all the operations I need to perform to get the expected result?

Source: Ask Javascript Questions

LEAVE A COMMENT