Search code examples
javascriptangularlodash

how to multiply slice an array using lodash javascript library


this.listSelectProjectStages = _.map(
                        _.values(_.get(map, 'PROJECT_STAGE', [])).slice(0, 5),
                        (e) => ({
                            value: e?.name,
                            label: e?.label,
                        })
                    ); 

as per the above lodash mapping the values from index 0 to 5 is fetched from PROJECT_STAGE array and pushe to the 'this.listSelectProjectStages' variable. I have a requirement change. I want to get 0 to 5 index and also last two values of the particular array and push to the 'this.listSelectProjectStages' variable. please help me.? how can I manipulate this code for that.?


Solution

  • First, store the array in a variable:

    let arr = _.values(_.get(map, 'PROJECT_STAGE', []));
    

    Then, use slice and concat to get the relevant parts:

    _.map(arr.slice(0,5).concat(arr.slice(-2)), e => ({value: e?.name, label: e?.label}));