-4

How i get below output in javascript

var keys = ["1","2","3"];
var values = ["one", "two","three"];


var final = {
    "1": "one",
    "2": "two",
    "3": "three"
}
2
  • What part are you having trouble with? Can you show your code so far? Commented May 1, 2015 at 13:10
  • Question shows a lack of research or code attempt to resolve issue. You are expected to have at least tried something and if you did post the code that wasn't working Commented May 1, 2015 at 13:32

2 Answers 2

3

Loop over one of the arrays and use the index to access the other, save in an object

var result = {};

keys.forEach(function(item, index) {
   result[item] = values[index]; 
});
Sign up to request clarification or add additional context in comments.

Comments

1

A very simple loop should suffice, copying the values & keys at each index into a new object:

var finalOutput = {};
for(var i=0, j=keys.length; i<j; i++) {
    finalOutput[keys[i]] = values[i];
}

Working Example

Note: You shouldn't use final as a variable name as it is a reserved future keyword

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.