1

I have one array of objects, and one object:

Array:

var arr = [{
  "key": "id",
  "value": "text_1"
},
{
  "key": "created_at",
  "value": "text_2"
},
{
  "key": "name",
  "value": "text_3"
},
{
  "key": "email",
  "value": "text_4"
}
];

Object:

var obj = {
  "id": 15,
  "created_at": 1630079164692,
  "name": "femb",
  "email": "fje@gmail.com",
  "reb": "91823",
  "google_oauth": {
    "id": "",
    "name": "",
    "email": ""
  },
  "facebook_oauth": {
    "id": 0,
    "name": "",
    "email": ""
  },
  "linkedin_oauth": {
    "id": "",
    "name": "",
    "email": ""
  },
  "github_oauth": {
    "id": "",
    "name": "",
    "email": ""
  }
};

I would like to use all the "values" from the array and construct a new object using the values as the first parameter, and "key" of that arr to find the entries in the "obj" object. Example what I would like to get:

{
  text_1: 15
  text_2: 1630079164692
  text_3: "femb"
  text_4: "fje@gmail.com"
}

I'm trying to do this using .map

var combined = Object.fromEntries(arr.map(item => [item.value, obj.[this.key]]));

But I'm having SyntaxError, not sure how I should access to the obj entries using the arr key.

Any ideas?

EDIT: I think I have found the way:

var combined = Object.fromEntries(arr.map(item => [item.value, obj[item.key]]));

Can someone tell me if It's a good way to access? Or there's better?

Thanks.

Seju4ni
  • 13
  • 4

0 Answers0