[英]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.