Reactjs 反应更新处于状态的数组

Reactjs 反应更新处于状态的数组,reactjs,react-native,Reactjs,React Native,我正在使用react原生贴图并尝试绘制多条多段线。我已将此结构中的点分组到状态: var groups = [{ Key: 'YELLOW', Points: [], }] 现在我想将lat/lng对象放入点数组中,但它给出了对象不可扩展的错误。我尝试使用[…this.state.groups]扩展attibutes,但我无法在其中添加点 我有一个单独的数组,它包含一个类似的结构,其中包含所有的点数据。这里的概念是,我需要将数据推送到该组数据结构中,每个点都有延迟,以便多段线/

我正在使用react原生贴图并尝试绘制多条多段线。我已将此结构中的点分组到状态:

var groups = [{
    Key: 'YELLOW',
    Points: [],
}]
现在我想将lat/lng对象放入点数组中,但它给出了对象不可扩展的错误。我尝试使用[…this.state.groups]扩展attibutes,但我无法在其中添加点


我有一个单独的数组,它包含一个类似的结构,其中包含所有的点数据。这里的概念是,我需要将数据推送到该组数据结构中,每个点都有延迟,以便多段线/管线绘制缓慢。

假设您具有以下结构

const groups = [{Key: 'YELLOW',Points: [],},{Key: 'BLUE',Points: [],}]
您想将一对
lat/lng
推入每个
,这可以通过执行以下操作来实现

const pushPoint = point =>{
    const updatedGroups = group.map(group =>({
        ...group,
        Points : group.Points.concat(point)
    }))

    return updatedGroups
}

this.setState({groups : this.pushPoint(this.state.groups)})
您需要
扩展原始组,并通过返回一个新数组来覆盖
属性,该数组是前一个
和新的

串联的结果

 const totsl= [{key: 'YELLOW',Points: [],},{Key: 'BLUE',Points: [30,40],}],
要使用此.state.groups,您应该

state={groups = [{
    Key: 'YELLOW',
    Points: [],
}};

totsl.Points.map(item=>
this.setState({this.state.groups.Points:item})
to add in the state Points it should b use this syntax this.state({groups :{points:value"}}
,


告诉我它是否工作或仍然有问题

我以这种方式使用了splice和concat功能:

添加

this.setState((state) => {

    var newArray = [...state.cordinates, {
        latitude: point.latitude,
        longitude: point.longitude,
    }];

    return {
        cordinates: newArray,
    };

});
删除:

this.setState((state) => {

    var newArray = [...this.state.cordinates];

    newArray.splice(-1, 1);

    return {
        cordinates: newArray,
    };

});

尝试
groups[0]。Points.concat(新值)
@ravibagul91删除元素怎么样?我试过拼接,没用。检查这个-还有这个-移除元件怎么样?我试过拼接,但没用。使用过滤器。示例totsl.Points.filter(item=>item.id1!==id)删除元素怎么样?我试过了,没用。你应该用过滤器
array.filter(x=>x.id!==idToRemove)