简体   繁体   English

在 object,React / Native 中处理从嵌套数组中重新/移动单个项目的最佳方法

[英]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.我可以访问日期和 ID,并且对 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.但后来我对 {value} 是一个数组以及如何过滤/忽略这个项目有点困惑。

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. tldr:每个元素的名字是一个“YYYY-MM-DD”的日期,这个元素是一个数组,每个数组只包含id,name。

trying to 'delete' an item in the array in one case, or 'move' the item to a different "YYYY-MM-DD".尝试在一种情况下“删除”数组中的项目,或将项目“移动”到不同的“YYYY-MM-DD”。

edit: looking for a function approach编辑:寻找 function 方法

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,它将从一个数组中删除一个元素,可选择将其添加到另一个数组中。

 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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