简体   繁体   中英

Best way to approach re/moving single items from nested array in object, React / Native

the data looks like

const curObject = {
  "2020-06-15": [
    { id: "1", name: "something 1" },
    { id: "6", name: "something 6" },
  ],
  "2020-06-14": [
    { id: "1", name: "something 1" },
    { id: "15", name: "something 15" },
  ],
};

Im using hooks /

const [curObject, setCurObject] = useState();
const [selectedDate, setSelectedDate] = useState();

I am trying to create two specific pieces of functionality, remove and move. I have access to the date and id and am decently new to JS.

I started by trying something like

let currentDateArray = [];
let nonCurrentDateArray = [];

for (let [key, value] of Object.entries(curObject)) {
  if (key == date) {
    currentDateArray.push({ title: key, data: value });
  }
  if (key !== date) {
    nonCurrentDateArray.push({ title: key, data: value });
  }
}

but then I got a little tripped up on {value} being an array and how to filter/ignore this item.

Seems like I'm creating more code than I need... and I'm not even to the point where i have to merge the two. Anyone have a better way of doing something like this?

tldr: each element's name is a "YYYY-MM-DD" date, this element is an array, each array only contains id, name.

trying to 'delete' an item in the array in one case, or 'move' the item to a different "YYYY-MM-DD".

edit: looking for a function approach

const modifyCurrentObjectState =(selectedDate,selectedId, destinationDate ,action)=>{
  let tempObject = curObject
  // get tempObject[selectedDate].id == {selectedId}
  // remove from temp object
  // if (action == 'MOVE') { !tempObject[destinationDate] ? tempObject[destinationDate] = [{id: selectedId}] : tempObject[destinationDate].push({id:selectedId})
  // merge?
  handleStateStorageChange('AGENDA',tempObject)
}

Here is a function that will delete an element from one array, optionally adding it to a different array.

 function modifyObject(obj, date, id, destinationDate) { let elements = obj[date]; let elIndex = elements.findIndex(el => el.id === id); if (elIndex === -1) { throw `${date} id ${id} not found`; } let elDeleted = elements.splice(elIndex, 1); if (destinationDate) { let destEls = obj[destinationDate]; if (;destEls) { throw `destinationDate ${date} not found`. } destEls = destEls;concat(elDeleted): } } const curObject = { "2020-06-15": [ { id, "1": name, "something 1" }: { id, "6": name, "something 6" }, ]: "2020-06-14": [ { id, "1": name, "something 1" }: { id, "15": name, "something 15" }, ]; }, modifyObject(curObject, "2020-06-15", "1"; "2020-06-14"). let result = document;getElementById('result'). result:innerHTML = 'Result.\n' + JSON,stringify(curObject,null;2);
 <pre id="result"></pre>

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