简体   繁体   English

在一个对象数组中结合两个不同对象数组的不同属性

[英]Combining different properties of two different arrays of objects in one array of objects

I have one array of objects and two objects 我有一个对象数组和两个对象

array of objects something like this 像这样的对象数组

 [ { id: 1, name: 'Monthly Plan', cycle: 1, fees: 1000 },
      { id: 2, name: 'Yearly Plan', cycle: 12, fees: 10000 },
      { id: 3, name: 'Two Months Plan', cycle: 2, fees: 1500 },
      { id: 4, name: 'Three Months Plan', cycle: 3, fees: 2500 },
      { id: 5, name: 'Four Months Plan', cycle: 4, fees: 3000 }]

and now comes the objects actually I am making these objects by running a for loop something like this 现在出现了对象,实际上我通过运行for循环来制作这些对象,如下所示

if (newPlans) {
       newPlans.forEach(element => {
       let object = {};
       object.id = element.id;
       object.name = element.name,
       object.cycle = element.cycle,
       object.fees = element.fees
       console.log(object);
       console.log(Object.assign(billPlans.filter(bp => bp.id), object))
 });

  }

and I want to get result something like this 我想得到这样的结果

[ { id: 1, name: 'Monthly Plan', cycle: 1, fees: 1000 },
  { id: 2, name: 'Yearly Plan', cycle: 12, fees: 10000 },
  { id: 3, name: 'Two Months Plan', cycle: 2, fees: 1500 },
  { id: 4, name: 'Three Months Plan', cycle: 3, fees: 2500 },
  { id: 5, name: 'Four Months Plan', cycle: 4, fees: 3000 },
  { id: 6, name: 'Five Months Plan', cycle: 5, fees: 4000 } 
  { id: 7, name: 'Six Months Plan', cycle: 6, fees: 5000 } ]

but I am getting something like this 但我得到这样的东西

//console.log(object)
{ id: 6, name: 'Five Months Plan', cycle: 5, fees: 4000 }

 //console.log(object.assign)
[ { id: 1, name: 'Monthly Plan', cycle: 1, fees: 1000 },
  { id: 2, name: 'Yearly Plan', cycle: 12, fees: 10000 },
  { id: 3, name: 'Two Months Plan', cycle: 2, fees: 1500 },
  { id: 4, name: 'Three Months Plan', cycle: 3, fees: 2500 },
  { id: 5, name: 'Four Months Plan', cycle: 4, fees: 3000 },
  id: 6,
  name: 'Five Months Plan',
  cycle: 5,
  fees: 4000 ]

 //console.log(object)
{ id: 109, name: 'Six Months Plan', cycle: 6, fees: 5000 }

 //console.log(object.assign)
[ { id: 1, name: 'Monthly Plan', cycle: 1, fees: 1000 },
  { id: 2, name: 'Yearly Plan', cycle: 12, fees: 10000 },
  { id: 3, name: 'Two Months Plan', cycle: 2, fees: 1500 },
  { id: 4, name: 'Three Months Plan', cycle: 3, fees: 2500 },
  { id: 5, name: 'Four Months Plan', cycle: 4, fees: 3000 },
  id: 7,
  name: 'Six Months Plan',
  cycle: 6,
  fees: 5000 ]

As you can see the Object.assign is overriding the previously added object and object is assigning not in correct way as you can see it is not assigned in the object way so how can I achieve this . 如您所见, Object.assign覆盖了先前添加的对象,并且object分配的方式不正确,因为您看到它不是以对象方式分配的,所以我该如何实现。

You need not to assign, but push the object into the array. 您无需分配,而是将对象推入数组。

If the billPlans is the above array with 5 elements, you need just to do 如果billPlans是上面包含5个元素的数组, billPlans需要做

let object = {};
object.id = element.id;
object.name = element.name,
object.cycle = element.cycle,
object.fees = element.fees

billPlans.push(object);

Also I don't know why you are filtering the array, but the main concept is to push into the array, not use Object.assign . 我也不知道为什么要过滤数组,但是主要的概念是推入数组,而不是使用Object.assign

Object.assign just copies the properties from the destinations into the source object. Object.assign只是将属性从目标复制到源对象。

Expounding further to the previous answer given, try using a more functional approach that doesn't risk mutating data as a side effect. 进一步解释先前给出的答案,请尝试使用功能更强大的方法,该方法不会冒造成数据突变的风险。 .forEach() will run a loop, executing a given function at each index, but will NOT preserve data as it was originally passed. .forEach()将运行一个循环,在每个索引处执行给定功能,但不会保留最初传递的数据。 Instead use, .map() , .filter() , or .reduce() depending in your end goal. 而是使用, .map() .filter().reduce()取决于在你的最终目标。 Looking at your code and question as is, .map() works fine (though you can easily chain .filter() / .reduce() or even more .map() methods to the original .map() . .forEach() is not chain-able. .filter()看您的代码和问题, .map()可以正常工作(尽管您可以轻松地将.filter() / .filter() .reduce()或更多.map()方法链接到原始.map() 。. .forEach()不可链接。

const ObjGenerator = (newPlans) =>  
    !newPlans ? null :
         newPlans
           .map(el => { ...el }
           .map(obj => {
             console.log(obj)
             return obj
           }
           .map(obj => {
            // rather than using the immutable `.push()` method, try 
            // the non-immutable alternative via `.concat()`
            // Also, dynamic invocation of Object.assign provided.

            return Object.assign.apply(billPlans.filter(bp => bp.id), [{}].concat(obj))
           }

ObjGenerator(newPlans)  

Using the approach I describe also removes any duplicate entries that may get created. 使用我描述的方法还删除了可能创建的所有重复条目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 合并来自两个不同数组的两个对象 - Combining two objects from two different arrays 将两个单独的数组与对象组合成一个数组 - combining two separate arrays into one array with objects 匹配两个不同对象的属性 arrays - Matching properties of objects in two different arrays 从两个不同的数组创建对象数组 - Create an array of objects from two different arrays 查找 function 以匹配两个不同 arrays 对象中的相同 ID,并将键/值对插入对象数组之一 - A lookup function to match the same IDs in two different arrays of objects and inserting key/value pairs into one of the array of objects 如何通过比较对象的两个数组与对象中不同元素的方法来过滤数组? - How to filter array by comparing two arrays of objects with different elements in their objects? JavaScript 如何通过两个不同的 arrays 对象并有条件地返回一个数组 - JavaScript how to map through two different arrays of objects and return one array conditionally Angular JS:将对象从一个数组存储到不同的数组/服务 - Angular JS: Store objects from one array to different arrays/services 两种不同类型的对象数组 - Two different types of array of objects 如何比较javascript中两个不同arrays的对象 - How to compare objects of two different arrays in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM