remtsoy
remtsoy

Reputation: 465

Split object into 2 arrays javascript

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]

Upvotes: 1

Views: 30572

Answers (9)

Anil Koppula
Anil Koppula

Reputation: 753

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 ]

Upvotes: 1

M Stahl
M Stahl

Reputation: 11

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:

Upvotes: 1

jesusverma
jesusverma

Reputation: 1745

Here is the simplest way:-

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

for more please go Standard built-in objects

Upvotes: 14

Andreas Grünh
Andreas Grünh

Reputation: 336

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

Upvotes: 1

deleted user
deleted user

Reputation: 832

The simplest & most elegant way to do this:

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

Upvotes: 8

dakab
dakab

Reputation: 5915

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 ]

Upvotes: 2

krivtom
krivtom

Reputation: 24916

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]

Upvotes: 2

bhspencer
bhspencer

Reputation: 13600

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

Upvotes: 4

Farkhat Mikhalko
Farkhat Mikhalko

Reputation: 3665

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

Upvotes: 3

Related Questions