0

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)
}
2
  • so you want to filter curObject and separate data based on some logic and you need two different result array? Commented Jun 15, 2020 at 15:59
  • i have a temp = current state, modify temp, save back to the state, flow. i want to remove id 1 from 2020-06-15:[] and I want to move id 1 to 2020-06-01:[].. but via variables, so lets say selectedDate and selectedId. Commented Jun 15, 2020 at 16:02

1 Answer 1

1

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>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.