score:0
you forgot return
inside your map.
render() {
const obj = this.props.posts.posts;
let arr;
if (obj) {
arr = Object.values(obj); //Converting an Object into an array
}
return (
<div>
{ arr ?
<div>
{ arr.map(post => {
return (
<div key={post.id}>
{post.title}
</div>
)
})
}
</div> :
<div>
No Data
</div>
}
</div>
);
}
or, alternatively, drop the curly braces with a oneliner:
arr.map(post => (<div key={post.id}>
{post.title}
</div>
)
)
and don't forget the key
attribute.
I also suggest you abstract way your dumb components. It'll make your code more readable.
score:1
You need map your data. Something like this:
<div>
<ul>
{props.incomingDataArray.map((singleArrayItem, index) => {
return (
<li key="list + index">{singleArrayItem.commentCount}</li>
<li key="list + index">{singleArrayItem.timestamp}</li>
)
})
}
</ul>
<div>
score:2
You can keep your data as an object, and use Object.keys
to get an array of keys, map over the keys, and use the keys to access nested objects. If you don't know the shape of your data source, you could use recursion.
Here is a working code sandbox example where I've taken your data source and turned it into a table to illustrate how to do so.
Here is the code:
import React from 'react';
import { render } from 'react-dom';
import Hello from './Hello';
const defaultData = {
"8xf0y6ziyjabvozdd253nd": {
id: '8xf0y6ziyjabvozdd253nd',
timestamp: 1467166872634,
title: 'Udacity is the best place to learn React',
body: 'Everyone says so after all.',
author: 'thingtwo',
category: 'react',
voteScore: 6,
deleted: false,
commentCount: 2
},
"6ni6ok3ym7mf1p33lnez": {
id: '6ni6ok3ym7mf1p33lnez',
timestamp: 1468479767190,
title: 'Learn Redux in 10 minutes!',
body: 'Just kidding. It takes more than 10 minutes to learn technology.',
author: 'thingone',
category: 'redux',
voteScore: -5,
deleted: false,
commentCount: 0
}
}
const TableHeader = ({ fields }) => (
<thead>
<tr>
{
fields.map( field => <th key={ field }>{ field }</th>)
}
</tr>
</thead>
);
const TableBody = ({ data }) => (
<tbody>
{
Object.keys(data).map(
datum =>
<tr>
{
Object.keys(data[datum]).map(
field => <td>{data[datum][field]}</td>
)
}
</tr>
)
}
</tbody>
);
const App = () => (
<table>
<TableHeader fields={ Object.keys(defaultData[Object.keys(defaultData)[0]]) } />
<TableBody data={defaultData} />
</table>
);
render(<App />, document.getElementById('root'));
Source: stackoverflow.com
Related Query
- How to map over an object which has a unique id for each object values and return the values to a React component
- How to inline JSON object as map of attributes and values for component?
- How can I map over and object, that contains another Object I want to map over and return JSX from it?
- Loop through Object which has Array as value and create div for each key with its value as information
- How to use for loop in react so I can loop over two array and return the object in second array if both match
- How do I map values for custom thumbnails using react-responsive-carousel and react-image-magnifiers?
- How do i iterate over an array of object in react.js and add the values of prices in the object as shown below
- How to make an electron react app, which has 2 windows - a general one and one for the Tray
- How to map over 2 Array of object in React and render to Table
- React.js Map Array style for a unique id and each button disabled
- Rendering text boxes using map function for each object in this.state array. how can i handle text in the text area
- How do I get the "results" object nested in the array "news" and map over it?
- How can I map over two arrays and return matches as buttons?
- How do I map an through an array of objects and display each object individually? I want each pizza order to show
- how to map over an Object and render the arrays inside the object javascript/reactjs?
- How to check and return value of state object array and use that to identify which object to take data from
- How do I iterate over an array of objects match a common element from another array and return the key value for "name"
- How does a GraphQL mutation know what node it's modifying? Is there a unique and global node ID for each node in the database?
- how can i map the string that are in the object where empty string considered as empty line and each string restricted to one line
- How to map over and return items in localStorage?
- ReactJS - How can I push a "link" object to an array with unique values via a for loop?
- How can i get values from multiple input boxes on form submit and store the values as an object in an state array for React?
- how to correctly map an obj and return a single value with several identical values ReactJS
- I am trying to map over an array and add comma's in between each instance except for the last
- Looping through array of objects and return the key and value for each object
- How can I get the object which has been changed and also the new changed value when Selecting options from the drop down
- How to map over array of dissimilar objects and return JSX without Error
- ImmutableJS Map, how to iterate over a map and return a modified result
- for of loop unable to loop through values of an object which has been treated as an array in reactjs
- React - how map an array of objects and add a title without repeating and just showing each unique ttitle
More Query from same tag
- React Material-UI Modal TypeError: Cannot read property 'hasOwnProperty' of undefined
- How do I insert a component within a MAP of other component in React?
- Why are JS / React much quicker than WebAssembly / Rust?
- Render bar chart with chart.js and react and es6
- onChange event doesn't fire on first input
- React routing from external link
- React, cannot read this.props, when using style object
- react js and redux sending a api call before each route
- How to change the state of the checkbox using a method?
- my images array is empty when I pass student to actions in redux but it is working in postman
- Problems POSTing json to Node/Express server from Axios/React
- How do I make a generic type alias?
- Problem with useRef React Hook and event listener and change img src
- Mapped data rendering multiple Material-UI Dialog components
- Handling scroll Animation in React
- how to cache useRequest response?
- ReactJS modify array of objects spesific property
- React Router first route with no menu or main layout
- I can't call a function that I've assigned to a element in React
- react-tsparticles is hiding my other components when i just want it to be a backgroud
- How to make an animation in javascript/react js?
- React - setState within a for loop to add data to array
- I'm not able to get API response when the state is changed
- How do I get the exact height of an element which the height css attribute is set auto in reactjs?
- React JS: Upload File Options is Not working On Custom button
- How to disable ripple in React's Material UI library's Tab Component
- Marquee tag in React
- How to properly style React Rich Text Editor (react-rte)
- React DOM not re-rendering accordingly to setState
- How can I change the CSS properties of an imported component in React