0

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!

1
  • the reason that's not working is becaues array1 does not have any of the properties in array2, it's an array. you need to iterate over array1, and select the properties from there to add to array3 Commented Nov 21, 2019 at 12:15

5 Answers 5

1

You can simply use below code and achieve he desired outcome, no need to use any library.

array1.forEach(elements => {
  let a = {};
  array2.forEach((elem)=>{
    a[elem.header] = elements[elem.field];
  });
  array3.push(a);
});
console.log(array3);
Sign up to request clarification or add additional context in comments.

1 Comment

thank you for your response, & this is working perfectly fine
1

you can make a intersection:

let intersection = arrA.filter(x => arrB.includes(x));

your case:

let array3 = array1.filter(x => array2.includes(x));

intersection with lodash:

const _ = require("lodash")
var a = { 'a': 1, 'b': 2, 'c': 3 };
var b = { 'c': 3, 'd': 4, 'e': 5 };

_.intersection(_.keys(a), _.keys(b)); // ['c']

Comments

1
array2.forEach(element => {
   array1.forEach(e => {
       array3.push(e[element.field]);
   }
})

Comments

1

var 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
}]

var array2 = [{
  "field": "index",
  "header": "INDEX"
}, {
  "field": "firstname",
  "header": "FIRSTNAME"
}, {
  "field": "lastname",
  "header": "LASTNAME"
}];

var array3 = [];


array1.forEach(value => {
  var hasAnyField = false;
  var temp = {}; 
  array2.forEach(fieldDef => { //iterate through for each field definition

    var target = value[fieldDef.field];
    if (target) { // field located
      hasAnyField = true; // it has at least 1 field
      temp[fieldDef.header] = target; // set the header
    }
  });

  if (hasAnyField) { // if has any field we will have a valid object then
    array3.push(temp);
  }

});

console.log(array3);

Comments

1

A lodash way:

const 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}];

const array2 = [{"field":"index","header":"INDEX"},{"field":"firstname","header":"FIRSTNAME"},{"field":"lastname","header":"LASTNAME"}];


const fields = array2.map(field => field.field);

const array3 = array1.map(e => _pick(e, fields));

console.log(array3);

Stackblitz: https://stackblitz.com/edit/angular-aq8tua

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.