从数组对象中删除项目

时间:2018-03-16 17:33:15

标签: javascript reactjs redux

我有一个使用Redux的react应用程序。现在我有一个与redux一起显示的书籍列表,并希望实现CRUD。列出书籍的代码是:

listBooks(){
        return this.props.books.map((books) => {
            return(
                <tbody key={books.title}>
                    <tr className="tr-book">
                        <td>{books.series}</td>
                        <td>{books.title}</td>
                        <td>Vol. {books.volume}</td>
                        <td>
                            <button onClick={() => deleteBook(this.props.books, books.id)}className="btn-action">Delete</button>
                            <button onClick={() => editBook(this.props.books)}className="btn-action">Update</button>
                        </td>
                    </tr>
                </tbody>
            );
        })
    }

它列出的很好。 deleteBook操作具有以下操作:

export function deleteBook (book, id) {
    book = book.filter(function(book){
        return book.id !== id 
    });
    console.log(book.id);
    return {
        type: "BOOK_DELETED",
        payload: book
    }
}

它不起作用。我已经尝试了一些方法,但是大多数方法都没有用,因为book不是数组,而是一个Object of Arrays。在这种情况下,如何告诉函数deleteBook过滤这些图书并仅返回book.id !== id

更新:以下是图书的设置位置:

export default function listBooks() {
    return[
        {
            id: 1,
            volume: 1,
            series: 'A Song of Ice and Fire',
            title: 'Game of Thrones',
            rank: 0
        },
        {
            id: 2,
            volume: 2,
            series: 'A Song of Ice and Fire',
            title: 'Clash of Kings',
            rank: 0
        },
        {
            id: 3,
            volume: 3,
            series: 'A Song of Ice and Fire',
            title: 'Storm of Swords',
            rank: 0
        },
        {
            id: 4,
            volume: 4,
            series: 'A Song of Ice and Fire',
            title: 'A Feast of Crows',
            rank: 0
        }, {
            id: 5,
            volume: 5,
            series: 'A Song of Ice and Fire',
            title: 'A Dance With Dragons',
            rank: 0
        }, {
            id: 6,
            volume: 1,
            series: 'The Lord of the Rings',
            title: 'The Fellowship of the Ring',
            rank: 0
        }, {
            id: 7,
            volume: 2,
            series: 'The Lord of the Rings',
            title: 'The Two Towers',
            rank: 0
        }, {
            id: 8,
            volume: 3,
            series: 'The Lord of the Rings',
            title: 'The Return of the King',
            rank: 0
        }
    ]    
}

1 个答案:

答案 0 :(得分:1)

您的图书是一个对象的数组,而不是数组的对象

其次,您必须过滤要在减速器中删除的书而不是动作,因此您的减速器看起来像

export function booksReducer (state = initialState, action) {

    switch(action.type) {
       ...
       case 'DELETE_BOOK': return state.filter(function(book){
          return book.id !== action.id 
       });
       ...
    }
}

你的行动将是

export function deleteBook (id) {

    return {
        type: "DELETE_BOOK",
        payload: id
    }
}

并调用

之类的操作
listBooks(){
    return this.props.books.map((books) => {
        return(
            <tbody key={books.title}>
                <tr className="tr-book">
                    <td>{books.series}</td>
                    <td>{books.title}</td>
                    <td>Vol. {books.volume}</td>
                    <td>
                        <button onClick={() => deleteBook(books.id)}className="btn-action">Delete</button>
                        <button onClick={() => editBook(this.props.books)}className="btn-action">Update</button>
                    </td>
                </tr>
            </tbody>
        );
    })
}
相关问题