0

So I have an object (EDIT: originally called it a "hash") like

newHash = 
{item1: "spam",
item2: "everything"
}

and I want to return just "spam" and "everything".

It seems it doesn't work to write a for loop like this:

for (var x in newHash) {return newHash[x].value}

Thanks!

EDIT : replaced the word "hash" in question with "Object" as per popular convention.

2
  • 4
    "Return" in what format? You can't return multiple values (coming soon to ES6 via destructuring, or perhaps iterators in your case), so what are you looking for? An array of values? Commented Oct 8, 2013 at 10:10
  • 1
    You shouldnt need the .value part in js, but you presumably will just get the value for the first item in that object, i.e. "spam". youll want to put them in an array or something then return the lot Commented Oct 8, 2013 at 10:11

7 Answers 7

2

Try something like this:

function getObjectValues(obj) {
    var i, ret = [];
    for( i in obj) if( obj.hasOwnProperty(i)) ret.push(obj[i]);
    return ret;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Regarding the title of your question, I guess you want to do this :

var newHash = {
    item1: "spam",
    item2: "everything"
};
newHash.item1; // "spam"
newHash['item1']; // "spam"
newHash.item2; // "everything"
newHash['item2']; // "everything"

Then, if you want to get all the values, you'll need to store them into a list :

var list = [];
for (var key in newHash) {
    list.push(newHash[key]);
}
list; // ["spam", "everything"]
list.join(); // "spam,everything"

Since your keys are based on the same pattern, you could also do it like so :

var list = [];
for (var i = 1; i <= 2; i++) {
    list.push(newHash['item' + i]);
}

Comments

1

If you don't mind using Underscore (which is a great library!), you can do this:

newHash = {
    item1: "spam",
    item2: "everything"
}

_.values(newHash)

returns:

["spam", "everything"]

Comments

0

You can do the following:

return Object.keys(newHash).map(function (key) { return newHash[key]; });

,which returns an array with the values in the object.

It works like this: you get an array of keys, then use the map function to get a corresponding array with the value for each key.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

Comments

0

Your object declaration has a " character to much.

newHash = {
  item1: "spam",
  item2: "everything"
}

for( var x in newHash )
 console.log(newHash[x]);

http://jsfiddle.net/upHyu/

2 Comments

The " character is most likely just a typo in the question. This doesn't answer the part about "returning" the values.
True - I guess I lost focus on that he said the loop did not work.
0

(function(o){var res=[],i;for(i in o){res.push(o[i]);}return res;})(newHash);

Comments

0

You could always add a values function to the object prototype along the same lines as Object.keys().

if (!('values' in Object.prototype)) {
  Object.prototype.values = function (obj) {
    return Object.keys(obj).map(function (p) { return obj[p]; });
  }
}

var obj = {
  '1': 'one',
  '2': 'two',
  '3': 'three'
}

console.log(obj.values()); // ["one", "two", "three"]

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.