【问题标题】:Remove item from object of arrays从数组对象中删除项目
【发布时间】:2018-03-16 17:33:15
【问题描述】:

我有一个使用 Redux 的反应应用程序。现在我有一个与 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 不是数组,而是数组对象。在这种情况下,我如何告诉函数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
        }
    ]    
}

【问题讨论】:

  • 书籍的结构是什么,不知道你说的数组对象是什么意思
  • 同意上面的Shubham,书籍的初始状态是什么?它的数据结构是什么?
  • 添加了一些新代码@ShubhamKhatri
  • 该操作不应修改图书。你必须写一个reducer来做,当它把动作作为参数时

标签: javascript reactjs redux


【解决方案1】:

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

其次,您必须过滤要在 reducer 中删除的书,而不是 action,因此您的 reducer 看起来像

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

【讨论】:

    猜你喜欢
    • 2021-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多