-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.

1

2 Answers 2

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);

Sign up to request clarification or add additional context in comments.

Comments

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.