0

_lodash

Here is what my tickerTagObject array looks like:

It's an array of objects, each object contains another object + 1 array

enter image description here

I'm able to grab the tickers using this syntax below:

var tickerObjs = _(tickerTagObjs)
    .filter(function(tiks) { return tiks; })
    .pluck('ticker')
    .value();

console.log(tickerObjs);

^ This gives me an array of the ticker objects

However using the same for the tags doesn't work, perhaps because the tags are an Array not an Object?

var tagObjs = _(tickerTagObjs.tags)
    .filter(function(tags) { return tags; })
    .pluck('tags')
    .value();

console.log(tagObjs);

enter image description here

How would you use lodash to "pluck" the tag objects inside the array tags inside each TagsObject?

1 Answer 1

2

That's because in the second snippet you are passing direct, undefined tags property of array to the lodash constructor and subsequent operations effectively do not do anything.

Also note that filter calls in your snippets are redundant and you can simply remove them.

You could also use the native Array map method:

var tags = tickerTagObjs.map(function(el) { return el.tags; });
Sign up to request clarification or add additional context in comments.

6 Comments

Ah thanks that was it! Hmm I'm getting tags now, but it's inside an Array which is inside an Array... oh well on to the next prob! 9 more mins :D
@LeonGaban You are welcome! Yes, it should return an array of arrays.
Is there a way to easily avoid that? I mean I can just do tags[0] but rather it just be tags
@LeonGaban Well, no there isn't. There are several values and that's why array exists. If you need the first value of the array you can use lodash first method.
@LeonGaban Yeah, Lodash is awesome because JavaScript is awesome!
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.