Javascript 对对象数组列表排序时的无限循环
因此,我试图通过单击标题对列进行排序,这会造成很大的损坏。我猜这是一个无限循环:) 尽管如此,当它是数组的数组而不是对象的数组时,同样的逻辑工作得很好。 都在redux reducer中 我的臭虫在哪里Javascript 对对象数组列表排序时的无限循环,javascript,infinite-loop,Javascript,Infinite Loop,因此,我试图通过单击标题对列进行排序,这会造成很大的损坏。我猜这是一个无限循环:) 尽管如此,当它是数组的数组而不是对象的数组时,同样的逻辑工作得很好。 都在redux reducer中 我的臭虫在哪里 const newData = an array of objects. newData[0] - { show: true, value: [5, 3, "quidem molestiae enim"] }
const newData = an array of objects.
newData[0] - {
show: true,
value: [5, 3, "quidem molestiae enim"]
}
newData[0].value - it's a table row with td.
const sortDataFunc = (state, action) => {
const newData = [].concat(action.data);
index = action.columnIndex;
let i, x, y, shouldSwitch, switchcount = 0;
let switching = true;
let dir = "asc";
/* Make a loop that will continue until
no switching has been done: */
while (switching) {
switching = false;
for (i = 0; i < (newData.length - 1); i++) {
// Start by saying there should be no switching:
shouldSwitch = false;
/* Get the two elements you want to compare,
one from current row and one from the next: */
x = newData[i].value[index];
y = newData[i + 1].value[index];
/* Check if the two rows should switch place,
based on the direction, asc or desc: */
if (dir === "asc") {
if (x > y) {
// If so, mark as a switch and break the loop:
shouldSwitch = true;
break;
}
} else if (dir === "desc") {
if (x < y) {
// If so, mark as a switch and break the loop:
shouldSwitch = true;
break;
}
}
}
if (shouldSwitch) {
/* If a switch has been marked, make the switch
and mark that a switch has been done: */
const newRow = newData[i + 1];
const copyObj = Object.assign({}, newRow);
newData.splice(i + 1, 1);
newData.splice(i, 0, copyObj);
switching = true;
switchcount ++;
} else {
/* If no switching has been done AND the direction is "asc",
set the direction to "desc" and run the while loop again. */
if (switchcount === 0 && dir === "asc") {
dir = "desc";
switching = true;
}
}
}
return {
...state,
dataToTable: newData
}
};
const newData=对象数组。
新数据[0]-{
秀:没错,
价值:[5,3,“quidem molestiae enim”]
}
newData[0]。值-它是一个带有td的表行。
const sortDataFunc=(状态、操作)=>{
const newData=[].concat(action.data);
索引=action.columnIndex;
设i,x,y,shouldSwitch,switchcount=0;
让切换=真;
let dir=“asc”;
/*做一个循环,循环将持续到
未进行任何切换:*/
while(切换){
切换=错误;
对于(i=0;i<(newData.length-1);i++){
//首先说,不应进行切换:
shouldSwitch=false;
/*获取要比较的两个元素,
一个来自当前行,另一个来自下一行:*/
x=新数据[i]。值[索引];
y=新数据[i+1]。值[索引];
/*检查两排是否应交换位置,
根据方向,asc或desc:*/
如果(目录==“asc”){
如果(x>y){
//如果是,标记为开关并断开回路:
shouldSwitch=true;
打破
}
}否则如果(dir==“desc”){
if(x
尝试使用标准js排序和拆分数据和视图
let newData=[
{show:true,value:[5,1,“quidem molestiae enim”]},
{show:true,value:[6,3,“abc”]},
{show:true,值:[7,2,“zzz”]},
{show:false,值:[8,9,“不显示”]},
];
let dir=[1,1,1]//排序方向
函数排序(一){
if(i dir[i]*(a.value[i]-b.value[i]);
如果(i==2)newData=newData.sort((a,b)=>dir[i]*(a.value[i]d.show?“”+d.value.map(x=>`${x}`)).join(“”)+.join(“”);
}
show();
thead td{光标:指针;边框底部:1px纯黑色;填充:0 10px 3px 10px}
(单击列名进行排序)
身份证件
年龄
正文
问题是您的切换
变量永远不会是false
,因为您在shouldSwitch的if/else块中都赋值了true
。