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)
}