1

I've got an object like that

 obj =   {
    'baz':1,
    'foo':7,
    'zap':12,
    'qax':15
    }

I need to split it into 2 arrays

arr1 = ['baz', 'foo', 'zap', 'qax']
arr2 = [1, 7, 12, 15]
1
  • Duplicate of this and this and many others. Commented Jul 25, 2015 at 18:12

9 Answers 9

14

Here is the simplest way:-

var arr1 = Object.keys(obj);
var arr2 = Object.values(obj);

for more please go Standard built-in objects

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

1 Comment

Should be noted that this method is not supported in some browsers: Support
8

The simplest & most elegant way to do this:

var arr1 = Object.keys(obj);
var arr2 = arr1.map(function (k) {
    return obj[k];
});

2 Comments

don't you think elegant is a matter of opinion?
@bhspencer: perhaps, but by elegant I am referring to the shortness of it.
4
 obj =   {
    'baz':1,
    'foo':7,
    'zap':12,
    'qax':15
    }

var arr1 = [];
var arr2 = [];

for (var prop in obj) {
   arr1.push(prop);
   arr2.push(obj[prop]);
}

2 Comments

It might not work as expected, since looping order of object keys is implementation dependent
True but the index of the key and the index of the value will match in the output arrays.
3

The simplest way - iterate over object properties(key, value)

obj =   {
    'baz':1,
    'foo':7,
    'zap':12,
    'qax':15
    }

var a = [];
var b = [];
for(var i in obj){
  if(obj.hasOwnProperty(i)){
    a.push(i);
    b.push(obj[i]);
  }
}

console.log(a);
console.log(b);

1 Comment

I don't think hasOwnProperty check is necessary when using an object literal stackoverflow.com/questions/11313982/…
2

You can loop over properties and add properties to arr1 and values to arr2. You can use this code to achieve that:

var obj = { 'baz':1, 'foo':7, 'zap':12, 'qax':15 };
var arr1 = [];
var arr2 = [];
for(var propertyName in obj) {    // loop through properties
    arr1.push(propertyName);
    arr2.push(obj[propertyName]);
}
console.log(arr1);   // prints ["baz", "foo", "zap", "qax"]
console.log(arr2);   // prints [1, 7, 12, 15]

Comments

2

Simple:

var obj = { 'baz':1, 'foo':7, 'zap':12, 'qax':15 };
var arr1 = [], arr2 = [];
for(var prop in obj){
    arr1.push(prop);
    arr2.push(obj[prop]);
}
console.log( arr1 ); // === [ "baz", "foo", "zap", "qax" ]
console.log( arr2 ); // === [ 1, 7, 12, 15 ]

Comments

1

You have to iterate over every attribute of your object:

var obj =   {
  'baz':1,
  'foo':7,
  'zap':12,
  'qax':15
}
var keys = [];
var values = [];

for(var x in obj) {
  keys.push(x);
  values.push(obj[x]);
}

Comments

1
obj =   {
  'baz':1,
  'foo':7,
}

const [vars, vals] = Object.keys(obj).reduce(([a, b], k) => {
   a.push(k)
   b.push(options[k])
   return [a, b]
}, [[], []])

vars //-> ['bar', 'foo']
vals //-> [1, 7] 

Requires availability of:

Comments

1

Use Object.getOwnPropertyNames(obj) to get the Keys of obj, and for values I'm using the for loop.

JavaScript:

var obj =  {
    'baz':1,
    'foo':7,
    'zap':12,
    'qax':15
    }
var j=0, val=[];
    for(let x in obj) {
        (val[j++] = obj[x]);  //storing the value of obj in val
    }

console.log(Object.getOwnPropertyNames(obj))
console.log(val)

Output:

[ 'baz', 'foo', 'zap', 'qax' ]
[ 1, 7, 12, 15 ]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.