删除包含特定索引对象的数组的问题

时间:2019-05-17 11:23:07

标签: javascript arrays reactjs splice

我所在的州有一批发货对象:

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)。但是它总是删除最后一个对象。

1 个答案:

答案 0 :(得分:0)

尝试lodash的reject

import {reject} from 'lodash';

const newShipment =  reject(shipments, (item,index) => {
  return index === myIndex; // some condition here
});