<分区>
我有一个存储在 Redis 键中的 JSON 字符串。每当将用户添加到另一个用户列表时,我都会向该键添加一个对象。如何删除名称属性与要删除的值匹配的对象。
[{"name":"srtyt1","wins":0,"losses":0,"levels":0,"color":1672960,"avatar":[]},
{"name":"srtyt2","wins":0,"losses":0,"levels":0,"color":1672960,"avatar":[]},
{"name":"srtyt3","wins":0,"losses":0,"levels":0,"color":1672960,"avatar":[]} ]
上面的字符串是 Redis 的结果,我正在将其解析为 allFriends
。我还有一个变量 exFriend
,它将保存名称属性之一的值。
有没有办法删除 name 属性等于 "srtyt1"
的对象?或者我需要重组我的数据吗?我在 Mozilla map 文档中看到了这个循环,但我猜它不适用于对象?
let allFriends = JSON.parse(result);
//iterate through all friends until I find the one to remove and then remove that index from the array
for (let [index, friend] of allFriends) {
if (friend.name === exFriend) {
//remove the friend and leave
allFriends.splice(index, 1);
break;
}
}