根据Javascript中的通用值合并同一数组中的对象

ded*_*les 1 javascript arrays lodash

我有一个数组:

[
  {
    assignmentId:17,
    email:"john.smith@email.com"
    expectation: "Make sure to proofread!",
    firstName:"John"
    id:23
    ignoreForFeedback: true
    lastName:"Smith"
    level:2
    levelFraction:null
    score:35
  },
  {
    assignmentId:17
    countsPerCategory: Array(4)
    email:"john.smith@email.com"
    firstName:"John"
    frequentErrors: Array(5)
    id:23
    ignoreForGrading: true
    lastName:"Smith"
  },
  {
    assignmentId:17,
    email:"cl@email.com"
    expectation: "cite sources",
    firstName:"Cindy"
    id:45
    ignoreForFeedback: true
    lastName:"Lee"
    level:2
    levelFraction:null
    score:32
  },
  {
    assignmentId:17
    countsPerCategory: Array(4)
    email:"cl@email.com"
    firstName:"Cindy"
    frequentErrors: Array(5)
    id:45
    ignoreForGrading: true
    lastName:"Lee"
  }
]
Run Code Online (Sandbox Code Playgroud)

我想将具有相同“ id”的对象组合到数组中的相同对象中。它们的公用密钥也应该组合(例如:“ firstName”,“ email”)。有人可以建议最好的方法吗?使用ES6或Lodash

rye*_*lar 6

您可以使用lodash#groupBy来对数组中的所有项目进行分组id,然后将其lodash#map与以lodash#assign包裹的iteratee 一起使用,lodash#spread以使数组回调作为的参数列表lodash#assgin

var result = _(array)
  .groupBy('id')
  .map(_.spread(_.assign))
  .value();
Run Code Online (Sandbox Code Playgroud)

var result = _(array)
  .groupBy('id')
  .map(_.spread(_.assign))
  .value();
Run Code Online (Sandbox Code Playgroud)
var array = [
  {
    assignmentId:17,
    email:"john.smith@email.com",
    expectation: "Make sure to proofread!",
    firstName:"John",
    id:23,
    ignoreForFeedback: true,
    lastName:"Smith",
    level:2,
    levelFraction:null,
    score:35
  },
  {
    assignmentId:17,
    countsPerCategory: Array(4),
    email:"john.smith@email.com",
    firstName:"John",
    frequentErrors: Array(5),
    id:23,
    ignoreForGrading: true,
    lastName:"Smith"
  },
  {
    assignmentId:17,
    email:"cl@email.com",
    expectation: "cite sources",
    firstName:"Cindy",
    id:45,
    ignoreForFeedback: true,
    lastName:"Lee",
    level:2,
    levelFraction:null,
    score:32
  },
  {
    assignmentId:17,
    countsPerCategory: Array(4),
    email:"cl@email.com",
    firstName:"Cindy",
    frequentErrors: Array(5),
    id:45,
    ignoreForGrading: true,
    lastName:"Lee"
  }
];

var result = _(array)
  .groupBy('id')
  .map(_.spread(_.assign))
  .value();
  
console.log(result);
Run Code Online (Sandbox Code Playgroud)
body > div { min-height: 100%; top: 0; }
Run Code Online (Sandbox Code Playgroud)

这是使用的替代解决方案,Array#filter它利用的第二个参数为Array#filter过滤器的回调函数提供上下文。我们使用this上下文作为一种机制来按其存储缓存的对象id,然后使用它来决定是否保留数组中的这些对象。

var result = array.filter(function(v) {
  return this[v.id]?
    !Object.assign(this[v.id], v):
    (this[v.id] = v);
}, {});
Run Code Online (Sandbox Code Playgroud)

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>
Run Code Online (Sandbox Code Playgroud)
var result = array.filter(function(v) {
  return this[v.id]?
    !Object.assign(this[v.id], v):
    (this[v.id] = v);
}, {});
Run Code Online (Sandbox Code Playgroud)