0

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?

3 Answers 3

1

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

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

Comments

0

You can also use destucure for each item and map it to array of these values:

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

const newList = itemList.map(({ProductName, Sku}) => [
  Sku,
  ProductName
]);

console.log(newList);

Comments

0

To keep things simple, I would use Object.values as such:

const newList = [];
itemList.map(item => newList.push(Object.values(item)));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.