lodash从数组中选择对象字段

时间:2016-06-17 09:24:44

标签: javascript lodash

我有一些对象:

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

我需要从数组中选择特定字段。结果,我希望得到以下内容:

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

如您所见,我想选择_id字段和_source对象的内容。我怎么能用lodash做到这一点?

我找到了.map函数,但它没有使用键数组: var res = _.map(results, "_source");

5 个答案:

答案 0 :(得分:32)

你可以这样做:

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

一点解释:

  1. _.map():预计会从集合中获取每个项目的函数,以便您可以将其映射到其他项目。
  2. _.partialRight():采用一个函数,稍后将调用其参数附加到结尾
  3. _.pick():获取从对象指定的路径。

答案 1 :(得分:4)

在普通的Javascript中,您可以使用Array#map进行迭代,并为每个对象组装一个新对象,而不会破坏原始对象。

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);

答案 2 :(得分:1)

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>

答案 3 :(得分:1)

我有相同的要求,以下解决方案最适合我。

let users = [
{
  "_id": "5ead7783ed74d152f86de7b0",
  "first_name": "User First name 1",
  "last_name": "User Last name 1",
  "email": "user1@example.com",
  "phone": 9587788888
},
{
  "_id": "5ead7b780d4bc43fd0ef92e7",
  "first_name": "User FIRST name 1",
  "last_name": "User LAST name 1",
  "email": "user2@example.com",
  "phone": 9587788888
}
 ];

users = users.map(user => _.pick(user,['_id','first_name']))

console.log(users)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

答案 4 :(得分:0)

您可以map()结果,并将对象assign()中的_id键值与_source对象一起保存。

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

&#13;
&#13;
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>');
&#13;
<script src="https://cdn.jsdelivr.net/lodash/4.13.1/lodash.min.js"></script>
&#13;
&#13;
&#13;

你也可以选择用简单的JS来写这个:

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

&#13;
&#13;
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>');
&#13;
&#13;
&#13;