I have objects like this:
{ "aa": "11", "bb" : "22", "cc" : "33" }
{ "aa": "text1", "bb" : "text2", "cc" : "text3" }
I need to merge these to become this array
[ ["text1", "11"], ["text2", "22"], ["text3", "33"] ]
Is there an easy way to do this?
thefourtheye
240k53 gold badges466 silver badges505 bronze badges
asked May 2, 2014 at 4:05
4 Answers 4
Here is some JS FP just for fun:
var o1 = { "aa": "11", "bb" : "22", "cc" : "33" },
o2 = { "aa": "text1", "bb" : "text2", "cc" : "text3" };
var values = function(obj) {
return Object.keys(obj).map(function(key) {
return obj[key];
});
};
function zip(arrays) {
return arrays[0].map(function(_,i){
return arrays.map(function(array) {
return array[i];
});
});
}
var zipped = zip([
values(o1),
values(o2)
]);
console.log(zipped);
PS: zip
function implementation borrowed at https://stackoverflow.com/a/10284006/251311
answered May 2, 2014 at 4:12
6 Comments
trashrobber
Thanks for the posting zip implementation, that will be useful anyway. I accepted the other answer though because it is simpler.
Dileep
@zerkms what does the
function(_,i)
means ?zerkms
@Dileep: it's an anonymous function that accepts 2 arguments: 1st is with
_
name, second is with i
. Keeping in mind .map()
passes the value and the key - the former one will be the value, the second is the key. _
is just a "conventional" name for an argument to be discarded - you may have noticed it's not used, but only i
. And as soon as we need i
- we cannot omit the first argument hence need to give it some name. PS: ask if my tangled description is hard to get :-)Dileep
@zerkms Dude you got some supercomputer in your head..!! Took a while understand but the logic is awesome ;-)
zerkms
@Dileep: it's not me - I borrowed it (and put a credit for it) at stackoverflow.com/a/10284006/251311 But it's "simple" to reinvent it anyway isn't it?
|
var objects = [{
"aa": "11",
"bb": "22",
"cc": "33"
}, {
"aa": "text1",
"bb": "text2",
"cc": "text3"
}];
var result = [];
for (var key in objects[1]) {
result.push([objects[1][key], objects[0][key]]);
}
console.log(result);
# [ [ 'text1', '11' ], [ 'text2', '22' ], [ 'text3', '33' ] ]
Or
console.log(Object.keys(objects[1]).map(function(key) {
return [objects[1][key], objects[0][key]];
}));
# [ [ 'text1', '11' ], [ 'text2', '22' ], [ 'text3', '33' ] ]
If you had the objects in two different variables, like this
var o1 = { "aa": "11", "bb" : "22", "cc" : "33" },
o2 = { "aa": "text1", "bb" : "text2", "cc" : "text3" };
then
console.log(Object.keys(o2).map(function(key) {
return [o2[key], o1[key]];
}));
# [ [ 'text1', '11' ], [ 'text2', '22' ], [ 'text3', '33' ] ]
answered May 2, 2014 at 4:11
Comments
An easier way could be :
var a = { "aa": "11", "bb": "22", "cc": "33" };
var b = { "aa": "text1", "bb": "text2", "cc": "text3" };
var c=[];
$.each(a, function (index, value) {
c.push(a[index], b[index]);
});
PS: Using jQuery.
answered May 2, 2014 at 4:18
8 Comments
zerkms
1. what is
$
? 2. {}
doesn't have push
methodzerkms
Still, what the heck is
$
?Avishek
are you using jquery or only javascript?
zerkms
I'm not an OP. And there is no any
jquery
in question tags. If third party libraries were allowed - it would be one line solution: _.zip
(underscorejs.org/#zip)Avishek
$ is the jquery object
|
Folks Try this one. Hope so it will work for you.
var x = { "aa": "11", "bb" : "22", "cc" : "33" }
var y = { "aa": "text1", "bb" : "text2", "cc" : "text3" }
var a = [];
var b = [];
for(var k in x){ a.push(k);}
for(var i = 0; i<a.length;i++){
var c = [];
c.push(x[a[i]]);
c.push(y[a[i]]);
b.push(c);
c = [];
}
console.log(b);
Thanks and regards.
Comments
lang-js
AnArray = [{Objectname: ObjectValue, Objectname2: ObjectValue2},{Objectname: ObjectValue, Objectname2, ObjectValue2}];
Then:AnArray[Index#].Objectname1
(or 2 or whatever you use for Object Name) Do you mean like this example Object Array?