我所在的州有一批发货对象:
shipments: [
{
containerSize: '',
numberOfContainer: '',
grossWeight: '',
grossWeightUnit: '',
dimension: '',
dimensionUnit: '',
commodityType: '',
commodity: '',
temperature: '',
temperatureUnit: ''
}
],
my function to remove:
removeField = (e, idx) => {
e.preventDefault();
console.log(idx);
console.log(this.state.shipments);
console.log(this.state.shipments[idx]);
let removedArray = [ ...this.state.shipments ];
console.log(removedArray);
removedArray.splice(idx, 1);
this.setState({
shipments: removedArray
});
};
我想使用拼接功能删除特定对象
splice(id,1)
。但是它总是删除最后一个对象。
答案 0 :(得分:0)
尝试lodash的reject
:
import {reject} from 'lodash';
const newShipment = reject(shipments, (item,index) => {
return index === myIndex; // some condition here
});