[英]How to remove a specific Object from an array of Objects, by object's property?
[英]Remove specific property from an array of object
我在從具有特定屬性的數組對象中拼接項目時遇到問題。 這是解釋。
delColumn($event: any, el: any) {
if ($event.target && el > -1) {
var colId: string = this.receivedData[0].value.columns[el].id;
var obj = this.receivedData[0];
obj.values.columns = obj.values.columns.filter(s => s.id != colId);
obj.values.rows.forEach(s => {
delete s.Col_1;
return s;
});
}
}
現在,我的要求是當我單擊“刪除列”時,它會進入此方法,並且我想刪除與之關聯的特定列和行。
提前感謝您的幫助。
rows=[{Col1:1,Value:1},
{Col1:1,Value:2},
{Col1:1,Value:3},
{Col1:1,Value:4}];
rowsNew=this.rows.map(x=>{
return this.objectWithoutKey(x,"Col1");
})
objectWithoutKey(object, key){
const {[key]: deletedKey, ...otherKeys} = object;
return otherKeys;
}
ngOnInit()
{
console.log(this.rows);
console.log(this.rowsNew);
}
使用filter
和forEach
obj.values.columns = obj.values.columns.filter(s => s.id != "Col_1");
obj.values.rows.forEach( s => {
delete s.Col_1;
return s;
});
我已經為此類問題創建了NPM軟件包
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.