我的问题很快。这是问题所在:

//Gets string from database in "User1|User2|User3" format
var frArray = res[0].friendRequests.split('|');

//frArray should now equal ['User1', 'User2', etc]

//data.friend is a string for the friend we are removing from requests
//let's assume it's User1

console.log(frArray.indexOf(data.friend)); //This prints 0

console.log(frArray); //This prints User1 which is correct

frArray = frArray.splice(frArray.indexOf(data.friend), 1);

console.log(frArray);
//This prints User1 STILL which is not correct it should've removed it


我将不胜感激,将不胜感激。我已经坚持了一段时间。谢谢

最佳答案

splice返回删除的元素的数组。只需删除赋值,以便您的变量继续引用原始数组,并在适当位置进行修改:

frArray.splice(frArray.indexOf(data.friend), 1);


例:



const frArray = ["Jane", "Mohammed", "An"];
const removed = frArray.splice(0, 1);
console.log(`removed: ${JSON.stringify(removed)}`);
console.log(`frArray: ${JSON.stringify(frArray)}`);

关于javascript - 为什么Javascript的Splice函数不起作用?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56399182/

10-08 23:14