Home > Blockchain >  JavaScript - Ordering an array of objects with a chunk size / repeating pattern
JavaScript - Ordering an array of objects with a chunk size / repeating pattern

Time:07-05

I have some JSON (returned from a DB query, sample below) that I'm trying to figure out how I can order in a very specific manner:

 ──────────────── ───────────────── ───────────────── 
| 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.            |
 ──────────────── ───────────────── ───────────────── 

It needs to repeat after a set "chunk size" (in this case 3), and loop through the data.

The data is pre-sorted in the order in which the References should appear, (taking into effect the date and various other criteria), but not in the order that the steps appear.

I've been able to easily sort it either by the "Step" or by the "Reference" using a loop (and combine those values into a single string. But I have not been able to figure out how to order as shown in the first column. I need to take the first 3 references, and combine with the step "1", then take those same 3 references, combine with the step "2", and then take those same 3 references, combine with the step "3", and then start with the next "group" of references and repeat the process. (The position of AA11-1 being crucial in grasping that.)

Here's an example of the JSON data: [This is a simplified data set.]

[
    {
        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,
    }
]

I've tried a lot of things, and I'm not really any further ahead. Here's an example of something I was playing with, but it does not achieve the desired results. (Splits it into separate arrays, but not in a helpful way.)

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)

Any pointers would be appreciated!

CodePudding user response:

Finally figured this out, using a triple loop.

 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  
      }
    }

After that I can process the array items however I want, but this puts them in the needed order.

Here, as a functioning snippet:

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"));

  • Related