Combining different properties of two different arrays of objects in one array of objects

Vikas

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

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 .

Suren Srapyan

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

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 just copies the properties from the destinations into the source object.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Compare two arrays of objects, where objects in each array have different properties

Modify objects in resutl array when combining two arrays into one

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 merge objects present in two different arrays into a single array of objects?

How to filter array by comparing two arrays of objects with different elements in their objects?

Merge two objects in different arrays

Compare Two different arrays and update array of objects with useState in React

Swift: different objects in one array?

How to combine two list of different objects into one list by combining one element from each list?

How to merge two matching objects from different array into one object?

Binding two objects of different classes but with similar properties

How to test if two collections of objects are in a different order based on one of the objects' properties?

Create object from objects in two different arrays

How to compare objects of two different arrays in javascript

How to convert an array of objects (of different lengths) into one object that has different properties from the elements in the original array?

How to compare object properties between different objects in different arrays

select data from two different arrays of objects to create a new array of objects

Recursively filter array of objects with different properties

AngularJS combining objects in array on properties

Union of two array of two different Objects

Javascript: How to call on different objects in array of arrays?

How to sort different arrays of objects inside an array

Merge objects with corresponding key values from two different arrays of objects

Combining two Objects with same properties to array value Javascript

Combining two ggplot objects from different function calls?

Different objects from one interface with different methods and properties

How to join two arrays into one array of objects

Merge two arrays into one Array of objects React

How to combine two arrays into one array of objects?