score:5
At first this is not valid javascript:
const users = [
'tom': {
phone: '123',
email: 'hotmail'
},
// ...
];
You should either define an array or an object. Let's say your users
is an object literal:
const users = {
'tom': {
phone: '123',
email: 'hotmail'
},
// ...
};
Then you can iterate over entries of the object:
const list = Object.entries(users).map(([name, info]) =>
(<User
name={name}
phone={info.phone}
email={info.email}
/>)
);
However Object.entries()
is not supported in all browsers, check if it works in your environment.
score:0
You need to be referring to "user" instead of "users" inside of the Parent component's map function. The purpose of the "user" variable is to represent the current instance of each element in the array. So the map function should look like:
const list = users.map((user) =>
(<User
name={user}
phone={user.phone}
email={user.email}
/>),
);
instead.
score:0
One solution is to change your "list" to an actual list:
const users = [
{
name: 'tom',
phone: '123',
email: 'hotmail'
},
{
name: 'rob,
phone: '321',
email: 'yahoo'
},
{
name: 'bob',
phone: '333',
email: 'gmail'
},
];
Now user user.name
instead of user
.
Alternatively, create an object keyed by the names of each user:
const users = {
'tom': {
phone: '123',
email: 'hotmail'
},
'rob': {
phone: '321',
email: 'yahoo'
},
'bob': {
phone: '333',
email: 'gmail'
},
};
Then map over the keys:
const list = Object.keys(users).map((user) =>
(<User
name={user}
phone={users[user].phone}
email={users[user].email}
/>),
);
score:0
Your const users should be modified to map through them.
const users = [
{
name: 'tom',
phone: '123',
email: 'hotmail',
},
{
name:'rob',
phone: '321',
email: 'yahoo'
},
{
name: 'bob',
phone: '333',
email: 'gmail'
},
];
const list = users.map((usr =>
(<User
name={usr.name}
phone={usr.phone}
email={usr.email}
/>),
);
return <ul>{list}</ul>;
}
score:1
First thing is that users
is not a valid array If you want key value pair in main scope then use Object
({})
render() {
const users = {
'tom': {
phone: '123',
email: 'hotmail'
},
'rob': {
phone: '321',
email: 'yahoo'
},
'bob': {
phone: '333',
email: 'gmail'
},
};
const list = Object.keys(users).map((user) =>
(<User
name={user}
phone={users[user].phone}
email={users[user].email}
/>),
);
return <ul>{list}</ul>;
}
Source: stackoverflow.com
Related Query
- How to loop through properties of objects inside of an array within a React component?
- How to loop through an array an put create an array of objects using react and javascript?
- loop through multiple objects inside the first array from JSON data displaying two arrays with objects in React using FUNCTION COMPONENT
- how can i loop through the array inside of an object. and render it in the react component
- What is the correct way to loop through this array of objects inside of render, react
- How to loop through an array and display data within the object of that array, in react and javascript
- How can I create and loop through a custom array within a react functional component?
- How do i loop through an array of objects in react
- How do I create an array of objects in a React class component through a for loop to be mapped in a functional component?
- Loop through simple array of objects in React
- React Object of Objects - How to loop through
- how to properly iterate through array of post objects retrieved through Wp Rest Api in react js
- How to count multiple objects inside nested array sequentially in react js
- How to loop through objects in JSX react
- How to update properties of an array nested inside objects in ReactJS
- How to map through nested array of objects in React
- How to set a state of an array of object inside an array of objects in react
- How do I loop through an array in an array of objects
- How to loop an object that is inside an array and is inside an object? React
- How to render/map through an array of objects in React
- how to foreach/map array inside objects on react hooks?
- How to loop through nested objects in jsx react
- How to loop an array inside an array with React
- How to update a useState which has an array, inside this array I have objects this objects will update when the input value will change in react js
- Loop through an array of objects, find duplicate objects by id, add their values in a single object, react js
- Loop through array of objects and display them in react component
- How to properly iterate or loop through an objects in react
- how to loop through data and assign values to state array in react
- React Native - How to change the style and properties to a pressed row inside a map loop
- How to loop array and output the values inside an array of objects as 'value' and 'label'?
More Query from same tag
- trying to send querystring parameters to another page using reactJS
- how to set miliseconds to date format inside a map in react?
- How can I use a class component at different places with unique IDs?
- How to filter a nested array with custom key in React / Javascript
- How to use default bootstrap class with styled-components?
- Update input box when outside is clicked in ReactJS
- How can I create a browser router in ReactJS?
- ReactJS passing a state value to onClick event function as a parameter
- When updating the state of the search input it is always 1 input behind in react
- Cross origin issue when upload to aws
- react-redux: `state.get` is not a function
- Uncaught TypeError: (0 , _reactRedux.combineReducers) is not a function
- React - State Error when loading image
- Received a promise that resolves to: undefined. Promise elements must resolve to a class or function
- Creating a react container component in typescript for a MuiPickersUtilsProvider component
- Destructuring props assignment
- Properties on this.props does not exist
- How to Route some page without sidebar in React
- Returning a Redirect within then of an async function
- Which is faster and performant: useState or useReducer?
- How to prevent a route from registering before jwt verifies react
- How can you backtrack through indexes of an array if element returned is null
- Filtering element in array is not working in Reactjs
- react-router-dom not working as intended
- How to decide on the most efficient way to call a handler function in React?
- Use enviroment variables in dockerized react app
- React props.children is not array
- React functional component reinitialises local functions and variables on re render (React Hooks)
- onKeyDown / onKeyUp listener in React
- Using react-router-dom, BrowserRouter never works and HashRouter only sometimes works. Why?