I have this object
a = {key:'animals',
options: ['dog','cat','penguin']}
How can I simplify it to this:
b = ['animals','dogcatpenguin']
Like so
var a = {
key: 'animals',
options: ['dog','cat','penguin']
};
var key, b = [];
for (key in a) {
b.push(Array.isArray(a[key]) ? a[key].join('') : a[key]);
}
console.log(b);
Or you can use Object.keys with .map
var a = {
key: 'animals',
options: ['dog','cat','penguin']
};
var b = Object.keys(a).map(function (key) {
return Array.isArray(a[key]) ? a[key].join('') : a[key];
});
console.log(b);
var a = {
key: 'animals',
options: ['dog','cat','penguin']
};
var idx, b = [];
for (idx in a) { //Iterate through all the items of object a
b.push(Array.isArray(a[key]) ? a[key].join('') : a[key]); //Check if item is array join with (blank) or if not directly push it new array
}
console.log(b);
b = [a.key,Object.values(a)[1].reduce((a,b)=> a+b)]
var b = []; b.push(a.key); b.push(a.options.join(''));var b = new Array(a.key, a.options.join(""));- It's important to note what @FelixKling says if you intend to iterate, then order of an object's keys is never guaranteed