I have the following arrays,
array1 = [{"index":1,"firstname":"John","lastname":"Cruiser","phoneNumber":765758757},{"index":2,"firstname":"Carl","lastname":"Turner","phoneNumber":123432434},{"index":1,"firstname":"Anna","lastname":"Mull","phoneNumber":23434455}]
array2 = [{"field":"index","header":"INDEX"},{"field":"firstname","header":"FIRSTNAME"},{"field":"lastname","header":"LASTNAME"}]
Now, I want push data of array1 into a new array but I do not want to add all columns into the new array, I want to add only those fields which are available in array2.
The output of array3 should be as below,
array3 = [{"INDEX":1, "FIRSTNAME":"John","LASTNAME":"Cruiser" },{"INDEX":2,"FIRSTNAME":"Carl","LASTNAME":"Turner"},{"INDEX":1,"FIRSTNAME":"Anna","LASTNAME":"Mull"}]
I tried doing something like this,
array2.forEach(element => {
array3.push(array1[element.field]);
})
But, this is not working can someone help me with this,Any help is appreciated.
Thanks in advance!