I'm using react-native cli to build application. I have store required data in variable. For delete unwanted cart, I have pass the json object that bind with the element. Unfortunately, the data is not deleted.
My data:-
[
{
"ProdID":306,
"ProdName":"Freezing Choc",
"ProdImage":"./Source/Product/2_20190131_101857.png",
"ProdVar":[
],
"Quantity":1
},
{
"ProdID":307,
"ProdName":"Bandung",
"ProdImage":"./Source/Product/3_20190131_101912.png",
"ProdVar":[
],
"Quantity":1
},
{
"ProdID":308,
"ProdName":"Hawaiian Breeze",
"ProdImage":"./Source/Product/4_20190131_102006.png",
"ProdVar":[
],
"Quantity":3
}
]
My Script:-
removeBtns(item) {
return [
{
text: <Icon name='trash' style={{ fontSize: 15 }} />,
onPress: () => this.Func_RemoveSelectedCart(item),
type: 'delete',
}
]
}
Func_RemoveSelectedCart = async (item) => {
try {
console.log('ITEM TO REMOVE: ' + JSON.stringify(item));
await this._onAwaitAlert().then((response) => {
if (response === 'OK') {
delete appGlobal.ObjProduct[item];
console.log('AFTER REMOVE: ' + JSON.stringify(appGlobal.ObjProduct));
}
})
.catch(e => {
console.log('promise catch ' + e);
});
} catch (e) {
console.log('Unable to remove selected cart. ' + e);
}
}
renderProductList() {
var contents = this.state.prodData.map((item) => {
return (
<Content key={item.ProdID}>
<Swipeout right={this.removeBtns(item)}>
<Card>
<CardItem>
.
.
.
.
</CardItem>
</Card>
</Swipeout>
</Content>
);
});
return (
<Content padder>
{contents}
</Content>
)
}
Let's say I want to remove second item from cart. On console.log, it wrote:-
ITEM TO REMOVE: {"ProdID":307,"ProdName":"Bandung","ProdImage":"./Source/Product/3_20190131_101912.png","ProdVar":[],"Quantity":1}
AFTER REMOVE: [{"ProdID":306,"ProdName":"Freezing Choc","ProdImage":"./Source/Product/2_20190131_101857.png","ProdVar":[],"Quantity":1},{"ProdID":307,"ProdName":"Bandung","ProdImage":"./Source/Product/3_20190131_101912.png","ProdVar":[],"Quantity":1},{"ProdID":308,"ProdName":"Hawaiian Breeze","ProdImage":"./Source/Product/4_20190131_102006.png","ProdVar":[],"Quantity":3}]
I have tried based from search engine result on forum, discussion, tutorial:-
1: delete appGlobal.ObjProduct[item]; Nothing happen
2: appGlobal.ObjProduct.delete(item); Exception delete is not a function
3: appGlobal.ObjProduct.splice(item); It remove every data in ObjProduct
I cannot use delete using ProdID because there is variant/options that can having same ProdID but different variant/options. Example:-
[
{
"ProdID":301,
"ProdName":"Mango Punch",
"ProdImage":"./Source/Product/1_20190131_1212300.png",
"ProdVar":[],
"Quantity":1
},
{
"ProdID":305,
"ProdName":"Berrysome-Go",
"ProdImage":"./Source/Product/1_20190131_101800.png",
"ProdVar":[{
"PVID":131,
"PVName":"Name 1"
}],
"Quantity":3
},
{
"ProdID":305,
"ProdName":"Berrysome-Go",
"ProdImage":"./Source/Product/1_20190131_101800.png",
"ProdVar":[{
"PVID":132,
"PVName":"Name 2"
}],
"Quantity":2
},
{
"ProdID":305,
"ProdName":"Berrysome-Go",
"ProdImage":"./Source/Product/1_20190131_101800.png",
"ProdVar":[{
"PVID":133,
"PVName":"Name 3"
}],
"Quantity":7
}
]
Array.prototype.findIndex()method to get the index of the element to use indelete appGlobal.ObjProduct[index];delete appGlobal.ObjProduct[item].itemis an object, not an array index.