⊗jsrtPmFmAOO 41 of 112 menu

Outputting an Array of Objects in React

Let's say we have an array of objects with products:

const prods = [ {name: 'product1', cost: 100}, {name: 'product2', cost: 200}, {name: 'product3', cost: 300}, ]; function App() { }

Let's list each product in its own paragraph:

function App() { const res = prods.map(function(item) { return <p>{item.name} {item.cost}</p>; }); return <div> {res} </div>; }

You can put the name and price of the product in a separate tag, for example, like this:

function App() { const res = prods.map(function(item) { return <p> <span>{item.name}</span>: <span>{item.cost}</span> </p>; }); return <div> {res} </div>; }

Let's not forget to add the key attribute:

function App() { const res = prods.map(function(item, index) { return <p key={index}> <span>{item.name}</span>: <span>{item.cost}</span> </p>; }); return <div> {res} </div>; }

In the App component the following array is given:

const users = [ {name: 'user1', surn: 'surn1', age: 30}, {name: 'user2', surn: 'surn2', age: 31}, {name: 'user3', surn: 'surn3', age: 32}, ];

Print the elements of this array as a list ul.

enru