Is it possible to map only a portion of an array? (Array.map())

前端 未结 8 924
终归单人心
终归单人心 2020-12-31 07:16

I am building a project using React.js as a front-end framework. On one particular page I am displaying a full data set to the user. I have an Array which contains this full

相关标签:
8条回答
  • 2020-12-31 07:39

    Array.reduce should do what you're asking for. Just change the if statement depending on which range you want.

    var excludeAfterIndex = 5;
    
    feed.reduce((mappedArray, item, index) => {
      if (index > excludeAfterIndex) { // Whatever range condition you want
        mappedArray.push(<FeedItem key={index} data={item}/>);
      }
    
      return mappedArray;
    }, []);
    
    0 讨论(0)
  • 2020-12-31 07:43

    Yes, you can map portion of array, based on index. For example:

    yourArray = yourArray.map(function (element, index, array) {
            if (array.indexOf(element) < yourIndex) {
                return {
                   //logic here
                };
            } else {
                return {
                    //logic here
                };
            }
    
        });
    
    0 讨论(0)
提交回复
热议问题