0

So I have a list of objects like

 var options =   
 [{"car" : "red"},{"house": "green"},{"dog":"bark"}]

That I am trying to turn into an array of values from that, so it would look like this:

["red","green","bark"]

Here is my (non-working) attempt

newUrl = _.each(options, function(obj){
                    return _.values(obj);
                });
                console.log(newUrl);

So iterate through each object and fetch the value. The iteration works, the .values does not seem to be working how I though it would though.

1
  • each does not return anything. perhaps you need .reduce Commented Mar 11, 2015 at 14:00

2 Answers 2

3

Without underscore, you can do:

var colors = options.map(function(item) { 
    return item[Object.keys(item)[0]] 
});
Sign up to request clarification or add additional context in comments.

Comments

3

You don't need underscore for this, just Array.prototype.map();

var newUrl = options.map(function (item) {
    return item[Object.keys(item)[0]];
});

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.