Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Sort array in to several based on value from second array

I’m trying to move objects from one array into seven different arrays. They are all in one array from the start. I want to sort them based on a value from another array that have one attribute that correlates.

Array 1 that I want to sort

var serialList = [
    {
        "OrderNumber": "1",
        "ArticleNumber": "CLIENT",
        "SerialNumber": "111"

    },
    {
        "OrderNumber": "2",
        "ArticleNumber": "IPAD",
        "SerialNumber": "222"

    },
    {
        "OrderNumber": "3",
        "ArticleNumber": "PHONE",
        "SerialNumber": "333"

    },
    {
        "OrderNumber": "4",
        "ArticleNumber": "SWITCH",
        "SerialNumber": "444"

    },
    {
        "OrderNumber": "5",
        "ArticleNumber": "HARDWARE",
        "SerialNumber": "555"

    },
    {
        "OrderNumber": "6",
        "ArticleNumber": "MAC",
        "SerialNumber": "666"

    }, 
    {
        "OrderNumber": "7",
        "ArticleNumber": "PRINTER",
        "SerialNumber": "777"

    }, 
    {
        "OrderNumber": "8",
        "ArticleNumber": "MAC",
        "SerialNumber": "888"

    }
];

Array that I want to compare from

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

var articleNumberList = [
    {
        "Article": "CLIENT",
        "Model": "client"

    },
    {
        "Article": "IPAD",
        "Model": "ipad"

    },
    {
        "Article": "PHONE",
        "Model": "phone"

    },
    {
        "Article": "SWITCH",
        "Model": "switch"

    },
    {
        "Article": "HARDWARE",
        "Model": "hardware"

    },
    {
        "Article": "MAC",
        "Model": "mac",


    }, {
        "Article": "PRINTER",
        "Model": "printer"


    }
];

I want to check the first array attribute ArticleNumber and compare it to the second array attribute Article. Once it finds a match, sort it into another array, corresponding to the Article attribute

What I have tried (and is currently working)

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

    for (u = 0; u < articleNumberList.length; u++) {
        if (serialList[i].ArticleNumber == articleNumberList[u].Article) {
            if (serialList[i].ArticleNumber == "CLIENT") {
                clientList.push(serialList[i]);

            } else if (serialList[i].ArticleNumber == "IPAD") {
                ipadList.push(serialList[i]);

            } else if (serialList[i].ArticleNumber == "PHONE") {
                phoneList.push(serialList[i]);

            } else if (serialList[i].ArticleNumber == "SWITCH") {
                switchList.push(serialList[i]);

            } else if (serialList[i].ArticleNumber == "HARDWARE") {
                hardwareList.push(serialList[i]);

            } else if (serialList[i].ArticleNumber == "MAC") {
                macList.push(serialList[i]);

            } else if (serialList[i].ArticleNumber == "PRINTER") {
                printerList.push(serialList[i]);

            }
        }
    }
};

Why I don’t want to use this solution:
It is a nestled for loop and this will eventually be used for a database of all kinds of hardware, the array serialList is today several thousands long and will continue to grow, the array articleNumberList is currently at around 40 and will also continue to grow. I also don’t really like the IF, ELSE IF statements, I feel that it probably can be handled with array.filter() but I don’t really know how to approach it.

>Solution :

If you have a list of valid article types, you can initialize a map (buckets) that hold an array of serial items.

Note: You should not create a variable for each list. Just store each list in a map that is keyed on the type.

const serialList = [
  { "OrderNumber": "1", "ArticleNumber": "CLIENT", "SerialNumber": "111" },
  { "OrderNumber": "2", "ArticleNumber": "IPAD","SerialNumber": "222" },
  { "OrderNumber": "3", "ArticleNumber": "PHONE", "SerialNumber": "333" },
  { "OrderNumber": "4", "ArticleNumber": "SWITCH", "SerialNumber": "444"  },
  { "OrderNumber": "5", "ArticleNumber": "HARDWARE", "SerialNumber": "555" },
  { "OrderNumber": "6", "ArticleNumber": "MAC", "SerialNumber": "666" }, 
  { "OrderNumber": "7", "ArticleNumber": "PRINTER", "SerialNumber": "777" }, 
  { "OrderNumber": "8", "ArticleNumber": "MAC", "SerialNumber": "888"}
];

const articleNumberList = [
  { "Article": "CLIENT", "Model": "client" },
  { "Article": "IPAD", "Model": "ipad" },
  { "Article": "PHONE", "Model": "phone" },
  { "Article": "SWITCH", "Model": "switch" },
  { "Article": "HARDWARE", "Model": "hardware" },
  { "Article": "MAC", "Model": "mac", },
  { "Article": "PRINTER", "Model": "printer" }
];

const buckets = articleNumberList
  .reduce((acc, { Article }) => acc.set(Article, []), new Map);

for (let serialItem of serialList) {
  buckets.get(serialItem.ArticleNumber)?.push(serialItem);
}

console.log(Object.fromEntries([...buckets]));

console.log(buckets.get('MAC')); // Get only the MAC items
.as-console-wrapper { top: 0; max-height: 100% !important; }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading