2

I want to merge values of multiple arrays in one object to one array like this:

[1,2,3]
[4,5,6]

To:

[
    {name: 1, value: 4}, 
    {name: 2, value: 5}, 
    {name: 3, value: 6}
]
eisbehr
  • 12,243
  • 7
  • 38
  • 63

5 Answers5

0

Use Array#map method to create a new array based on existing.

var a = [1, 2, 3],
  b = [4, 5, 6];

// iterate over array `a` to genearate object array
var res = a.map(function(v, i) {
  // generate object ( result array element )
  return {
    name: v, // name from array `a`
    value: b[i] // value from array `b` get using index
  };
})

console.log(res);
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
0

You can use a simple for loop on one array and create your result however you like.

var names  = [1, 2, 3],
    values = [4, 5, 6],
    result = [];

for( var i = 0; i < names.length; i++ ) {
    result.push({
        name: names[i],
        value: values[i]
    });
}

console.log(result);
eisbehr
  • 12,243
  • 7
  • 38
  • 63
0
    var names = [1,2,3];
    var values =  [4,5,6];
    var result =[];

    for(var i=0;i<names.length;i++){
     var obj= {
       "name":names[i],
       "value": values[i]
     }
     result.push(obj);
    }
    console.log(result);
Tuhin
  • 3,335
  • 2
  • 16
  • 27
  • I think it is not a good idea to loop an array this way! Whenever using `for ... in` you should really keep this in mind: http://stackoverflow.com/questions/500504/why-is-using-for-in-with-array-iteration-a-bad-idea – eisbehr Aug 05 '16 at 07:40
  • When you have it in mind now, you should possible delete this answer, because it is generally *bad practice* and should be avoid anywhere. ;) – eisbehr Aug 05 '16 at 07:45
0

arr1 = [1,2,3];
arr2 = [4,5,6];

//prepare array to fill
obj = [];

// for every item we merge them into an object and pass them into the newObj
function merge(item, index, arr2, newObj){
  temp = {};
  temp[item] = arr2[index];
  newObj.push(temp);
}

//give the item as key, the index for the second array, and the object we want to fill
arr1.forEach((item, index) => merge(item, index, arr2, obj));

console.log(obj);
Randy
  • 9,419
  • 5
  • 39
  • 56
0

OR in PHP :)

<?php
    error_reporting(E_ALL);
    ini_set("display_errors", 1);

    $names = array('Chevalier','Sorcier','Archer');
    $values = array('5','6','8');
    $fusion = array();

    for($i=0; $i<count($names); $i++){
        $fusion[$names[$i]] = $values[$i];
    }

    echo '<pre>';
    print_r($names);
    print_r($values);
    print_r($fusion);
    echo '</pre>';
?>
Kingroys
  • 7
  • 1
  • While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, as this reduces the readability of both the code and the explanations! – Blue Aug 05 '16 at 13:56