我有下面提到的一系列对象
Flag =[{type: "folder", name: "Real Estate", order: "1", delete: "false", canModify: "true"}
{type: "folder", name: "Legal", order: "2", delete: "false", canModify: "false"}
{type: "folder", name: "Construction", order: "3", delete: "false", canModify: "true"}
{type: "folder", name: "Design", order: "4", delete: "false", canModify: "true"}];
我要从输出中删除名称,顺序的数组
Flag =[{type: "folder", delete: "false", canModify: "true"}
{type: "folder", delete: "false", canModify: "false"}
{type: "folder", delete: "false", canModify: "true"}
{type: "folder", delete: "false", canModify: "true"}];
array.splice()或 array.pop()
答案 0 :(得分:2)
我不确定如何使用splice
和pop
,我会使用rest运算符删除属性:
const Flag = [{
type: "folder",
name: "Real Estate",
order: "1",
delete: "false",
canModify: "true"
}, {
type: "folder",
name: "Legal",
order: "2",
delete: "false",
canModify: "false"
}, {
type: "folder",
name: "Construction",
order: "3",
delete: "false",
canModify: "true"
}, {
type: "folder",
name: "Design",
order: "4",
delete: "false",
canModify: "true"
}, ];
const result = Flag.map(x => {
const {
name,
order,
...rest,
} = x;
return rest;
});
console.dir(result);
答案 1 :(得分:0)
更好的方法是使用delete
运算符将属性删除到对象中
Flag =[{type: "folder", name: "Real Estate", order: "1", delete: "false", canModify: "true"},
{type: "folder", name: "Legal", order: "2", delete: "false", canModify: "false"},
{type: "folder", name: "Construction", order: "3", delete: "false", canModify: "true"},
{type: "folder", name: "Design", order: "4", delete: "false", canModify: "true"}];
Flag.forEach( obj => {
delete obj.name;
delete obj.order;
});
console.log(Flag)
array.splice()
或array.pop()
会将对象删除到您的数组中,而不是属性中
答案 2 :(得分:0)
您可以使用delete
关键字删除对象的属性
let Flag =[{type: "folder", name: "Real Estate", order: "1", delete: "false", canModify: "true"},
{type: "folder", name: "Legal", order: "2", delete: "false", canModify: "false"},
{type: "folder", name: "Construction", order: "3", delete: "false", canModify: "true"},
{type: "folder", name: "Design", order: "4", delete: "false", canModify: "true"}];
Flag.forEach(obj => {
delete obj.name;
delete obj.order;
})
console.log(Flag);
答案 3 :(得分:0)
使用删除
Flag = [{
type: "folder",
name: "Real Estate",
order: "1",
delete: "false",
canModify: "true"
},
{
type: "folder",
name: "Legal",
order: "2",
delete: "false",
canModify: "false"
},
{
type: "folder",
name: "Construction",
order: "3",
delete: "false",
canModify: "true"
},
{
type: "folder",
name: "Design",
order: "4",
delete: "false",
canModify: "true"
}
];
function del() {
for (i = 0; i < Flag.length; i++) {
if (Flag[i].name) delete Flag[i].name;
if (Flag[i].order) delete Flag[i].order;
}
return Flag;
}
console.log(del());
答案 4 :(得分:0)
方法1
Flag = [{
type: "folder",
name: "Real Estate",
order: "1",
delete: "false",
canModify: "true"
},
{
type: "folder",
name: "Legal",
order: "2",
delete: "false",
canModify: "false"
},
{
type: "folder",
name: "Construction",
order: "3",
delete: "false",
canModify: "true"
},
{
type: "folder",
name: "Design",
order: "4",
delete: "false",
canModify: "true"
}
];
var removeKeys = ['name', 'order'];
myArray = Flag.filter(function(obj) {
removeKeys.forEach(e => delete obj[e]);
return obj;
});
console.log(myArray);
方法2
Flag = [{
type: "folder",
name: "Real Estate",
order: "1",
delete: "false",
canModify: "true"
},
{
type: "folder",
name: "Legal",
order: "2",
delete: "false",
canModify: "false"
},
{
type: "folder",
name: "Construction",
order: "3",
delete: "false",
canModify: "true"
},
{
type: "folder",
name: "Design",
order: "4",
delete: "false",
canModify: "true"
}
];
Flag = Flag.filter(function(obj) {
delete obj.name;
delete obj.order
return obj;
});
console.log(Flag);