简体   繁体   English

从数组中删除对象

[英]remove object from array in react

I have an array of objects saved in state and each of the objects in the array has another array on objects. 我有一个状态保存的对象数组,该数组中的每个对象在对象上都有另一个数组。

here is an example of a single object: 这是单个对象的示例:

            id       : uuid.v1(),
            area     : 'Scratch',
            name     : 'Untitled',
            status   : "",
            size     : "",
            created  : {
                name : "username",
                time : new Date()
            },
            modified : {
                name : "username",
                time : new Date()
            },
            content: [{
                id        : uuid.v1(),
                Content   : "Content 1",
                TowerRed  : "",
                TowerText : "text"
            },
            {
                id        : uuid.v1(),
                Content   : "Content 2",
                TowerRed  : "",
                TowerText : "text"
            }]

I need to be able to remove an object from inside the content array inside of these objects. 我需要能够从这些对象内部的内容数组中删除一个对象。

For example I need to remove the object containing 'content 1' without modifying the object containing 'content 2' 例如,我需要删除包含“内容1”的对象而不修改包含“内容2”的对象

I have tried to use the react immutability helpers her but with no success. 我试图使用反应不变性帮助者,但没有成功。

here I loop through the parent objects and the content objects until I find a match with the 'itemID' (itemID) is the id of the object i want to remove form the content array. 在这里,我遍历父对象和内容对象,直到找到与“ itemID”(itemID)匹配的对象,该对象是我要从内容数组中删除的对象的ID。

     var tickerIndex = null;
        var tickerItemIndex = null;

        for (var i = 0; i < this.state.scratchTickers.length; i++) {

            var tickersContent = this.state.scratchTickers[i].content;
            tickerIndex = i;

            for (var x = 0; x < tickersContent.length; x++) {
                if (tickersContent[x].id == itemID) {                   
                    tickerItemIndex = x;
                }           
            }
        }

then I create a reference to the parent object and its content array: 然后创建对父对象及其内容数组的引用:

var item = this.state.scratchTickers[tickerIndex].content;

Finally I use the update immuatbility help to splice the array and remove the correct object based on its itemID: 最后,我使用更新抗扰性帮助来拼接数组并根据其itemID删除正确的对象:

this.setState ({
        scratchTickers: update(item, {
            $splice: [[tickerItemIndex, 1]]
        })
    })

But this just doesn't seem to work. 但这似乎不起作用。

here is the whole function: 这是整个功能:

_deleteTickerItem: function(itemID) {
        var tickerIndex = null;
        var tickerItemIndex = null;

        for (var i = 0; i < this.state.scratchTickers.length; i++) {

            var tickersContent = this.state.scratchTickers[i].content;
            tickerIndex = i;

            for (var x = 0; x < tickersContent.length; x++) {
                if (tickersContent[x].id == itemID) {                   
                    tickerItemIndex = x;
                }           
            }
        }

        var item = this.state.scratchTickers[tickerIndex].content;

        this.setState ({
            scratchTickers: update(item, {
                $splice: [[tickerItemIndex, 1]]
            })
        })

    },

It looks like your setState call is overriding your scratchTickers data with just the value of one particular scratchTicker 's content . 看起来您的setState调用仅使用一个特定scratchTicker content的值覆盖了scratchTickers数据。

What you're essentially trying to do is map your array of scratchTickers to a new array with filter ed content. 实际上,您要做的是 scratchTickers数组scratchTickers到具有过滤内容的新数组。

_deleteTickerItem: function(itemID) {
  this.setState({
    scratchTickers: this.state.scratchTickers.map(scratchTicker =>
      Object.assign({}, scratchTicker, {
        content: scratchTicker.content.filter(content => content.id != itemID)
      })
    )
  });
}

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/map https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects / Array / filter https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/assign

Update: 更新:

When rendering, make sure if you're using an iterator that you're not using the array indices as keys, like this. 渲染时,请确保是否使用的迭代器未将数组索引用作键,例如这样。

// bad
{content.map((content, index) =>
  <p key={index}>{content.Content}</p>
)}

When React diffs with the virtual DOM on a change, it will look at the keys to determine what has changed. 当React对虚拟DOM进行更改时,它将查看键以确定已更改的内容。 So if you're using indices and there is one less in the array, it will remove the last one. 因此,如果您使用的是索引,但数组中少了一个,它将删除最后一个。 Instead, use the id 's of the content as keys, like this. 而是将contentid用作键,像这样。

// good
{content.map(content =>
  <p key={content.id}>{content.Content}</p>
)}

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

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