我具有以下数据结构,我想按“位置”对它们进行排序。
[
{
"id": 52,
"position": 2,
"components_under_section": [
{
"id": 122,
"position": 2
},
{
"id": 123,
"position": 1
}
]
},
{
"id": 53,
"position": 1,
"components_under_section": [
{
"id": 112,
"position": 2
},
{
"id": 113,
"position": 1
}
]
}
]
到目前为止,这是我尝试过的操作,可以对外部对象进行排序,但不能对components_under_section进行排序。我有想念吗?预先感谢。
array.sort( (a, b) => {
let compAPosition = a[Object.keys(a)[0]]["position"];
let compBPosition = b[Object.keys(b)[0]]["position"];
if(a.components_under_section.length){
a.components_under_section.sort ( (c, d) => {
let compCPosition = c[Object.keys(c)[0]]["position"];
let compDPosition = d[Object.keys(d)[0]]["position"];
return ( compCPosition > compDPosition ? 1 : ((compDPosition > compCPosition ) ? -1 : 0 ) );
})
}
return ( compAPosition > compBPosition ? 1 : ((compBPosition > compAPosition ) ? -1 : 0 ) );
})
所需的输出(按components_under_section排序,然后按外部对象排序):
[
{
"id": 53,
"position": 1,
"components_under_section": [
{
"id": 113,
"position": 1
},
{
"id": 112,
"position": 2
}
]
},
{
"id": 52,
"position": 2,
"components_under_section": [
{
"id": 123,
"position": 1
},
{
"id": 122,
"position": 2
}
]
}
]
答案 0 :(得分:3)
您可以通过回调进行位置排序,然后直接对外部数组进行排序,然后对内部数组driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
//driver.findElement(By.xpath(".//button[starts-with(@type,’submit’)]")).click();
driver.findElement(By.xpath(".//*[/html/body/div[4]/div/form/div[3]/div[2]/button']")).click();
进行迭代和排序。
components_under_section