0

If I have one array of objects with inner properties like so:

var array = [
    {
        "Structure_SortID": "001.001.003",
    },
    {
        "Structure_SortID": "001.001.003",
    },
    {
        "Structure_SortID": "001.001.003",
    },
    {
        "Structure_SortID": "001.001.003",
    },
    {
        "Structure_SortID": "001.004.002",
    },
    {
        "Structure_SortID": "001.004.002",
    }
];

How can I reduce this one array into a single 2D array that contains 2 arrays of matching "Structure_SortID" properties? Output should be:

var array = [
    // Filtered so only 001.004.002
    [
        {
            "Structure_SortID": "001.001.003",
        },
        {
            "Structure_SortID": "001.001.003",
        },
        {
            "Structure_SortID": "001.001.003",
        },
        {
            "Structure_SortID": "001.001.003",
        }
    ],
    // Filtered so only 001.004.002
    [
        {
            "Structure_SortID": "001.004.002",
        },
        {
            "Structure_SortID": "001.004.002",
        }
    ]
];

The solution needs to allow for an infinite number of lists with matching "Structre_SortID" properties. And needs to support situations where there is only 1 array of matching ID's inside the 2D array. And needs to support empty arrays or single item arrays alongside matching lists inside the 2D array.

1
  • I think the best option would be to add a key to each array and then remove it if you dont needed any more. var array = [ '001.004.003': { { "Structure_SortID": "001.001.003", }, .... { "Structure_SortID": "001.001.003", } }, '001.004.002': { { "Structure_SortID": "001.004.002", }, { "Structure_SortID": "001.004.002", } } ]; and then do another for loop and make the object an array as your example Commented Jun 16, 2022 at 5:13

1 Answer 1

1

function getArrayOfObjectByKey(data, key) {
    return Object.values(
        (data || []).reduce((acc, e) => {
            if (key in e) {
                let value = e[key];
                if (value in acc) {
                    acc[value].push(e);
                } else {
                    acc[value] = [e];
                }
            }
            return acc;
        }, {})
    );
}

var array = [
    {"Structure_SortID": "001.001.003"},
    {"Structure_SortID": "001.001.003"},
    {"Structure_SortID": "001.001.003"},
    {"Structure_SortID": "001.001.003"},
    {"Structure_SortID": "001.004.002"},
    {"Structure_SortID": "001.004.002"},
];
console.log(getArrayOfObjectByKey(array, 'Structure_SortID'));

Sign up to request clarification or add additional context in comments.

2 Comments

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
This worked perfectly. Thank you so much. The duplicate question that is apparently the same as my question, did NOT work for me. So thanks for answering regardless.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.