javascript - merge array on keys -


sorry if duplicate, can't find suitable answer. have 2 arrays:

array1 = { '0': 'f1', '1': 'f2'}; array2 = { '0': 's1', '1': 's2'}; 

how combine (merge) these 2 arrays this:

array3 = [['f1', 's1'], ['f2', 's2']]; 

thanks!

if inputs arrays

var = ['f1', 'f2']; var b = ['s1', 's2']; 

the transformation looking zip

var zip = (xs,ys)=>   xs.reduce((zs,x,i)=>     zs.concat([[x, ys[i]]]), []);  zip(a,b); //=> [['f1', 's1'], ['f2', 's2']] 

this assumes both inputs same length.


if inputs objects numerical keys

var = {'0': 'f1', '1': 'f2'}; var b = {'0': 's1', '1': 's2'};  var zipobj = (xs,ys)=>   object.keys(xs).reduce((zs,xk)=>     zs.concat([[xs[xk], ys[xk]]]), []);  zipobj(a,b); //=> [['f1', 's1'], ['f2', 's2']] 

despite not having asked it, oblige trey i've included es5 versions of functions

"use strict";  // zip arrays var zip = (xs,ys)=>   xs.reduce((zs,x,i)=>     zs.concat([[x, ys[i]]]), []);  // zip objects var zipobj = (xs,ys)=>   object.keys(xs).reduce((zs,xk)=>     zs.concat([[xs[xk], ys[xk]]]), []); 

Popular posts from this blog

Apache NiFi ExecuteScript: Groovy script to replace Json values via a mapping file -

node.js - How do I prevent MongoDB replica set from querying the primary? -