【发布时间】:2021-04-23 21:21:50
【问题描述】:
我正在尝试使用 react native redux 开发一个动态 DraggableFlatList,其中来自 onDragEnd 的更新数组被分派到商店。因此,我试图创建一个函数,在该函数中,我使用 onDragEnd 的返回对象中的“from”和“to”参数在调度之前更改一个新数组。例如,在下面的对象中,我使用了三个项目,它们是数组中的对象:
Object {
"data": Array [
Object {
"backgroundColor": "rgb(154, 0, 132)",
"category": "Practical",
"description": "Zero",
"duedate": Object {
"cond": false,
"date": "",
},
"id": 0.7945943069813785,
"iterations": "",
"key": "0",
},
Object {
"backgroundColor": "rgb(120, 5, 132)",
"category": "Practical",
"description": "One",
"duedate": Object {
"cond": false,
"date": "",
},
"id": 0.8857539547977513,
"iterations": "",
"key": "1",
},
Object {
"backgroundColor": "rgb(184, 10, 132)",
"category": "Practical",
"description": "Two ",
"duedate": Object {
"cond": false,
"date": "",
},
"id": 0.11232602853449736,
"iterations": "",
"key": "2",
},
],
"from": 2,
"to": 1,
}
在这里,我希望描述为“二”的对象与描述为“一”的对象交换位置。这些键无关紧要,因为我在渲染期间为它们提供了新的键。 到目前为止,正在执行替换的函数如下所示:
const dragComplete = (item) => {
let itemArray = item.data;
// from object is going to be replaced with to object and visa vreca
let indexFrom = item.from;
let indexTo = item.to;
let objMovesFrom = itemArray[indexFrom];
let objMovesTo = itemArray[indexTo];
let sortedArray = itemArray;
console.log('Object moves from : ' + objMovesFrom.description);
console.log('Obejct moves to : ' + objMovesTo.description);
sortedArray.map((task, i) => {
if ((i = indexFrom)) {
sortedArray.splice(indexFrom, 1, objMovesTo);
}
if ((i = indexTo)) {
sortedArray.splice(indexTo, 1, objMovesFrom);
}
});
console.log(item);
//dispatch(setTaskList(item.data));
};
我还没想明白... 感谢您提供有用的答案!
【问题讨论】:
-
map不会更改数组,而是返回一个新数组。
标签: javascript arrays react-native react-native-flatlist