Can I use LoDash to map a large json object (from a REST service) to a smaller object?

I am getting a list of tweets (from a REST service) which is a really big list of many properties, but I am only interested in a few properties in each item of the returned collection.

What's the best way to map this to a smaller object? Will Lodash help here?

Will I just iterate over it and create a lot of new object?

+3


source to share


2 answers


Assuming you've already created an array of large tweet objects from your JSON (String), there is actually an underscore / lodash function to create objects with subsets of properties. Thank you after _. Select .

Basically, you would do something along the lines of:



var importantParams = ["id", "user", "text", "created_at"];

var smallerObjArray = _.map(largerObjArray, function(largeObj){
    return _.pick(largeObj, importantParams);
});

      

+3


source


Definitely lodash can really be helpful when looking up a property in a large json. It has many useful methods for managing collections. pluck is my favorite.



Iterating over this large object is tedious work. It can be done, but lodash is a simple and smart way :)

0


source







All Articles