简体   繁体   中英

Redux Update array inside array

Currently I have a store state like this:

 const initState = [ devices: { id: 0, data: [] } ]

I would like to insert a string into the data array. And if the array reaches a certain length, clear it. How can I do that? This is what I have currently but it's not working

 return state.map((device) => { if (device.instanceId !== action.payload.instanceId) { return device; } else { if (device.bytestreamData.length < CACHE_SIZE) { var newArray = [...device.bytestreamData]; newArray.push(action.payload.bytestreamData); return newArray; } else { var newArray = [...device.bytestreamData]; newArray.length = 0; newArray.push(action.payload.bytestreamData); return newArray; } } });

Thank you very much

I'm assuming you want to add the new string to an empty array when you reach the max-size of the array

  if (device.bytestreamData.length === CACHE_SIZE) {
    var newArray = [];
    newArray.push(action.payload.bytestreamData);
    return newArray;
  } else {
    var newArray = device.bytestreamData.slice();
    newArray.push(action.payload.bytestreamData);
    return newArray;
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM