Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display only first two object in JavaScript?

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')
);
like image 380
Sathya Avatar asked Mar 13 '23 00:03

Sathya


1 Answers

You can use .slice,

<UserList data={ data.slice(0, 2) } />

Example

like image 55
Oleksandr T. Avatar answered Mar 24 '23 05:03

Oleksandr T.