Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
488 views
in Technique[技术] by (71.8m points)

javascript - 为什么Array.indexOf()可以与一个redux动作一起正常工作,而不能与另一个动作一起正常工作? (相同的减速器)(Why is Array.indexOf() working correctly with one redux action but not with another? (same reducer))

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

string mathes one of the array items, but the indexOf() function still returns -1

(字符串运算是数组项之一,但是indexOf()函数仍返回-1)

It doesn't match one of the array items as of when you're calling indexOf , because if it did, indexOf would find it.

(调用indexOf ,它与数组项之一匹配,因为如果匹配,则indexOf会找到它。)

indexOf isn't broken .

(indexOf 没有坏 。)

So that leaves two general possibilities:

(这样就留下了两种一般可能性:)

  1. The string that's in the array looks like it should match, but is slightly different from the string in the payload .

    (数组中的字符串看起来应该匹配,但与payload的字符串略有不同。)

    Ways it might be different that may not be obvious:

    (可能并不明显的不同方式:)

    • One of them (the one in the array or the one in payload ) may have a space at the beginning or end, etc.

      (其中一个(数组中的一个或payload的一个)可能在开头或结尾处都有空格,等等。)

    • One of them may have a slight difference in capitalization.

      (其中之一可能在大小写上略有不同。)

    • They may have slightly different characters that are easily confused, like ? and c? .

      (它们可能具有容易混淆的略有不同的字符,例如?c? 。)

    • The entry in the array may have a comma in it, for instance "thisTag,thatTag" which won't, of course, match "thatTag" if you go looking, although it looks like "thatTag" is in the array.

      (数组中的条目中可能包含逗号,例如"thisTag,thatTag" ,尽管看起来像"thatTag"在数组中,但如果您去看,当然不会与"thatTag"匹配。)

    • One of them may be a String object where the other is a string primitive;

      (其中一个可以是String对象,另一个可以是字符串基元。)

      String objects and string primitives are not === to each other, and indexOf uses a === test.

      (String对象和字符串基元彼此不是=== ,并且indexOf使用===测试。)

  2. The string isn't in the selectedTags array as of when you call indexOf , even though it looks like it in the console.

    (截至调用indexOf ,该字符串已不在selectedTags数组中,即使在控制台中看起来也是如此。)

    That's because of this feature of consoles .

    (这是因为consoles这一功能 。)

To figure it out, set a breakpoint on the indexOf line and, when it's hit, look at the string in payload and the string in selectedTags (if it's there — if not, then it was the console thing).

(为了弄清楚它,在indexOf行上设置一个断点,当命中它时,查看payload的字符串和selectedTags的字符串(如果有的话-如果不是,那是控制台的事)。)

You'll find some difference.

(您会发现一些区别。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...