使用整数时,Javascript indexOf不是函数

时间:2017-07-31 07:45:52

标签: javascript

我有以下数组(只是一个简单的例子):

books:[
            {id: 1, isbn: '0226020452', title: 'The Bridge on the Drina', author: 'Ivo Andrić',
            description: '',
                added: '2017-07-29'
            },
            {id: 2, isbn: '9781628724189', title: 'Bosnian Chronicle: A Novel', author: 'Ivo Andrić',
                description: '',
                added: '2017-07-29'
            },
            {id: 3, isbn: '9781856100229', title: 'Damned Yard and Other Stories', author: 'Ivo Andrić',
                description: '',
                added: '2017-07-29'
            },

当我尝试执行以下功能时,我收到此错误未捕获的TypeError:book.id.indexOf不是函数

updateBook: function (state, updateBook) {

    let foundBook = state.books.filter((book) => {
            return book.id.indexOf(parseInt(updateBook.id, 10)) >= 0;
    })

    console.log(foundBook);

    Object.assign(state.book[foundBook], updateBook);
   }

这里发生了什么?

1 个答案:

答案 0 :(得分:1)

您可以使用Array#findIndex获取数组的索引。

SET !EXTRACT_TEST_POPUP NO
SET !EXTRACT NULL
TAG POS=1 TYPE=SELECT ATTR=id:ctl00_ContentPlaceHolder1_ddltype&&TXT:* EXTRACT=HTM
SET values1 EVAL("var v = '{{!EXTRACT}}'.match(/value=\"(.+?)\"/g).toString().replace(/value=\"|\"/g, '').split(','); v.shift(); v;")
PROMPT {{values1}}

SET !EXTRACT NULL
TAG POS=1 TYPE=SELECT ATTR=id:ctl00_ContentPlaceHolder1_ddlReportOn&&TXT:* EXTRACT=HTM
SET values2 EVAL("var v = '{{!EXTRACT}}'.match(/value=\"(.+?)\"/g).toString().replace(/value=\"|\"/g, '').split(','); v.shift(); v;")
PROMPT {{values2}}