1

I have an object with key and an array values like this:

var obj = {
   key1: ['value1','value2','value3'],
   key2: ['value1','value2','value3'],
   key3: ['value1','value2','value3'],
   key4: ['value1','value2','value3']
};

What I want is to fetch every key's arrays.

How can I do this?

I wrote a function using Object.keys but it is only fetching the keys of the object:

Object.keys(obj).forEach(function (key) {

   console.log(key);
});
3
  • what do you want to do with these values later? is it okay to just put all of them in an array? Commented Jan 7, 2016 at 7:08
  • 1
    Try console.log(obj[key]); Commented Jan 7, 2016 at 7:08
  • Thanks, works like a charm to me :) Commented Jan 7, 2016 at 7:10

6 Answers 6

2

Use obj[key] to access the value of the key key in the Object obj.

Object.keys(obj).forEach(function (key) {
   console.log(obj[key]);  // Will be an array
});

Better iterator would be for...in

for (key in obj) {
  console.log(obj[key]);
}
Sign up to request clarification or add additional context in comments.

3 Comments

May I ask why for .. in is better than the other one?
Because in the earlier on You are calculating the keys using Object.keys(obj) and then iterating using forEach which has it's own limitation as you can not break it. Instead in the later on you are just trying to access whatever comes in. And it looks neat too.
you are comparing Object.keys vs for ... in and the breaking of the loop. Object.keys have no loop, but the other array methods. Array.prototype.every has break and Array.prototype.some has break. so why not combine one of these two with the wanted task?
2

You can loop through the items in the object using:

for (item in obj) {
  console.log(obj[item]);
}

obj[item] will give you each array.

Using your existing solution you need to change key to obj[key].

Comments

2

This worked..

var obj = {
   key1: ['value1','value2','value3'],
   key2: ['value1','value2','value3'],
   key3: ['value1','value2','value3'],
   key4: ['value1','value2','value3']
};
Object.keys(obj).forEach(function (key) {
   console.log(obj[key]);
});

See Fiddle : https://jsfiddle.net/5t4c4sg8/

Comments

0

Not sure what you want to do with values later, but if you just want to put all of them in an array then simply do

var allValues = [];
Object.keys(obj).forEach(function (key) {

   allValues.concat(obj[key]);

});

Comments

0

This will get all key values & then you can push that array into specific array

for(var key in obj)
    console.log(obj[key])

Comments

0

Just an example how to use Object.keys() in combination with Array.prototype.some()

The some() method tests whether some element in the array passes the test implemented by the provided function.

for a short ciruit if the callback returns true. It stops the iteration.

var obj = {
    key1: ['value1', 'value2', 'value3'],
    key2: ['value1', 'value2', 'value3'],
    key3: ['value1', 42, 'value3'],
    key4: ['value1', 'value2', 'value3']
};

Object.keys(obj).some(function (k) {
    document.write('k: ' + k + '<br>');
    if (~obj[k].indexOf(42)) {
        document.write('Found the answer in ' + k);
        return true;
    }
});

Comments

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.