Array formatting of JSON objects with key values

I have an array with below format, I am trying to remove duplicate keys and merge it to one. Please let me know how to achieve this.

[
  {"tablename":"table1","tablecolumns":"yes"},
  {"tablename":"table1","columnname":"column1"},
  {"tablename":"table1","columnname":"col2"},
  {"tablename":"table2","tablecolumns":"yes"},
  {"tablename":"table2","columnname":"column3"},
  {"tablename":"table2","columnname":"col4"}
]

expected format:

[
    {"tablename":"table1","tablecolumns":"yes","columnname":"column1","columnname":"col2"},
    {"tablename":"table2","tablecolumns":"yes","columnname":"column3","columnname":"col4"}
]

Answers:

Answer

You can use reduce method:

const result = arr.reduce((a, {tablename, tablecolumns, columnname}) => {
  a[tablename] = a[tablename] || {tablename, tablecolumns, columnnames: []};
  if (columnname)
    a[tablename].columnnames.push(columnname);
  return a;
},{})

An example:

let arr = [
  { "tablename": "table1", "tablecolumns": "yes" },
  { "tablename": "table1", "columnname": "column1" },
  { "tablename": "table1", "columnname": "col2" },
  { "tablename": "table2", "tablecolumns": "yes" },
  { "tablename": "table2", "columnname": "column3" },
  { "tablename": "table2", "columnname": "col4" }
];


const result = arr.reduce((a, {tablename, tablecolumns, columnname}) => {
  a[tablename] = a[tablename] || {tablename, tablecolumns, columnnames: []};
  if (columnname)
    a[tablename].columnnames.push(columnname);
  return a;
},{})

console.log(Object.values(result));

UPDATE:

If you have an additional tag datatype, then you can use the following code snippet:

let arr = [
    {"tablename":"table1","tablecolumns":"yes"},
    {"tablename":"table1","columnname":"col1","datatype":"Alphabetic"},
    {"tablename":"table2","tablecolumns":"yes"},
    {"tablename":"table2","columnname":"tabl2_colu","datatype":null},
    {"tablename":"table2","columnname":"tab2_col2","datatype":"Numeric"}
];

const result = arr.reduce((a, {tablename, tablecolumns, columnname, datatype}) => {
    a[tablename] = a[tablename] || {tablename, tablecolumns, columns: []};
    if (columnname)
      a[tablename].columns.push({columnname, datatype});
    return a;
  },{})
console.log(Object.values(result));

Answer

So I have mentioned in my comment below your question you can't have the same property name for an object as you expect - columnname.

Instead I suggest to have a different structure and store the column names in an array.

Like the following:

const data = [
    {"tablename":"table1","tablecolumns":"yes"},
    {"tablename":"table1","columnname":"column1"},
    {"tablename":"table1","columnname":"col2"},
    {"tablename":"table2","tablecolumns":"yes"},
    {"tablename":"table2","columnname":"column3"},
    {"tablename":"table2","columnname":"col4"}
];

const result = data.reduce((a,c) => {
  const foundelem = a.find(e => e.tablename === c.tablename);
  
  if (foundelem) {
    foundelem.columnnames.push(c.columnname);
  } else {
    a.push({
      tablename: c.tablename,
      tablecolumns: c.tablecolumns,
      columnnames: []
    });
  }
  
  return a;
}, []);

console.log(result);

I hope that helps!

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.