In my project, I am handling a JSON string that is stored in a Redis key. Whenever a user is added to another user's list, an object is added to this key. The dilemma I face is how to efficiently remove the object from the key where the 'name' property matches the value to be removed.
[{"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":[]} ]
After retrieving the result from Redis and parsing it into allFriends
, I also set a variable called exFriend
which stores one of the name properties.
Now, my main question arises: is there a way to eliminate the object with the name property matching "srtyt1"
? Or will I need to rethink my data structure? While exploring solutions, I came across a loop in the Mozilla docs for maps, but unfortunately, it seems this approach does not work seamlessly with objects.
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;
}
}