简体   繁体   English

如何使用 React 从数组中显示特定数量的项目

[英]How to display a specific amount of items from an array using React

Instead of showing every single item inside 'portfolioComponents', I want to show a specific amount of items, from a specific number ('startArrayHere') in the array, and raise and lower from which number ('startArrayHere') I start showing items from the Array.我不想显示“portfolioComponents”中的每个项目,而是从数组中的特定数字(“startArrayHere”)显示特定数量的项目,并从哪个数字(“startArrayHere”)开始显示项目从阵列。 I know the For-Loop is wrong, but I just can't figure out how to do it - can anybody help me out here?我知道 For-Loop 是错误的,但我就是不知道该怎么做 - 有人可以帮我吗?

class Portfolio extends Component {
    
        constructor(){
            super ()
            this.state = {
                portitems: portfolioItems,
                startArrayHere: 0,
                amountOfItemsToShow: 6
            }
            this.handleClick = this.handleClick.bind(this)
        }
        handleClick() {
            if(this.state.startArrayHere < (portfolioItems.length - 
            this.state.amountOfItemsToShow))
            this.setState(prevState => {
                return {
                    startArrayHere: startArrayHere + 1
                }
            })
        }
    
        render(){
            const portfolioComponents = this.state.portitems.map(item => 
                <PortfolioTemp key={item.id} portfolioitem={item} />)
            return (
                <div>
                    <button onClick={() => this.handleClick()}>STATE CHANGE</button>
                    <div className="portfolio-container">
                        {
                            for (let i = 0; i < this.state.amountOfItemsToShow; i++){
                                portfolioComponents[i + this.state.startArrayHere]
                            }
                        }
                    </div>
                </div>
            )
      }
} 
export default Portfolio;

To get a subset of an Array in JS, use the .slice method要在 JS 中获取 Array 的子集,请使用.slice方法

Array.slice(startIndexInclusive, endIndexExclusive)

Reference:参考:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice

Example:例子:

const a = ['Hello', 'World', 'Foo', 'Bar']

console.log(a.slice(0, 1)) // prints: ['Hello']

console.log(a.slice(0, 2)) // prints: ['Hello', 'World']

console.log(a.slice(2, a.length) // prints: ['Foo', 'Bar']

console.log(a.slice(0, a.length) // prints the entire array ... note: this would be pointless, as you could just print 'a' itself

So, to incorporate your amountOfItems you'd have to just do所以,要合并你的amountOfItems你必须做

a.slice(startIndex, startIndex + amountOfItems)

I hope this helps somewhat.我希望这会有所帮助。

You can create a shallow copy of an array between any two indicies with Array.prototype.slice() .您可以使用Array.prototype.slice()在任意两个索引之间创建数组的浅表副本。 In practice, you would use this to make a truncated copy of portfolioItems from startArrayHere to startArrayHere + amountOfItemsToShow + 1 , and render those items only.在实践中,您将使用它来制作从startArrayHerestartArrayHere + amountOfItemsToShow + 1的portfolioItems 的截断副本,并仅渲染这些项目。

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

相关问题 Javascript:如何显示数组中的一定数量的项目并在单击按钮时显示其他项目? - Javascript: how to display certain amount of items from an array and display others on button click? 如何从数组中删除特定数量的项目但在随机位置而不丢失数组顺序? - How can I remove a specific amount of items from an array but at random locations without loosing the array order? 如何从数组中生成随机数量的项目 - how to generate random amount of items from an array 如何在 Vuejs/JS 中显示一组对象中的唯一项和出现次数? - How to display unique items and the amount of occurances from an array of objects in Vuejs/JS? 如何使用react array map方法将项目添加到特定ID - How to add items into specific Id using the react array map method 如何使用 JS/JQuery 显示数组中的单个项目 - How to display single items from array using JS/JQuery 如何在循环请求中自动从数组中取出 X 个项目 - How to automate taking X amount of items from array in a loop request 如何在数据库的反应中显示项目列表 - how to display a list of items in react from a db 如何使用jQuery从子数组显示特定数据? - how to display specific data from sub-array using jquery? React 显示嵌套数组项 - React Display Nested Array Items
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM