I want to do the following:
// an object
var object = {
one: null,
two: null,
three: null
};
// an array
var array = ['this is one', 'this is two', 'this is three'];
I now want to merge them both together so I get;
var merged = {
one: 'this is one',
two: 'this is two',
three: 'this is three'
};
I don't want to use any 3rd library just pure javascript (ECMA5).
So what is the trick?
Regards, bodo
Try this:
// an object
var object = {
one: null,
two: null,
three: null
};
// an array
var array = ['this is one', 'this is two', 'this is three'];
function merge(arraysrc, array2dest) {
var x, i = 0;
var merged = [];
for (x in array2dest) {
var obj = {};
obj[x] = arraysrc[i++];
merged.push(obj);
}
return merged;
}
var a = merge(array, object);
alert(JSON.stringify(a));
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.