使用lodash方法返回新形式的数组

时间:2018-09-20 17:35:38

标签: javascript arrays json object lodash

我的第一个数组:

[
  {
    "id": 1,
    "name": "Gilman",
    "entryCheckPointId": 1,
    "entryCheckPoint": {
      "id": 1,
      "name": "Checkpoint A"
    }
  },
  {
    "id": 2,
    "name": "Arshad",
    "entryCheckPointId": 3,
    "entryCheckPoint": {
      "id": 3,
      "name": "Checkpoint C"
    }
  },
  {
    "id": 3,
    "name": "Illyas",
    "entryCheckPointId": 3,
    "entryCheckPoint": {
      "id": 3,
      "name": "Checkpoint C"
    }
  }
]

第二个数组:

[ 
  { 
    "entryCheckPointId": 1, 
    "count": 1
  },
  { 
    "entryCheckPointId": 3, 
    "count": 2 
  } 
]

我想使用entryCheckPointId匹配它们,并且我想返回这样的新数组形式

预期输出:

[
  {
    label: 'Checkpoint A', 
    value: 1, // count 
    color: 'green'
  }, 
  {
    label: 'Checkpoint C', 
    value: 2, // count
    color: 'pink'
  }, 
]

我尝试过使用地图功能,但无法正常运行。

我尝试了以下代码:

var mergedList = _.map(a1, function(item){
    return _.extend(item, _.findWhere(a2, { id: item.id }));
}); // a1 is firstArray and a2 is secondArray

是否可以使用任何lodash库?

还有一个疑问,lodash和下划线是否都相同?

2 个答案:

答案 0 :(得分:1)

使用纯Javascript(我想这不是您想要的,但可能会有所帮助):

var x = [{"id": 1,"name": "Gilman","entryCheckPointId": 1,"entryCheckPoint": {"id": 1,"name": "Checkpoint A"}},{"id": 2,"name": "Arshad","entryCheckPointId": 3,"entryCheckPoint": {"id": 3,"name": "Checkpoint C"}},{"id": 3,"name": "Illyas","entryCheckPointId": 3,"entryCheckPoint": {"id": 3,"name": "Checkpoint C"}}]

var y = [ { "entryCheckPointId": 1, "count": 1},{ "entryCheckPointId": 3, "count": 2 }]

var colors  = ["pink", "red", "green"]

var result = y.map((a,i)=>({
  label: x.filter(b=>b.entryCheckPointId == a.entryCheckPointId)[a.count-1].entryCheckPoint.name,
  value: a.count, 
  color: colors[i]
}))

console.log(result)

答案 1 :(得分:1)

请找到两个使用lodash和不使用var data1 = [{ "id": 1, "name": "Gilman", "entryCheckPointId": 1, "entryCheckPoint": { "id": 1, "name": "Checkpoint A" }}, { "id": 2, "name": "Arshad", "entryCheckPointId": 3, "entryCheckPoint": { "id": 3, "name": "Checkpoint C" }}, { "id": 3, "name": "Illyas", "entryCheckPointId": 3, "entryCheckPoint": { "id": 3, "name": "Checkpoint C" }}] var data2 = [ { "entryCheckPointId": 1, "count": 1 }, { "entryCheckPointId": 3, "count": 2 } ] var colors = ['red', 'green', 'blue'] var lodashResult = _.map(data2, ({entryCheckPointId, count}, i) => ({ label: _.find(data1, {entryCheckPointId}).entryCheckPoint.name, count, color: colors[i] })) var noLodashResult = data2.map(({entryCheckPointId, count}, i) => ({ label: data1.find(x => x.entryCheckPointId == entryCheckPointId).entryCheckPoint.name, count, color: colors[i] })) console.log('lodash', lodashResult) console.log('noLodash', noLodashResult)的示例。这两个示例都利用了ES6的一些新功能(如速记对象文字,对象解构等)。

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>
_.lodash

如您所见,def sid_assign(x): if x >= 7: return 3 if x >= 6 | x < 7: return 2 else: return 1 id_sums = df.groupby('consumer_id').order_total.transform('mean') df['SID'] = id_sums.apply(sid_assign) print(df) consumer_id order_total SID 0 1 5 1 1 2 6 2 2 3 7 3 3 1 5 1 更加简单和简短。