1

I want to convert

{
  ...
  {
    name: "Product 1",
    category: "Category 1"
  },
  {
    name: "Product 2",
    category: "Category 2"
  },
  ...
}

to

'Product 1 (Category 1)\r\nProduct 2 (Category 2)\r\n ... '

So my final render looks like:

Product 1 (Category 1)
Product 2 (Category 2)

Can anyone help me with this?

1
  • The object in your question is invalid. Every element in an object needs to have a key. Is the outer one supposed to be an array? Commented Jun 20, 2020 at 21:17

1 Answer 1

2

You can use map and join:

const data = [
  {
    name: "Product 1",
    category: "Category 1"
  },
  {
    name: "Product 2",
    category: "Category 2"
  }
];

const res = data.map(({name, category}) => `${name} (${category})`).join('\r\n');

console.log(res);

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.