插入匹配 属性 的对象

inserting object that matches the property

我们如何将对象 1 插入到下面对象 2 的每个对象中,以便每个对象都具有第一个对象的值,因此 属性 来自对象的 firstMileRange、secondMileRange 和 thirdMileRange 将是替换为对象 1 中的值。

任何想法都将不胜感激。谢谢

#Object 1 - 要插入的数据或对象

[
    {
        "firstMileRange": 1,
        "secondMileRange": 2,
        "thirdMileRange": 3
    }
]

#对象 2 - 将数据插入到的对象

[
    {
        "id": 1521,
        "demographicType": "Demos",
        "demos": "Estimated Population",
        "streetName": null,
        "vehicleCount": null,
        "firstMile": 100000,
        "firstMileRange": null,
        "secondMile": 50000,
        "secondMileRange": null,
        "thirdMile": 50000,
        "thirdMileRange": null
    },
    {
        "id": 1522,
        "demographicType": "Demos",
        "demos": "Households",
        "streetName": null,
        "vehicleCount": null,
        "firstMile": 5999,
        "firstMileRange": null,
        "secondMile": 5999,
        "secondMileRange": null,
        "thirdMile": 5999,
        "thirdMileRange": null
    },
]

#expectedOutput

[
    {
        "id": 1521,
        "demographicType": "Demos",
        "demos": "Estimated Population",
        "streetName": null,
        "vehicleCount": null,
        "firstMile": 100000,
        "firstMileRange": 1,
        "secondMile": 50000,
        "secondMileRange": 2,
        "thirdMile": 50000,
        "thirdMileRange": 3
    },
    {
        "id": 1522,
        "demographicType": "Demos",
        "demos": "Households",
        "streetName": null,
        "vehicleCount": null,
        "firstMile": 5999,
        "firstMileRange": 1,
        "secondMile": 5999,
        "secondMileRange": 2,
        "thirdMile": 5999,
        "thirdMileRange": 3,
    },
]
const array1 = [{a: 1, b: 1, c: 1}, {a: 2, b: 2, c: 2}, {a: 3, b: 3, c: 3}];
const obj = { a: 999, b: 123 }

array1.forEach(o => { o.a = obj.a; o.b = obj.b; });

console.log(array1);

// expected output: Array [Object { a: 999, b: 123, c: 1 }, Object { a: 999, b: 123, c: 2 }, Object { a: 999, b: 123, c: 3 }]

您可以使用 map 方法遍历包含对象的数组,并将 object 与在数组中找到的每个对象合并。

const objectsArray = [{
      id: 1521,
      demographicType: "Demos",
      demos: "Estimated Population ",
      streetName: null,
      vehicleCount: null,
      firstMile: 100000,
      firstMileRange: null,
      secondMile: 50000,
      secondMileRange: null,
      thirdMile: 50000,
      thirdMileRange: null
    },
    {
      id: 1522,
      demographicType: "Demos",
      demos: "Households",
      streetName: null,
      vehicleCount: null,
      firstMile: 5999,
      firstMileRange: null,
      secondMile: 5999,
      secondMileRange: null,
      thirdMile: 5999,
      thirdMileRange: null
    }
  ],
  theObject = {
    firstMileRange: 1,
    secondMileRange: 2,
    thirdMileRange: 3
  },
  /** merge an object into another */
  merge = (arr, obj) => arr.map(o => o = {...o, ...obj});

console.log(merge(objectsArray, theObject));

以上示例按预期工作,但存在问题!如果您要应用于其他对象的对象包含您不希望它们合并到最终对象数组中的属性怎么办?上面的例子会合并这些不需要的属性!为了解决这个问题,我们可能有一个应该提取的所需属性的映射。

const objectsArray = [{
      id: 1521,
      demographicType: 'Demos',
      demos: 'Estimated Population',
      streetName: null,
      vehicleCount: null,
      firstMile: 100000,
      firstMileRange: null,
      secondMile: 50000,
      secondMileRange: null,
      thirdMile: 50000,
      thirdMileRange: null
    },
    {
      id: 1522,
      demographicType: 'Demos',
      demos: 'Households',
      streetName: null,
      vehicleCount: null,
      firstMile: 5999,
      firstMileRange: null,
      secondMile: 5999,
      secondMileRange: null,
      thirdMile: 5999,
      thirdMileRange: null
    }
  ],
  theObject = {
    firstMileRange: 1,
    secondMileRange: 2,
    thirdMileRange: 3,
    unwantedAttr1: 'should not',
    unwantedAttr2: 'be merged'
  },
  attributesMap = ['firstMileRange', 'secondMileRange', 'thirdMileRange'],
  /** merges an object into another, only attributes passed as the third parameter will be merged and any other attribute will be ignored  */
merge = (arr, obj, attrs) => arr.map(o => {
  attrs.forEach(a => o[a] = obj[a] || null);
  return o;
});

console.log(merge(objectsArray, theObject, attributesMap));

因此,unwantedAttr1unwantedAttr2 不会合并到对象中,因为它们没有出现在传递给 merge 函数的 attributesMap 中。

.map() 遍历第二个数组。在第二个数组的每个对象上,将第一个数组合并到第二个数组的对象:

Object.assign(obj2, ...arr1)
// spread ... arr2 to expose the object inside of it.

const update = [{
  "firstMileRange": 1,
  "secondMileRange": 2,
  "thirdMileRange": 3
}];

const group = [{
    "id": 1521,
    "demographicType": "Demos",
    "demos": "Estimated Population",
    "streetName": null,
    "vehicleCount": null,
    "firstMile": 100000,
    "firstMileRange": null,
    "secondMile": 50000,
    "secondMileRange": null,
    "thirdMile": 50000,
    "thirdMileRange": null
  },
  {
    "id": 1522,
    "demographicType": "Demos",
    "demos": "Households",
    "streetName": null,
    "vehicleCount": null,
    "firstMile": 5999,
    "firstMileRange": null,
    "secondMile": 5999,
    "secondMileRange": null,
    "thirdMile": 5999,
    "thirdMileRange": null
  }
];

const updated = group.map(obj => Object.assign(obj, ...update));

console.log(updated);