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);
Try this
var a = {
key: 'animals',
options: ['dog', 'cat', 'penguin']
}
var result = Object.keys(a).map(function(key){
var item = a[key];
return item instanceof Array ? item.join('') : item;
});
console.log(result);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With