繁体   English   中英

从数组对象中删除项目

[英]Remove item from object of arrays

我有一个使用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
        }
    ]    
}

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

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

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>
        );
    })
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM