代码之家  ›  专栏  ›  技术社区  ›  Arsalan Khattak

为什么?文本。包括(“”)为真

  •  0
  • Arsalan Khattak  · 技术社区  · 8 年前

    我有一个包含对象的数组。

    let todos = [ {
        text: 'Order cat food',
        completed: true
    }, {
        text: 'Clean Kitchen',
        completed: false
    }, {
        text: 'Buy food',
        completed: false
    }, {
        text: 'Do work',
        completed: true
    }, {
        text: 'Exercise',
        completed: true
    }]
    

    我正在申请待办事项,结果出乎意料。我用下面的代码来理解发生了什么,它返回true。

    todos.forEach(element => {
        console.log(element.text.includes(''))
    });
    

    所以我的问题是为什么它对空字符串返回true?

    1 回复  |  直到 8 年前
        1
  •  0
  •   Juan Cortés    8 年前

    空字符串包含空字符串。 indexOf 像这样 "".indexOf("")

    注意如何 "" 两边都一样。 ,位于索引0上。