警告
本文最后更新于 2019-07-31,文中内容可能已过时。
思路就是先将json转换成数组,然后在循环遍历数组,然后再用splice函数进行删除数组
1
2
3
4
5
6
7
8
9
10
11
12
13
|
let delId = 3;
let jsonArr = ['{"userList": [{"name": "yqchilde", "userId": "2", "joinTime": "07月31日"}, {"name": "test404", "userId": "3", "joinTime": "07月30日"}], "className": "计算机科学一班"}']
jsonArr = JSON.parse(jsonArr);
for (let i = 0; i < jsonArr.userList.length; i++) {
if (delId == jsonArr.userList[i].userId) {
jsonArr.userList.splice(i, 1);
}
};
jsonArr = JSON.stringify(jsonArr);
console.log(jsonArr)
//结果如下
// {"userList":[{"name":"yqchilde","userId":"2","joinTime":"07月31日"}],"className":"计算机科学一班"}
|