var tags = ['tag1', 'tag2', 'tag3', 'tag4', 'tag5',
'tag6', 'tag7', 'tag8', 'tag9', 'tag0'];
var selectedTags = [];
const tagsReducer = (state={}, action) => {
var tagIndex = '';
if(action.type==="ADD_TAG") { //this if statement works as expected
tagIndex = tags.indexOf(action.payload);
selectedTags.push(tags[tagIndex]);
tags.splice(tagIndex, 1);
return {tags, selectedTags};
}
if(action.type==="REMOVE_TAG"){ //this doesn't
tagIndex = selectedTags.indexOf(action.payload);
console.log(selectedTags); //prints the expected array
console.log(action.payload); //prints the expected string
console.log(tagIndex); //prints -1 (doesn't find the string)
console.log(typeof(selectedTags)); //prints object
console.log(typeof(action.payload)); // prints string
tags.push(selectedTags[tagIndex]);
selectedTags.splice(tagIndex, 1);
return {tags, selectedTags};
}
return {tags, selectedTags}
}
The string mathes one of the array items, but the indexOf() function still returns -1.
(字符串对数组项之一进行数学运算,但是indexOf()函数仍返回-1。)
It's confusing because the first action works fine, but the second doesn't.(这很令人困惑,因为第一个操作可以正常运行,但是第二个则不能。)
Any suggestions?(有什么建议么?)
ask by redmaster translate from so
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…