简体   繁体   中英

turn 2 array into an array object?

 var a = [ 'Child' , 'Adult']; var b = [2,6]; var c = {} for(var i=0; i<a.length; i++){ c[a[i]] = b[i] } document.getElementById('result').innerHTML = JSON.stringify(c, 0, 4); 
 <pre id="result"></pre> 

Above loop produced

{
    "Child": 2,
    "Adult": 6
}

but how to produce the resutl like this

[{"child":2},{"Adult":6}]

which later on is easy for me to loop through.

var a = [ 'Child' , 'Adult'];
var b = [2,6];
var c = []

for(var i=0; i<a.length; i++){
                var o = {}
                o[a[i]] = b[i];
                c.push(o)
            }

if you use a functional library like Ramda, then you can do :-

R.zipWith(R.createMapEntry,a,b);

Simple and procedural.

Obviously c should be an array, judging by the output you want. So in the loop you just have to create an object with the right key and value and add that to the array c.

 var a = ['Child', 'Adult']; var b = [2,6]; var c = []; for (var i = 0 ; i < a.length ; i++) { var key = a[i]; var val = b[i]; var obj = {}; obj[key] = val; c[i] = obj; } document.getElementById('result').innerHTML = JSON.stringify(c, 0, 4); 
 <pre id="result"></pre> 

More functional with map.

As mentioned in other answers: There are more functional programming oriented ways of doing this. The above code is as simple as possible in order to help you learn.

You could also do something like:

 var a = ['Child', 'Adult']; var b = [2,6]; c = a.map(function(key,i) { var obj = {}; obj[key] = b[i]; return obj; }); document.getElementById('result').innerHTML = JSON.stringify(c, 0, 4); 
 <pre id="result"></pre> 

Considering you have to access array b with an index anyways I wouldn't say this way is any better.

Even more functional with zip and map.

A more functional way would be to combine a and b using a zip function and then map that to a new array.

 var a = ['Child', 'Adult']; var b = [2,6]; function zip(arrays) { return arrays[0].map(function(_,i){ return arrays.map(function(array){return array[i]}) }); } var c = zip([a,b]).map(function(obj) { var result = {}; result[obj[0]] = obj[1]; return result; }); document.getElementById('result').innerHTML = JSON.stringify(c, 0, 4); 
 <pre id="result"></pre> 

You are setting var c as an object, but expecting it to be an array. Try:

var a = [ 'Child' , 'Adult'];
var b = [2,6];
var c = []

for(var i=0; i<a.length; i++){

    var temp = {};
    temp[a[i]] = b[i];

    c.push( temp );
}

Something like this should do.

var a = [ 'Child' , 'Adult'];
var b = [2,6];
var c = [];

for(var i=0; i<a.length; i++){
  var obj = {}; // create a temp object
  obj[a[i]] = b[i]; // fill the values 
  c.push(obj); // push it to c
}

 var a = [ 'Child' , 'Adult']; var b = [2,6]; var c = []; for(var i=0; i<a.length; i++){ c[i] = {}; c[i][a[i]] = b[i]; } document.getElementById('result').innerHTML = JSON.stringify(c, 0, 4); 
 <pre id="result"></pre> 

Try using Array.prototype.map() , moving c into callback to create object for each item in a

 var a = ["Child", "Adult"]; var b = [2, 6]; var arr = a.map(function(val,key) { var c = {}; c[val] = b[key]; return c }); document.getElementById('result').innerHTML = JSON.stringify(arr, 0, 4); 
 <pre id="result"></pre> 

in firefox (and ES6 or chrome with experimental JS flag in about:flags set), you can do this neat little trick:

var a = [ 'Child' , 'Adult'];
var b = [2,6];

a.map(  (a,i)=> a={[a]: b[i]}  ); // == [{"Child":2},{"Adult":6}]

(sadly the a= part is needed to make it not look like a function body, but it's harmless anyway...

if you don't mind destroying the array, this is slightly cleaner reading:

a.map( a => a={[a]: b.shift()} );

I think it's neat than plain vanilla can do this task with fewer chars of code than something like Ramda...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM