JavaScript数组对象删除某些元素实际开发中,总是会遇到数组对象中,删除指定的元素对象,以下⽰例是我在开发中使⽤的的删除的⽅法,仅供参考
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>数组对象删除元素对象</title>
</head>
<body>
<script>
// 1、从数组中删除某些元素
let array =[
{
INSTU_CDE:'0000',
ORG_NAME:'北区分⾏',
LEVELUNIT:'2',
APP_ID:'62',
SUPERUNITID:'500',
UNITSEQ:'500,002',
ID:'002',
UNITNAME:'北区分⾏',
UNITID:'002',
ISLEAF:false,
},
{
INSTU_CDE:'0000',
ORG_NAME:'东区分⾏',
LEVELUNIT:'2',
APP_ID:'62',
SUPERUNITID:'500',
UNITSEQ:'500,003',
ID:'003',
UNITNAME:'东区分⾏',
UNITID:'003',
ISLEAF:false,
},
{
INSTU_CDE:'0000',
ORG_NAME:'南区分⾏',
LEVELUNIT:'2',
APP_ID:'62',
SUPERUNITID:'500',
UNITSEQ:'500,004',
ID:'004',
UNITNAME:'南区分⾏',
UNITID:'004',
ISLEAF:false,
},
{
INSTU_CDE:'0000',
ORG_NAME:'北京分⾏',
LEVELUNIT:'2',
APP_ID:'62',
SUPERUNITID:'500',
UNITSEQ:'500,005',
ID:'005',
UNITNAME:'北京分⾏',
UNITID:'005',
ISLEAF:false,
ISLEAF:false,
},
{
INSTU_CDE:'0000',
ORG_NAME:'天津分⾏', LEVELUNIT:'2',
APP_ID:'62', SUPERUNITID:'500', UNITSEQ:'500,006',
ID:'006',
UNITNAME:'天津分⾏', UNITID:'006',
ISLEAF:false,
},
{
INSTU_CDE:'0000',
ORG_NAME:'海南银⾏', LEVELUNIT:'2',
APP_ID:'62', SUPERUNITID:'500', UNITSEQ:'500,100001',
ID:'100001',
UNITNAME:'海南银⾏', UNITID:'100001',
ISLEAF:false,
},
{
INSTU_CDE:'0000',
ORG_NAME:'北京分公司', LEVELUNIT:'2',
APP_ID:'62', SUPERUNITID:'500', UNITSEQ:'500,800',
ID:'800',
UNITNAME:'北京分公司', UNITID:'800',
ISLEAF:false,
},
{
INSTU_CDE:'0000',
ORG_NAME:'上海分公司', LEVELUNIT:'2',
APP_ID:'62', SUPERUNITID:'500', UNITSEQ:'500,900',
ID:'900',
UNITNAME:'上海分公司', UNITID:'900',
ISLEAF:false,
},
{
INSTU_CDE:'0000',
ORG_NAME:'三亚分⾏', LEVELUNIT:'3',
APP_ID:'62', SUPERUNITID:'100001', UNITSEQ:'500,100001,100002', ID:'100002',
UNITNAME:'三亚分⾏', UNITID:'100002',
ISLEAF:true,
},
];
let splitArr =[
{
INSTU_CDE:'0000',
INSTU_CDE:'0000',
ORG_NAME:'南区分⾏',
LEVELUNIT:'2',
APP_ID:'62',
SUPERUNITID:'500',
UNITSEQ:'500,004',
ID:'004',
UNITNAME:'南区分⾏',
UNITID:'004',
ISLEAF:false,
},
{
INSTU_CDE:'0000',
ORG_NAME:'北京分⾏',
LEVELUNIT:'2',
APP_ID:'62',
SUPERUNITID:'500',
UNITSEQ:'500,005',
ID:'005',
UNITNAME:'北京分⾏',
UNITID:'005',
ISLEAF:false,
},
];
console.log('原始数组对象', array);
// 输出 --------> 原始数组对象 [{"INSTU_CDE":"0000","ORG_NAME":"北区分⾏","LEVELUNIT":"2","APP_ID":"62","SUPERUNITID":"500","UNITSEQ":"500, 002","ID":"002","UNITNAME":"北区分⾏","UNITID":"002","ISLEAF":false},{"INSTU_CDE":"0000","ORG_NAME":"东区分⾏","LEVELUNIT":"2","APP_ID":"62"," SUPERUNITID":
"500","UNITSEQ":"500,003","ID":"003","UNITNAME":"东区分⾏","UNITID":"003","ISLEAF":false},{"INSTU_CDE":"0000","ORG_NAME":"南区分⾏","LEVELUNIT":"2","APP_ID":"62","SUPERUNITID":"500","UNITSEQ":"500,004","ID":"004","UNITNAME":"南区分⾏","UNITID":"004","ISLEAF":false},{"IN STU_CDE":"0000","ORG_NAME":"北京分⾏","LEVELUNIT":"2","APP_ID":"62","SUPERUNITID":"500","UNITSEQ":"500,005","ID":"005","UNITNAME":"北京分⾏","UNITID":"005","ISLEAF":false},{"INSTU_CDE":"0000","ORG_NAME":"天津分⾏","LEVELUNIT":"2","APP_ID":"62","SUPERUNITID":"500","UNITSEQ":"50 0,006","ID":"006","UNITNAME":"天津分⾏","UNITID":"006","ISLEAF":false},{"INSTU_CDE":"0000","ORG_NAME":"海南银⾏","LEVELUNIT":"2","APP_ID":"62" ,"SUPERUNITID":"500","UNITSEQ":"500,100001","ID":"100001","UNITNAME":"海南银⾏","UNITID":"100001","ISLEAF":false},{"INSTU_CDE":"0000","ORG_ NAME":"北京分公司","LEVELUNIT":"2","APP_ID":"62","SUPERUNITID":"500","UNITSEQ":"500,800","ID":"800","UNITNAME":"北京分公司","UNITID":"800","IS LEAF":false},{"INSTU_CDE":"0000","ORG_NAME":"上海分公司","LEVELUNIT":"2","APP_ID":"62","SUPERUNITID":"500","UNITSEQ":"500,900","ID":"900","U NITNAME":"上海分公司","UNITID":"900","ISLEAF":false},{"INSTU_CDE":"0000","ORG_NAME":"三亚分⾏","LEVELUNIT":"3","APP_ID":"62","SUPERUNITID": "100001","UNITSEQ":"500,100001,100002","ID":"100002","UNITNAME":"三亚分⾏","UNITID":"100002","ISLEAF":true}]
console.log('被删除数组对象', splitArr);
// 输出 ---------> 被删除数组对象 [{"INSTU_CDE":"0000","ORG_NAME":"南区分⾏","LEVELUNIT":"2","APP_ID":"62","SUPERUNITID":"500","UNITSEQ":"5 00,004","ID":"004","UNITNAME":"南区分⾏","UNITID":"004","ISLEAF":false},{"INSTU_CDE":"0000","ORG_NAME":"北京分⾏","LEVELUNIT":"2","APP_ID":"62 ","SUPERUNITID":"500","UNITSEQ":"500,005","ID":"005","UNITNAME":"北京分⾏","UNITID":"005","ISLEAF":false}]
// 从array中删除 splitArr
const newArray =[];
newarray = array.filter(arrItem =>{
// 到被删除数组对象中的主键,此处是 ID
const splitArrList = splitArr.map(splitArrItem =>{
return splitArrItem['ID'];
filter过滤对象数组});
return!splitArrList.includes(arrItem['ID']);
});
console.log('删除后数组对象', newarray);
// 输出 -----------> 删除后数组对象 [{"INSTU_CDE":"0000","ORG_NAME":"北区分⾏","LEVELUNIT":"2","APP_ID":"62","SUPERUNITID":"500","UNITSEQ": "500,002","ID":"002","UNITNAME":"北区分⾏","UNITID":"002","ISLEAF":false},{"INSTU_CDE":"0000","ORG_NAME":"东区分⾏","LEVELUNIT":"2","APP_ID":" 62","SUPERUNITID":"500","UNITSEQ":"500,003","ID":"003","UNITNAME":"东区分⾏","UNITID":"003","ISLEAF":false},{"INSTU_CDE":"0000","ORG_NAME":"天津分⾏","LEVELUNIT":"2","APP_ID":"62","SUPERUNITID":"500","UNITSEQ":"500,006","ID":"006","UNITNAME":"天津分⾏","UNITID":"006","ISLEAF":false} ,{"INSTU_CDE":"0000","ORG_NAME":"海南银⾏","LEVELUNIT":"2","APP_ID":"62","SUPERUNITID":"500","UNITSEQ":"500,100001","ID":"100001","UNITNA ME":"海南银⾏","UNITID":"100001","ISLEAF":false},{"INSTU_CDE":"0000","ORG_NAME":"北京分公司","LEVELUNIT":"2","APP_ID":"62","SUPERUNITID":"50 0","UNITSEQ":"500,800","ID":"800","UNIT
NAME":"北京分公司","UNITID":"800","ISLEAF":false},{"INSTU_CDE":"0000","ORG_NAME":"上海分公司","LEVELU NIT":"2","APP_ID":"62","SUPERUNITID":"500","UNITSEQ":"500,900","ID":"900","UNITNAME":"上海分公司","UNITID":"900","ISLEAF":false},{"INSTU_CDE":" 0000","ORG_NAME":"三亚分⾏","LEVELUNIT":"3","APP_ID":"62","SUPERUNITID":"100001","UNITSEQ":"500,100001,100002","ID":"100002","UNITNAME":"三亚分⾏","UNITID":"100002","ISLEAF":true}]
</script>
<script>
var a =[
{
id:1,
name:'hong',
},
},
{
id:2,
name:'huang',
},
{
id:3,
name:'lan',
},
];
var b =[
{
id:1,
name:'hong',
},
{
id:2,
name:'lan',
},
{
id:4,
name:'lv',
},
];
b.forEach(item =>{
a = a.filter(a => a.id != item.id);
});
console.log(JSON.stringify(a));
// [{"id":3,"name":"lan"}]
</script>
</body>
</html>
可以直接复制运⾏代码,查看打印结果
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论