1

I have below object and need to delete the column from the result, I will get the column name dynamically. Could you please help me how to delete the column and its corresponding object based on column name {"columnname":"couln2", "datatype":null} Array is:

{
"tabl1":
{"tablename":"tabl1","tablecolumns":"yes","patternCheckStatus":true,
"columns": [{"columnname":"column1","datatype":"Numeric","patternregex":"jjj"},{"columnname":"column2","datatype":"UpperCase","patternregex":"hkl;;"}]},
"table2":{"tablename":"table2","tablecolumns":"yes","patternCheckStatus":null,
"columns":[{"columnname":"t2column","datatype":"Alphabetic"}]
}}

let arr = 
    {"tabl1":{"tablename":"tabl1","tablecolumns":"yes","patternCheckStatus":true,"columns":[{"columnname":"column1","datatype":"Numeric","patternregex":"jjj"},{"columnname":"column2","datatype":"UpperCase","patternregex":"hkl;;"}]},"table2":{"tablename":"table2","tablecolumns":"yes","patternCheckStatus":null,"columns":[{"columnname":"t2column","datatype":"Alphabetic"}]}}


   

    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));
user2319726
  • 143
  • 1
  • 1
  • 10

3 Answers3

1

I cannot understand your question correctly but I think you want to delete a specific object having a specific columnname value from the arr. You can filter the arr like this:

function deleteColumn (column) {
  let newArr = arr.filter(item => {
     return item.columnname !== column
  })
  return newArr
}

Then you can run:

deleteColumn('tabl2_colu') // Will return an array without object having any columnname = 'tabl2_colu'
Nipun Jain
  • 999
  • 6
  • 13
0

If you want to get rid of some columns in array, then you can use Object.entries while map your array into another array:

let propertyName = 'tablename';
arr.map(s=> Object.fromEntries(Object.entries(s).filter(([k, v]) => k!= propertyName)))

An example:

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;
    },{})

let propertyName = 'tablename';
console.log(Object.values(result)
    .map(s=> Object.fromEntries(Object.entries(s)
        .filter(([k, v]) => k!= propertyName))));

UPDATE:

You can filter your array based in columnname:

let columnname = 'column2';
obj.tabl1.columns = obj.tabl1.columns.filter(f=> f.columnname != columnname);

An example:

let obj = { "tabl1":
  { "tablename": "tabl1", "tablecolumns": "yes", "patternCheckStatus": true,
  "columns": [{ "columnname": "column1", "datatype": "Numeric", "patternregex": "jjj" },
  { "columnname": "column2", "datatype": "UpperCase", "patternregex": "hkl;;" }] }, };

let columnname = 'column2';
obj.tabl1.columns = obj.tabl1.columns.filter(f=> f.columnname != columnname);
console.log(obj);
StepUp
  • 36,391
  • 15
  • 88
  • 148
  • thansk for the answer but i am getting below error , Property 'Fromentries' does not exist on type 'ObjectConstructor'.ts – user2319726 Jan 16 '20 at 13:19
  • @user2319726 it looks like you need to add `"lib": ["es2017"]` into your settings. [Here yo can read more](https://stackoverflow.com/questions/45422573/property-entries-does-not-exist-on-type-objectconstructor) :) – StepUp Jan 16 '20 at 13:25
  • I need to remove complete columnname object , i think now its deleting only column name key value, { "tabl1": {"tablename":"tabl1","tablecolumns":"yes","patternCheckStatus":true, "columns": [{"columnname":"column1","datatype":"Numeric","patternregex":"jjj"}, }} – user2319726 Jan 16 '20 at 13:46
  • @user could you write the desired output? – StepUp Jan 16 '20 at 13:49
  • Please see the below , before delete: { "tabl1": {"tablename":"tabl1","tablecolumns":"yes","patternCheckStatus":true, "columns": [{"columnname":"column1","datatype":"Numeric","patternregex":"jjj"},{"columnname":"column2","datatype":"UpperCase","patternregex":"hkl;;"}]}, } expected after deleting { "tabl1": {"tablename":"tabl1","tablecolumns":"yes","patternCheckStatus":true, "columns": [{"columnname":"column1","datatype":"Numeric","patternregex":"jjj"}]}, } – user2319726 Jan 16 '20 at 13:58
  • is it possible to achieve the above ? – user2319726 Jan 16 '20 at 17:08
  • thanks for the answer .. how to make table name as dynamic .. now you used tabl1 but i will not no the table name and i will only get column name .. – user2319726 Jan 16 '20 at 19:10
  • @user2319726 you can get your desired object by using square brackets: `let key='tabl1'; ` and then you can use like this `obj[key]` – StepUp Jan 16 '20 at 19:18
0

One way would be to loop through the array of objects, find the one you want to delete by the mean of its property identifier and them just remove that object from the array:

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

function remove_object_by_colname( colname )
  arr.forEach(function( arrayItem, index ) {
    if ( arrayItem.columnname == colname ) {
      arr.splice( index, 1 );
    }
  };
} );

remove_object_by_colname( 'tabl2_colu' ); // Will remove the 4th object from arr.
Jazzpaths
  • 645
  • 5
  • 9