从对象中删除数据
Posted
技术标签:
【中文标题】从对象中删除数据【英文标题】:Delete data from object 【发布时间】:2020-05-03 07:50:29 【问题描述】:我有下面的对象,需要从结果中删除列,我将动态获取列名。你能帮我如何根据列名删除列及其对应的对象"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));
【问题讨论】:
分享你需要的结果数组 您的意思是删除属性column
并删除tabl2_colu
? tabl2_colu
在哪里?
@Ahsan ,@nopole,请查看更新后的数组详情
【参考方案1】:
如果你想删除数组中的某些列,那么你可以使用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);
【讨论】:
谢谢你的回答,但我遇到了错误,'ObjectConstructor'.ts 类型上不存在属性'Fromentries' @user2319726 看起来您需要将"lib": ["es2017"]
添加到您的设置中。 Here yo can read more :)
我需要删除完整的列名对象,我认为现在它只删除列名键值, "tabl1": "tablename":"tabl1","tablecolumns":"yes"," patternCheckStatus":true, "columns": ["columnname":"column1","datatype":"Numeric","patternregex":"jjj",
@user 你能写出想要的输出吗?
请看下面,删除前: "tabl1": "tablename":"tabl1","tablecolumns":"yes","patternCheckStatus":true, "columns": [ "columnname":"column1","datatype":"Numeric","patternregex":"jjj","columnname":"column2","datatype":"UpperCase","patternregex":"hkl;; "], 删除后预期 "tabl1": "tablename":"tabl1","tablecolumns":"yes","patternCheckStatus":true, "columns": ["columnname":"column1" ,"datatype":"Numeric","patternregex":"jjj"], 【参考方案2】:
一种方法是遍历对象数组,通过其属性标识符找到要删除的对象,然后从数组中删除该对象:
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.
【讨论】:
【参考方案3】:我无法正确理解您的问题,但我认为您想从 arr
中删除具有特定 columnname
值的特定对象。
您可以像这样过滤 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'
【讨论】:
感谢您的回答..请查看更新后的数组以上是关于从对象中删除数据的主要内容,如果未能解决你的问题,请参考以下文章