4

I have this object

a = {key:'animals',
     options: ['dog','cat','penguin']}

How can I simplify it to this:

b = ['animals','dogcatpenguin']
5
  • does your keys are consistent ? Commented Apr 29, 2015 at 15:13
  • Iterate over the properties and push their values to a new array. Note however that browser may iterate over properties in different orders. If you always have these properties you can just access them directly. What have you tried so far? Where are you stuck? Commented Apr 29, 2015 at 15:13
  • 2
    You can try var b = []; b.push(a.key); b.push(a.options.join('')); Commented Apr 29, 2015 at 15:14
  • 1
    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 Commented Apr 29, 2015 at 15:22
  • I am using this solution for an Angular project and first answer works perfectly. Thank you all:) Commented Apr 30, 2015 at 7:28

6 Answers 6

9

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);

Sign up to request clarification or add additional context in comments.

Comments

2

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);

Comments

1

You can just create a new array with your computed values

var a = {key:'animals',
     options: ['dog','cat','penguin']};

var b = [
  a.key,
  a.options.join('')
];

document.write(JSON.stringify(b));

Comments

1
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);

Comments

1

Another possible solution.

a = {key:'animals',options: ['dog','cat','penguin']}

var b = new Array();
for(var index in a) { 
    var attr = a[index].toString().split(",").join("");
    b.push(attr);
}

Comments

1
b = [a.key,Object.values(a)[1].reduce((a,b)=> a+b)]

1 Comment

While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.