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]
remtsoy
  • 465
  • 1
  • 3
  • 15
  • Duplicate of [this](http://stackoverflow.com/questions/17635866/get-values-from-object-in-javascript) and [this](http://stackoverflow.com/questions/6268679/best-way-to-get-the-key-of-a-key-value-javascript-object) and many others. – dramzy Jul 25 '15 at 18:12

9 Answers9

14

Here is the simplest way:-

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

for more please go Standard built-in objects

jesusverma
  • 1,625
  • 1
  • 16
  • 17
8

The simplest & most elegant way to do this:

var arr1 = Object.keys(obj);
var arr2 = arr1.map(function (k) {
    return obj[k];
});
deleted user
  • 824
  • 1
  • 7
  • 11
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]);
}
bhspencer
  • 13,086
  • 5
  • 35
  • 44
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);
Farkhat Mikhalko
  • 3,565
  • 3
  • 23
  • 37
  • I don't think hasOwnProperty check is necessary when using an object literal http://stackoverflow.com/questions/11313982/should-hasownproperty-still-be-used-with-for-in-statements – bhspencer Jul 25 '15 at 18:13
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]
dotnetom
  • 24,551
  • 9
  • 51
  • 54
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 ]
dakab
  • 5,379
  • 9
  • 43
  • 67
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]);
}
Andreas Grünh
  • 336
  • 1
  • 6
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:

M Stahl
  • 11
  • 1
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 ]
karel
  • 5,489
  • 46
  • 45
  • 50
Anil Koppula
  • 743
  • 6
  • 11