我有一些JSON(从DB查询返回,示例如下),我正试图弄清楚如何以非常具体的方式订购:
+────────────────+─────────────────+─────────────────+
| Desired order | Not acceptable | Not acceptable |
+────────────────+─────────────────+─────────────────+
| AA08-1 | AA08-1 | AA08-1 |
| AA09-1 | AA09-1 | AA08-2 |
| AA10-1 | AA10-1 | AA08-3 |
| AA08-2 | AA11-1 | AA09-1 |
| AA09-2 | AA08-2 | AA09-2 |
| AA10-2 | AA09-2 | AA09-3 |
| AA08-3 | AA10-2 | AA10-1 |
| AA09-3 | AA11-2 | AA10-2 |
| AA10-3 | AA08-3 | AA10-3 |
| AA11-1 | AA09-3 | AA11-1 |
| etc. | etc. | etc. |
+────────────────+─────────────────+─────────────────+
它需要在一组"块大小"之后重复。(在本例中3),循环遍历数据。
数据按照References应该出现的顺序预先排序(考虑到日期和各种其他标准的影响),但不是按照步骤出现的顺序。
我已经能够很容易地对它进行排序,或者通过"步骤";或"参考";使用循环(并将这些值组合成单个字符串)。但是我还没有弄清楚如何按照第一列所示进行排序。我需要取前3个参考文献,并与步骤1结合,然后取这3个参考文献,与步骤2结合,然后取这3个参考文献,与步骤3结合,然后从下一个"组"开始;引用,重复这个过程。(的位置AA11-1在把握这一点上是至关重要的。
以下是JSON数据的示例:[这是一个简化的数据集。]
[
{
Reference: 'AA08',
Date: 2022-06-22,
Step: 1,
},
{
Reference: 'AA09',
Date: 2022-06-23,
Step: 1,
},
{
Reference: 'AA10',
Date: 2022-06-24,
Step: 1,
},
{
Reference: 'AA11',
Date: 2022-06-24,
Step: 1,
},
{
Reference: 'AA08',
Date: 2022-06-22,
Step: 2,
},
{
Reference: 'AA09',
Date: 2022-06-23,
Step: 2,
},
{
Reference: 'AA10',
Date: 2022-06-24,
Step: 2,
},
{
Reference: 'AA11',
Date: 2022-06-24,
Step: 2,
},
{
Reference: 'AA08',
Date: 2022-06-22,
Step: 3,
},
{
Reference: 'AA09',
Date: 2022-06-23,
Step: 3,
},
{
Reference: 'AA10',
Date: 2022-06-24,
Step: 3,
},
{
Reference: 'AA11',
Date: 2022-06-24,
Step: 3,
}
]
我已经尝试了很多东西,但我真的没有任何进展。这里有一个例子,我在玩的东西,但它没有达到预期的结果。(将其分成单独的数组,但不是以有用的方式)
const chunkSize = 3;
const processedData = [];
for (let i = 0; i < jsonData.length; i += chunkSize) {
const chunk = jsonData.slice(i, i + chunkSize);
const processedChunk = chunk.map((item) => ({
...item,
Reference: item.Reference + '-' + item.Step
}))
processedData.push(processedChunk)
}
console.log(processedData)
任何指示将不胜感激!
最后用一个三重循环解决了这个问题。
const numSteps = 3
const chunkSize = numSteps * numSteps
var processedData = []
for (let i = 0; i < productionArray.length; i += chunkSize) {
var c = i
for (let x = 0; x < numSteps; x++) {
for (let y = 0; y < chunkSize; y += numSteps) {
if(productionArray[c + y] !== undefined){ processedData.push(productionArray[c + y]) }
}
c++
}
}
之后,我可以按照我想要的方式处理数组项,但这将它们放在所需的顺序中。
在这里,作为一个正常运行的代码片段:
const productionArray=[
{
Reference: 'AA08',
Date: "2022-06-22",
Step: 1
},
{
Reference: 'AA08',
Date: "2022-06-23",
Step: 2
},
{
Reference: 'AA08',
Date: "2022-06-24",
Step: 3
},
{
Reference: 'AA09',
Date: "2022-06-24",
Step: 1
},
{
Reference: 'AA09',
Date: "2022-06-22",
Step: 2
},
{
Reference: 'AA09',
Date: "2022-06-23",
Step: 3
},
{
Reference: 'AA10',
Date: "2022-06-24",
Step: 1
},
{
Reference: 'AA10',
Date: "2022-06-24",
Step: 2
},
{
Reference: 'AA10',
Date: "2022-06-22",
Step: 3
},
{
Reference: 'AA11',
Date: "2022-06-23",
Step: 1
},
{
Reference: 'AA11',
Date: "2022-06-24",
Step: 2
},
{
Reference: 'AA11',
Date: "2022-06-24",
Step: 3
}
];
const numSteps = 3
const chunkSize = numSteps * numSteps
var processedData = []
for (let i = 0; i < productionArray.length; i += chunkSize) {
var c = i
for (let x = 0; x < numSteps; x++) {
for (let y = 0; y < chunkSize; y += numSteps) {
if(productionArray[c + y] !== undefined){ processedData.push(productionArray[c + y]) }
}
c++
}
}
console.log(processedData.map(r=>r.Reference+"-"+r.Step).join("n"));