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'
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.
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:
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]]
})
})
},