Lodash select object fields from an array

I have an array of objects:

var results= [ { "_type": "MyType", "_id": "57623535a44b8f1417740a13", "_source": { "info": { "year": 2010, "number": "string", }, "type": "stolen", "date": "2016-06-16T00:00:00", "createdBy": "57469f3c71c8bf2479d225a6" } } ]; 

I need to select specific fields from an array. As a result, I want to get the following:

 [ { "_id": "57623535a44b8f1417740a13", "info": { "year": 2010, "number": "string" }, "type": "stolen", "date": "2016-06-16T00:00:00", "createdBy": "57469f3c71c8bf2479d225a6" } ] 

As you can see, I want to select the _id field and the contents of the _source object. How can I do this with lodash?

I found the .map function, but does not accept an array of keys: var res = _.map(results, "_source");

+5
source share
4 answers

You can do:

 var mapped = _.map(results, _.partialRight(_.pick, ['_id', 'info', 'type', 'date', 'createdBy'])); 

A little explanation:

  • _. map : expects a function that takes each element from the collection so that you can map it to something else.
  • _. partialRight : accepts a function that will be called later with the addition of its arguments to the end
  • _. pick : Gets the path specified from the object.
+12
source

In simple Javascript, you can iterate through an Array#map and assemble a new object for each object without distorting the original object.

 var results = [{ "_type": "MyType", "_id": "57623535a44b8f1417740a13", "_source": { "info": { "year": 2010, "number": "string", }, "type": "stolen", "date": "2016-06-16T00:00:00", "createdBy": "57469f3c71c8bf2479d225a6" } }], res = results.map(function (a) { var o = { _id: a._id }; ["info", "type", "date", "createdBy"].forEach(function (k) { o[k] = a._source[k]; }); return o; }); console.log(res); 
+4
source

 var results = [{ _type: "MyType", _id: "57623535a44b8f1417740a13", _source: { info: { year: 2010, number: "string", }, type: "stolen", date: "2016-06-16T00:00:00", createdBy: "57469f3c71c8bf2479d225a6" } }]; var rootProperty = ['_id'] var innerProperty = '_source' var myArray = _.map(results, result => _(result) .pick(rootProperty) .assign(_.result(result, innerProperty)) .value() ) console.log(myArray) 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script> 
+1
source

You can map () the result and assign each element () the value of the _id key in the toegether object with the _source object.

 results = _.map(results, item => _.assign( { _id: item._id }, item._source )); 

 var results = [{ "_type": "MyType", "_id": "57623535a44b8f1417740a13", "_source": { "info": { "year": 2010, "number": "string", }, "type": "stolen", "date": "2016-06-16T00:00:00", "createdBy": "57469f3c71c8bf2479d225a6" } }]; results = _.map(results, item => _.assign( { _id: item._id }, item._source )); document.write('<pre>' + JSON.stringify(results, 0, 4) + '</pre>'); 
 <script src="https://cdn.jsdelivr.net/lodash/4.13.1/lodash.min.js"></script> 

You can also write this in plain JS:

 result = results.map(item => Object.assign( { _id: item._id }, item._source )); 

 var results = [{ "_type": "MyType", "_id": "57623535a44b8f1417740a13", "_source": { "info": { "year": 2010, "number": "string", }, "type": "stolen", "date": "2016-06-16T00:00:00", "createdBy": "57469f3c71c8bf2479d225a6" } }]; result = results.map(item => Object.assign( { _id: item._id }, item._source )); document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>'); 
0
source

All Articles