Here, I have stored four objects in an array. I want to display only the first two objects in the ReactDOM.render
method.
Currently, all of the items are displayed. I am looping through the array with the jQuery map
function.
How can I display only the first two objects?
var data = [
{id:1, content:'test1'},
{id:2, content:'test2'},
{id:3, content:'test3'},
{id:4, content:'test4'}
];
var UserList = React.createClass({
render: function() {
var Users = this.props.data.map(function(el, i){
return <li key={i}>{el.content}</li>;
});
return (
<ul>{Users}</ul>
);
}
});
ReactDOM.render(
<UserList data={data} />,
document.getElementById('container')
);
You can use .slice
,
<UserList data={ data.slice(0, 2) } />
Example
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With