Reputation: 143
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));
Upvotes: 1
Views: 474
Reputation: 38094
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);
Upvotes: 0
Reputation: 709
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.
Upvotes: 0
Reputation: 1014
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'
Upvotes: 1