我下面有一个数组对象。我想删除secondItem和fourthItem。我尝试这样做但没有运气
var removed = item.splice(1)
如何同时删除secondItem和fourthItem?
items:[]
0:
firstItem: "testing"
secondItem: "record"
thirdItem: 30
fourthItem: "40"
1:
firstItem: "testing2"
secondItem: "record2"
thirdItem: 33
fourthItem: 44
删除后应该看起来像这样
0:
firstItem:“正在测试”
thirdItem:30
1:
firstItem:“ testing2”
thirdItem:33
答案 0 :(得分:0)
在这种情况下,对于您存储在数组中的对象,将无法使用splice
方法,如果要删除对象中的第二和第四项,请使用delete
:
items.forEach(item => {
delete item.secondItem;
delete item.fourthItem;
});
答案 1 :(得分:0)
const values = [
{
firstItem: "testing",
secondItem: "record",
thirdItem: 30,
fourthItem: "40"
}, {
firstItem: "testing2",
secondItem: "record2",
thirdItem: 33,
fourthItem: 44
}
]
const res = values.map(({
firstItem,
thirdItem
}) => ({
firstItem,
thirdItem
}))
console.log(res)