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));

asked Jan 16, 2020 at 12:54
3
  • Share the resulting array as well that you need Commented Jan 16, 2020 at 13:03
  • you mean delete the property column and delete tabl2_colu? Where is tabl2_colu? Commented Jan 16, 2020 at 13:06
  • @Ahsan ,@nopole, please see the updated array details Commented Jan 16, 2020 at 13:08

3 Answers 3

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'
answered Jan 16, 2020 at 13:05
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the answer.. please see the updated array
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);

answered Jan 16, 2020 at 13:09

8 Comments

thansk for the answer but i am getting below error , Property 'Fromentries' does not exist on type 'ObjectConstructor'.ts
@user2319726 it looks like you need to add "lib": ["es2017"] into your settings. Here yo can read more :)
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"}, }}
@user could you write the desired output?
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"}]}, }
|
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.
answered Jan 16, 2020 at 13:11

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.