Search code examples
javascriptarraysmap-function

How to map a list of objects into an array of arrays


I have an array of objects that I need to reformat into a list of arrays in a specific format.

I need my list to be formatted like this

list: [
        [ "B", "A" ],
        [ "F", "E" ],
    ]

But the closest I have come is this

list: ["B A", "F E"]

using this code

const itemList = [
    {"ProductName":"A",
        "Sku":"B",},
    {"ProductName":"E",
        "Sku":"F",}
];

const newList = itemList.map(item => `${item.Sku} ${item.ProductName}`);

console.log(newList);

How would I map this correctly?


Solution

  • You can create array with the values inside map:

    const itemList = [
        {"ProductName":"A",
            "Sku":"B",},
        {"ProductName":"E",
            "Sku":"F",}
    ];
    
    const newList = itemList.map(item => [item.Sku, item.ProductName]);
    
    console.log(newList);