Skip to content
Advertisement

Javascript reorder array of nested arrays in particular order

I am looking for a way to reorder the beneath array of nested arrays in a specific order. The values within the exposureLevel array come in any order when imported.

var exposureLevel = [["Exposure Level","Number"],["High",15],["Low",38],["Medium",105],["None",156]];

Some of these values can also be missing from the exposureLevel array. Beneath are some possible examples of this:

[["Exposure Level","Number"],["Low",38],["Medium",105]];
[["Exposure Level","Number"],["High",15],["None",156]];
[["Exposure Level","Number"],["High",15],["Medium",105],["None",156]];

I would like the array to be in the order beneath, starting with the [1] element after the heading being None, Low, Medium, High. The three arrays beneath show how I’d like the three examples above to output. I’m not entirely sure how to do this, but I’d have to have some type of check where the string values within the sub-arrays are compared and that entire sub-array’s position in the parent array is adjusted.

[["Exposure Level","Number"],["Low",38],["Medium",105]];
[["Exposure Level","Number"],["None",156],["High",15]];
[["Exposure Level","Number"],["None",156],["Medium",105],["High",15]];

I have looked at these posts prior to posting this but haven’t had a great deal of success:

  1. For-each over an array in JavaScript
  2. JOLT – Reorder nested arrays
  3. How do I check if an array includes a value in JavaScript?
  4. Javascript array contains/includes sub array

My current solution:

for (let i = 0; i < exposureLevel.length; i++) {

  if(exposureLevel[i][0] == "None"){
    console.log(exposureLevel[i][0])
  } 
  if(exposureLevel[i][0] == "Low"){
    console.log(exposureLevel[i][0])
  } 
  if(exposureLevel[i][0] == "Medium"){
    console.log(exposureLevel[i][0])
  } 
  if(exposureLevel[i][0] == "High"){
    //not sure how to push onto array correctly
    console.log(exposureLevel[i][0])
  }

}

I understand my current solution doesn’t do anything, but I’ve managed to isolate the string in each sub-array. The next step is to push the entire array into a new array in the specified order. If anyone knows a better way to do the above, please let me know.

I have little experience with JS. If someone could assist me with this or point me in the right direction I’d greatly appreciate it. Please let me know if there’s any further information that is needed. Thanks.

Advertisement

Answer

Array.sort with mapping of keys to another setting orders

const exposureLevel = [["Exposure Level","Number"],["High",15],["Low",38],["Medium",105],["None",156]];
const orders = { None: 1, Low: 2, Medium: 3, High: 4 };
const sorted = exposureLevel.sort((a, b) => (orders[a[0]] || 0) - (orders[b[0]] || 0));
console.log(sorted);

Reference: Array.sort()

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement