Search code examples
arraysangularcomparison

How to compare the JSON format array VALUE and KEY to create a new array? in Angular 5


Here is my first JSON array format:

this.columnNames = [
  {field : "Name"},
  {field : "Address"},
  {field : "Age"}
];

Here is my first JSON array format:

this.rowData = [
  {Name : "Praveen",Address : "aiff",Age : "12",w :  "1",e :  "8"},
  {Name : "Akashay",Address : "xvn",Age : "15",w :  "2",e :  "7"},
  {Name : "Bala",Address : "hjk",Age : "16",w :  "3",e :  "6"}, 
  {Name : "Charu",Address : "sss",Age : "17",w :  "4",e :  "5"},
];  

Here I want to to compare the VALUE which is present in the first array(columnNames) and KEYS which is present in the second array. If it's equal, then I want to push those matching data from the second array(rowData) into the new array.

And I want my final result like this:

public rowData: any =[
  {Name : "Praveen",Address : "aiff",Age : "12"},
  {Name : "Akashay",Address : "xvn",Age : "15"},
  {Name : "Bala",Address : "hjk",Age : "16"}, 
  {Name : "Charu",Address : "sss",Age : "17"},
];

Solution

  • Grab the fields from each object in your columnNames array using .map(). Then, map each object in rowData to a new object created using .reduce(), which only includes the keys from your fields array:

    const columnNames = [
      {field : "Name"},
      {field : "Address"},
      {field : "Age"}
    ];
    
    const rowData = [
      {Name : "Praveen",Address : "aiff",Age : "12",w :  "1",e :  "8"},
      {Name : "Akashay",Address : "xvn",Age : "15",w :  "2",e :  "7"},
      {Name : "Bala",Address : "hjk",Age : "16",w :  "3",e :  "6"}, 
      {Name : "Charu",Address : "sss",Age : "17",w :  "4",e :  "5"},
    ];  
    
    const fields = columnNames.map(({field}) => field); // get array ["Name", "Address", "Age"]
    const result = rowData.map( // map each object in rowData to a new object
      o => fields.reduce((obj, k) => ({...obj, [k]: o[k]}), {})
      //    ^^ construct the new object, using reduce, spread syntax and computed property names
    );
    
    console.log(result);
    .as-console-wrapper { max-height: 100% !important;} /* ignore */

    If you can support Object.fromEntries() (which takes an array of nested [key, value] pairs and builds an object from them), then there is no need to use .reduce():

    const columnNames = [
      {field : "Name"},
      {field : "Address"},
      {field : "Age"}
    ];
    
    const rowData = [
      {Name : "Praveen",Address : "aiff",Age : "12",w :  "1",e :  "8"},
      {Name : "Akashay",Address : "xvn",Age : "15",w :  "2",e :  "7"},
      {Name : "Bala",Address : "hjk",Age : "16",w :  "3",e :  "6"}, 
      {Name : "Charu",Address : "sss",Age : "17",w :  "4",e :  "5"},
    ];  
    
    const fields = columnNames.map(({field}) => field);
    const result = rowData.map( 
      o => Object.fromEntries(fields.map(k => [k, o[k]]))
    );
    
    console.log(result);
    .as-console-wrapper { max-height: 100% !important;} /* ignore */