简体   繁体   English

我如何在React JS中使用Lodash获取特定的数组

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

How can i get the specific array using Lodash in react js ? 我如何在React js中使用Lodash获得特定的数组? I have an array of objects, 10 topUsers. 我有一个对象数组,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 我想显示从前5位开始的“ user”:“ carl”,假设{idx}是数组的索引

I'am try to use the _.map for mapping the data, but for specific index or array I dont have any idea. 我尝试使用_.map映射数据,但是对于特定的索引或数组,我没有任何想法。

<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. 您可以使用Array#slice (或lodash的等效项)将数组从索引切成末尾。 So arr.slice(4) , will create a new array from the 5th (0 based) item to the end. 因此arr.slice(4)将创建一个从第5个(从0开始)到末尾的新数组。

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. 顺便说一句-如果您想获得5个热门项目,则需要从索引5中进行切片。从第4个索引中进行切片将为您提供6个项目。

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>) })}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用lodash反应JS渲染数组项 - React JS render array items using lodash 如何在 react js [0] 中访问数组的特定索引 - how can I access specific index of array in react js [0] 如何使用JS在滚动中定位特定元素? - How can I target a specific element on scroll in React using JS? 如何使用用户数组和 axio 在反应 js 中调用复选框中的 onchange function? - How can I call onchange function in checkboxes using a user array and axio get in react js? 使用lodash react js使用数组vales过滤嵌套数据 - Filter nested data with array vales using lodash react js 我如何使用 react.js 处理一组复选框 - How can i handle an array of checkbox using react.js 如何使用 react js 从响应中获取 jsessionid? - How can I get the jsessionid from a response using react js? React JS lodash获得相同的功能 - React JS lodash get in the same function 如何使用 React hook 在 React Native 中使用 TextInput 更改对象数组的特定字符串? - How can I change a specific string of an array of objects with TextInput in react native using react hook? 使用 Lodash 链接,如何在映射到 arrays 的 object 时返回过滤的对象数组 - Using Lodash chaining, how can I return a filtered array of objects while mapping over an object of arrays
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM