我有下面的对象,需要从结果中删除列,我将动态获取列名。您能帮我如何基于列名{"columnname":"couln2", "datatype":null}
删除列及其对应的对象
数组是:
{
"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));
答案 0 :(得分:1)
我无法正确理解您的问题,但我认为您想从columnname
中删除具有特定arr
值的特定对象。
您可以这样过滤arr:
function deleteColumn (column) {
let newArr = arr.filter(item => {
return item.columnname !== column
})
return newArr
}
然后您可以运行:
deleteColumn('tabl2_colu') // Will return an array without object having any columnname = 'tabl2_colu'
答案 1 :(得分:0)
如果要摆脱数组中的某些列,则可以在将Object.entries
的数组放入另一个数组时使用map
:
let propertyName = 'tablename';
arr.map(s=> Object.fromEntries(Object.entries(s).filter(([k, v]) => k!= propertyName)))
一个例子:
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))));
更新:
您可以基于columnname
过滤数组:
let columnname = 'column2';
obj.tabl1.columns = obj.tabl1.columns.filter(f=> f.columnname != columnname);
一个例子:
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);
答案 2 :(得分:0)
一种方法是遍历对象数组,通过其属性标识符找到要删除的对象,然后将它们从数组中删除:
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.