JS Concatenate array values into one value
5 answers
So here
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);
+7
source to share
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);
+1
source to share