Returns 具有一个旧值的组合值的新数组

Returns the new array of combined values with one old value

我正在使用 Lodash 将两个数组组合成 return 个新值,

示例,

var lastValue = [{"id": 5, "view": {"id": 2932, "name": "Available"}, "rowAttribute": "last value 1"}, {"id": 6, "view": {"id": 2933, "name": "Active"}, "rowAttribute": "last value 2"}]

var updatedValue = [{"view": {"id": 2932, "name": "Available"}, "rowAttribute": "updated value 1"}, {"view": {"id": 2934, "name": "inactive"}, "rowAttribute": "updated value 2"}]

我正在使用 unionBy,

_.unionBy(lastValue, updatedValue, 'view.id');

我越来越喜欢,

[{"view": {"id": 2932, "name": "Available"}, "rowAttribute": "updated value 1"}, {"id": 6, "view": {"id": 2933, "name": "Active"}, "rowAttribute": "last value 2"}, {"view": {"id": 2934, "name": "inactive"}, "rowAttribute": "updated value 2"}]

我的期望是,

 [
  {
    "id": 5,
    "view": {
      "id": 2932,
      "name": "Available"
    },
    "rowAttribute": "updated value 1"
  },
  {
    "id": 6,
    "view": {
      "id": 2933,
      "name": "Active"
    },
    "rowAttribute": "last value 2"
  },
  {
    "view": {
      "id": 2934,
      "name": "inactive"
    },
    "rowAttribute": "updated value 2"
  }
]

我想要两个数组的组合值。如果 view.id is/are 在 updatedValue 中可用,那么它应该 return 更新后的值与 Id else 相同,来自两个数组。

您可以简单地使用 Object.assign 来获得想要的结果

var lastValue = [
  { id: 5, view: { id: 2932, name: "Available" }, rowAttribute: "last value" },
];

var updatedValue = [
  { view: { id: 2932, name: "Available" }, rowAttribute: "updated value" },
];

const result = lastValue.map((o, i) => Object.assign({}, o, updatedValue[i]));
console.log(result);

如果你想用view.id

更新对象

var lastValue = [
  { id: 5, view: { id: 2932, name: "Available" }, rowAttribute: "last value" },
];

var updatedValue = [
  { view: { id: 2932, name: "Available" }, rowAttribute: "updated value" },
];

const result = lastValue.map((o, i) =>
  Object.assign(
    {},
    o,
    updatedValue.find((obj) => obj.view.id === o.view.id)
  )
);
console.log(result);

您甚至可以使用 Map as

来优化结果

var lastValue = [
  { id: 5, view: { id: 2932, name: "Available" }, rowAttribute: "last value" },
];

var updatedValue = [
  { view: { id: 2932, name: "Available" }, rowAttribute: "updated value" },
];

const dict = new Map(updatedValue.map((o) => [o.view.id, o]));

const result = lastValue.map((o, i) => Object.assign({}, o, dict.get(o.view.id)));
console.log(result);

更新#4 如果你想加入数组那么你必须使用 findObject.assign

var lastValue = [
  {
    id: 5,
    view: { id: 2932, name: "Available" },
    rowAttribute: "last value 1",
  },
  { id: 6, view: { id: 2933, name: "Active" }, rowAttribute: "last value 2" },
];

var updatedValue = [
  { view: { id: 2932, name: "Available" }, rowAttribute: "updated value 1" },
  { view: { id: 2934, name: "inactive" }, rowAttribute: "updated value 2" },
];

updatedValue.forEach((o) => {
  const objInLastValue = lastValue.find((obj) => obj.view.id === o.view.id);
  if (objInLastValue) Object.assign(objInLastValue, o);
  else lastValue.push(o);
});

console.log(lastValue);
/* This is not a part of answer. It is just to give the output fill height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }