简体   繁体   中英

How can i get the specific array using Lodash in react js

How can i get the specific array using Lodash in react js ? I have an array of objects, 10 topUsers.

data:{
 "topUsers":[
   {
      "user":"foo"
   },
   {
      "user":"bar"
   },
   {
      "user":"mike"
   },
   {
      "user":"jen"
   },
   {
      "user":"carl"
   },
   {
      "user":"ben"
   },
   {
      "user":"tony"
   },
   {
      "user":"mark"
   },
   {
      "user":"peter"
   },
   {
      "user":"jake"
   }
]}

and I want to display start from top 5 which is "user":"carl", assume the {idx} is the index of array

I'am try to use the _.map for mapping the data, but for specific index or array I dont have any idea.

<TableBody displayRowCheckbox={false} style={{minHeight: 65}}>
{ _.map(this.state.data.topUsers, (users, idx) => { return(
<TableRow displayBorder={true}>
    <TableRowColumn style={{height: 75 , textAlign: 'center'}}>{idx + 1}</TableRowColumn>
    <TableRowColumn style={{ justifyContent: 'center', textAlign: 'center' , paddingLeft: '5px' }}>
        <Col xs={12}>
            <Row>
                <Col xs={4} style={{display: 'flex', justifyContent: 'center', alignItems: 'center'}}>
                    <label style={{whiteSpace: 'normal', overflow: 'hidden', textOverflow: 'ellipsis'}}>
                        {_.get(users, 'users', ' ')}
                    </label>
                </Col>
            </Row>
        </Col>
    </TableRowColumn>
</TableRow>) })}

您可以尝试_.slice(array, [start=0], [end=array.length])要了解更多信息,请参见此处输入链接描述

You can use Array#slice (or lodash's equivalent) to slice the array from an index to the end. So arr.slice(4) , will create a new array from the 5th (0 based) item to the end.

Btw - if you want the 5 top items, you need to slice from index 5. Slicing from the 4th index will give you 6 items.

For example:

 const topUsers = [{"user":"foo"},{"user":"bar"},{"user":"mike"},{"user":"jen"},{"user":"carl"},{"user":"ben"},{"user":"tony"},{"user":"mark"},{"user":"peter"},{"user":"jake"}]; const result = topUsers.slice(5) .map(({ user }) => user); // whatever you want to map user into console.log(result); 

And in your case:

<TableBody displayRowCheckbox={false} style={{minHeight: 65}}>
{ this.state.data.topUsers.slice(5).map((users, idx) => { return(
<TableRow displayBorder={true}>
    <TableRowColumn style={{height: 75 , textAlign: 'center'}}>{idx + 1}</TableRowColumn>
    <TableRowColumn style={{ justifyContent: 'center', textAlign: 'center' , paddingLeft: '5px' }}>
        <Col xs={12}>
            <Row>
                <Col xs={4} style={{display: 'flex', justifyContent: 'center', alignItems: 'center'}}>
                    <label style={{whiteSpace: 'normal', overflow: 'hidden', textOverflow: 'ellipsis'}}>
                        {_.get(users, 'users', ' ')}
                    </label>
                </Col>
            </Row>
        </Col>
    </TableRowColumn>
</TableRow>) })}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM