Home  >  Forum  >  Angular
Post New Query

How to merge multiple dynamic arrays and store it in another array using angular 2?



Asked on July 06, 2018
Hello,

I am getting multiple arrays dynamically. I need to take pk_location_id from those arrays. But the arrays are not merged, so I am facing issue in taking pk_location_id from those arrays. 

I am getting arrays as follows:
Array(2)
0:{pk_location_id: 3, tenant_id: null, locationname: "Karnataka", locationpath: null, location_phone: null, …}
1:{pk_location_id: 4, tenant_id: null, locationname: "Maharashtra", locationpath: null, location_phone: "9876543211", …}
length:2

Array(3)
0:{pk_location_id: 5, tenant_id: null, locationname: "banglore", locationpath: null, location_phone: "444444444", …}
1:{pk_location_id: 8, tenant_id: null, locationname: "Mysore", locationpath: "/Karnataka/India/HTL", location_phone: "9888888888", …}
2:{pk_location_id: 9, tenant_id: null, locationname: "Hubli", locationpath: "/Karnataka/India/HTL", location_phone: "876543212", …}
length:3

Array(1)
0:{pk_location_id: 6, tenant_id: null, locationname: "jay nagar", locationpath: "/banglore/Karnataka/India/HTL", location_phone: null, …}
length:1

Array(1)
0:{pk_location_id: 7, tenant_id: null, locationname: "puna", locationpath: "", location_phone: null, …}
length:1

I tried to merge these arrays using concat and push but that did not work.
I tried as follows:

const mergedarray = [].concat(...this.locations);

this.locationIds.push(...this.locations);

How to fetch pk_location_id from these arrays and store it in to another array?

Can anyone please provide solution for this?

Thanks & Regards
Shilpa Kulkarni





Replied on July 06, 2018
What is the format of your array. Is this?

[
  {pk_location_id: 2, locationname: "Karnataka", ...},
  {pk_location_id: 3, locationname: "Karnataka",  ...}
]



Replied on July 08, 2018
Yes I am getting array like this format. But I am getting records in different arrays which are dynamic. I want to merge those arrays and want to get only the pk_location_id from that merged array.


Replied on July 09, 2018
Use concat()

   ngOnInit() {
   let arr1 = [
  {pk_location_id: 1, locationname: "Karnataka1"},
  {pk_location_id: 2, locationname: "Karnataka2"}
 ];
   let arr2 = [
  {pk_location_id: 3, locationname: "Karnataka3"},
  {pk_location_id: 4, locationname: "Karnataka4"}
  ];
           let resultArr = [];

           resultArr= resultArr.concat(arr1);
   resultArr= resultArr.concat(arr2);
  resultArr.forEach(el => console.log(el.pk_location_id));
   }

Output

  1
  2
  3
  4



Replied on July 09, 2018
To store pk_location_id in another array use push()

   ngOnInit() {
   let arr1 = [
  {pk_location_id: 1, locationname: "Karnataka1"},
  {pk_location_id: 2, locationname: "Karnataka2"}
];
   let arr2 = [
  {pk_location_id: 3, locationname: "Karnataka3"},
  {pk_location_id: 4, locationname: "Karnataka4"}
  ];
           let resultArr = [];

           resultArr= resultArr.concat(arr1);
   resultArr= resultArr.concat(arr2);
   
  //pk_location_id Store in another array
          let locationArr = [];
  resultArr.forEach(el => locationArr.push(el.pk_location_id));
  
  locationArr.forEach(el => console.log(el));
   }

Output

  1
  2
  3
  4



Replied on July 09, 2018
Thank you Amit. It worked. Can you please tell how to print these pk_location_id in one array, instead of printing the pk_location_id individually.


Replied on July 09, 2018
I think you are looking for console.log(locationArr)

          let locationArr = [];
  resultArr.forEach(el => locationArr.push(el.pk_location_id));
  
          console.log(locationArr);


Write Answer







©2019 concretepage.com | Privacy Policy | Contact Us