-1

key details: ["100", "101", "102", "103"] member details : ["CHRIS", "ALYSSA", "DIMPLE", "MEETA"]

How can I create a key value pair dynamically, so that key would be member details and corresponding value will be Key details? Size and value of array is also dynamic, getting through some API.

Rajlaxmi
  • 1
  • 4
  • Does this answer your question? [Merge two arrays into one javascript object](https://stackoverflow.com/questions/30124979/merge-two-arrays-into-one-javascript-object) – Heretic Monkey Oct 09 '20 at 13:04

2 Answers2

1

you can use Array.reduce to accomplish it:

const values = ["100", "101", "102", "103"];
const keys = ["CHRIS", "ALYSSA", "DIMPLE", "MEETA"];

const result = keys.reduce((carry, key, index) => (carry[key] = values[index], carry), {});

console.log(result);
Mihai Matei
  • 24,166
  • 5
  • 32
  • 50
0

const keys = ["100", "101", "102", "103"];
const members = ["CHRIS", "ALYSSA", "DIMPLE", "MEETA"];


const result = {};
keys.forEach((value, i) => result[members[i]] = value);
console.log(result);
Sarun UK
  • 6,210
  • 7
  • 23
  • 48