1

How can I convert two arrays to the object?

My arrays:

arr1.push(3,2,3,6,11,7);        
arr2.push("num1","num2","num3","num4","num5","num6");

How to convert it to the object:

[{name:"num1", value:3}, {name:"num2", value:2}, {name:"num3", value:3}, {name:"num4", value:6}, {name:"num5", value:11}, {name:"num6", value:7}]

3 Answers 3

2

you can try

 var obj = [];

 for(var i = 0; i < arr1.length; i++){
     obj[i] = {name : arr1[i], value: arr2[i]};
 }
Sign up to request clarification or add additional context in comments.

Comments

0
var arr = [],
    cv  = -1;

while(++cv < arr1.length) {
  arr.push({name:arr2[cv], value:arr1[cv]});
}

Comments

0

You basically has to merge both arrays.Assuming both arrays will always has equal length

arr1.push(3,2,3,6,11,7);        
arr2.push("num1","num2","num3","num4","num5","num6");

var obj  = [], i;
for( i=0; i<l; i++) {
    obj.push(arr1[i]+":"+arr2[i]);
}
// obj is now required object

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.