我有一個這樣的物件:
var obj = [
{
item1: 'test',
item2: 'something',
item3: 'REMOVE THIS',
item4: 'REMOVE THIS',
item5: 'REMOVE THIS',
},
{
item1: 'test',
item2: 'something',
item3: 'REMOVE THIS',
item4: 'REMOVE THIS',
item5: 'REMOVE THIS',
},
{
item1: 'test',
item2: 'something',
item3: 'REMOVE THIS',
item4: 'REMOVE THIS',
item5: 'REMOVE THIS',
}
]
我obj現在想代表這個:
var obj = [
{
item1: 'test',
item2: 'something',
},
{
item1: 'test',
item2: 'something',
},
{
item1: 'test',
item2: 'something',
}
]
我找到了很多關于物件操作和使用鍵等的答案
JavaScript:物件的 filter()
如何有效地過濾物件的物件?
如何在 ES6 中使用其值過濾物件
這些答案的問題是
它是一種特定的物件格式,它們不包括如何處理多個物件
它完全洗掉物件,而不是值和鍵/行專案
我最接近的是:
var obj = [
{
item1: 'test',
item2: 'something',
item3: 'REMOVE THIS',
item4: 'REMOVE THIS',
item5: 'REMOVE THIS',
},
{
item1: 'test',
item2: 'something',
item3: 'REMOVE THIS',
item4: 'REMOVE THIS',
item5: 'REMOVE THIS',
},
{
item1: 'test',
item2: 'something',
item3: 'REMOVE THIS',
item4: 'REMOVE THIS',
item5: 'REMOVE THIS',
}
]
var newObj = [];
obj.forEach(function(el){
newObj.push(el.item1);
newObj.push(el.item2);
})
console.log(newObj)
顯然這并不完全是因為我需要推送完整的物件而不僅僅是鍵值。
我正在嘗試類似How to filter an object with its values in ES6但這個答案不起作用,因為它是按值而不是按鍵(?)
var obj = [
{
item1: 'test',
item2: 'something',
item3: 'REMOVE THIS',
item4: 'REMOVE THIS',
item5: 'REMOVE THIS',
},
{
item1: 'test',
item2: 'something',
item3: 'REMOVE THIS',
item4: 'REMOVE THIS',
item5: 'REMOVE THIS',
},
{
item1: 'test',
item2: 'something',
item3: 'REMOVE THIS',
item4: 'REMOVE THIS',
item5: 'REMOVE THIS',
}
]
var acceptedProps = ['item1', 'item2'];
var newObj = Object.keys(obj).reduce(function(r, e) {
if (acceptedProps.includes(obj[e])) r[e] = obj[e]
return r;
}, {})
console.log(newObj)
它只是給了我一個空陣列。我也試著翻轉一些東西
var obj = [
{
item1: 'test',
item2: 'something',
item3: 'REMOVE THIS',
item4: 'REMOVE THIS',
item5: 'REMOVE THIS',
},
{
item1: 'test',
item2: 'something',
item3: 'REMOVE THIS',
item4: 'REMOVE THIS',
item5: 'REMOVE THIS',
},
{
item1: 'test',
item2: 'something',
item3: 'REMOVE THIS',
item4: 'REMOVE THIS',
item5: 'REMOVE THIS',
}
]
var acceptedProps = ['item1', 'item2'];
var newObj = Object.keys(obj).reduce(function(r, e) {
if (acceptedProps.includes(obj[r])) r[e] = obj[r]
return r;
}, {})
console.log(newObj)
idk這個函式是什么意思r?e
對于多個物件的陣列,如何按屬性而不是值過濾物件?
編輯-好的,我剛剛找到了這個答案:
根據物件屬性洗掉陣列元素
var obj = [
{
item1: 'test',
item2: 'something',
item3: 'REMOVE THIS',
item4: 'REMOVE THIS',
item5: 'REMOVE THIS',
},
{
item1: 'test',
item2: 'something',
item3: 'REMOVE THIS',
item4: 'REMOVE THIS',
item5: 'REMOVE THIS',
},
{
item1: 'test',
item2: 'something',
item3: 'REMOVE THIS',
item4: 'REMOVE THIS',
item5: 'REMOVE THIS',
}
]
var newObj = obj.filter(function( obj ) {
return obj.field !== 'item3';
});
console.log(newObj)
所以現在...我必須以某種方式將其更改為 lop(?) 并在沒有行專案的情況下更新每個物件...
var obj = [
{
item1: 'test',
item2: 'something',
item3: 'REMOVE THIS',
item4: 'REMOVE THIS',
item5: 'REMOVE THIS',
},
{
item1: 'test',
item2: 'something',
item3: 'REMOVE THIS',
item4: 'REMOVE THIS',
item5: 'REMOVE THIS',
},
{
item1: 'test',
item2: 'something',
item3: 'REMOVE THIS',
item4: 'REMOVE THIS',
item5: 'REMOVE THIS',
}
]
var obj = [
{
item1: 'test',
item2: 'something',
item3: 'REMOVE THIS',
item4: 'REMOVE THIS',
item5: 'REMOVE THIS',
},
{
item1: 'test',
item2: 'something',
item3: 'REMOVE THIS',
item4: 'REMOVE THIS',
item5: 'REMOVE THIS',
},
{
item1: 'test',
item2: 'something',
item3: 'REMOVE THIS',
item4: 'REMOVE THIS',
item5: 'REMOVE THIS',
}
]
var newObj = obj.forEach(function(el) {
el.filter(function( el ) {
return el.field !== 'item3';
});
})
console.log(newObj)
但我得到:
el.filter is not a function...
我該怎么做呢?我究竟做錯了什么?
uj5u.com熱心網友回復:
您可以使用Array.map()迭代陣列項以創建新陣列,Object.entries()過濾出作為陣列項的物件的值,并用于Object.fromEntries()從過濾后的條目中構建物件。
var obj = [{
item1: 'test',
item2: 'something',
item3: 'REMOVE THIS',
item4: 'REMOVE THIS',
item5: 'REMOVE THIS',
},
{
item1: 'test',
item2: 'something',
item3: 'REMOVE THIS',
item4: 'REMOVE THIS',
item5: 'REMOVE THIS',
},
{
item1: 'test',
item2: 'something',
item3: 'REMOVE THIS',
item4: 'REMOVE THIS',
item5: 'REMOVE THIS',
}
];
var newObj = obj.map(item => Object.fromEntries(
Object.entries(item)
.filter(([key, value]) => value !== 'REMOVE THIS')
));
console.log(newObj);
uj5u.com熱心網友回復:
你可以嘗試類似的東西
obj.map((item) => {
return {
item1: item.item1,
item2: item.item2,
}
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/480433.html
標籤:javascript 数组 目的
