# Array - last (n) item

**URL:** <https://discourse.nodered.org/t/array-last-n-item/67883>\
**Category:** General\
**Created:** [18 September 2022 12:32 UTC](https://discourse.nodered.org/t/array-last-n-item/67883 "2022-09-18T12:32:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Giamma](https://avatars.discourse-cdn.com/v4/letter/g/e47774/32.png) [@Giamma](https://discourse.nodered.org/u/Giamma)\
**Post date:** [18 September 2022 12:32 UTC](https://discourse.nodered.org/t/array-last-n-item/67883/1 "2022-09-18T12:32:43Z")

</div>

Hi,  
I have a file txt with the min / max temperature for each day and with this code ...

```auto
const x = msg.payload;

x.forEach(element => {

    let data
    let tempMin
    let tempMax

    msg.payload = (element.col1)
    data = msg.payload.substr(0, 2)
    
    msg.payload = (element.col1)
    tempMin = msg.payload.substr(6, 4)

    tempMax = (element.col2)

    let arrayData = []   
    let arrayTempMin = []
    let arrayTempMax = []
    
    arrayData = flow.get('valueData') || []
    arrayTempMin = flow.get('valueTempMin') || []
    arrayTempMax = flow.get('valueTempMax') || []
        
    arrayData.push(data)
    arrayTempMin.push(tempMin)
    arrayTempMax.push(tempMax)
    
    flow.set("valueData", arrayData)
    flow.set("valueTempMin", arrayTempMin)
    flow.set("valueTempMax", arrayTempMax)
    
});

return msg;

```

(where x is the data from txt file), I set 3 array and send the value to a chart and it's works fine......  
but the txt file has more 100 row and I'd like to select only the last 30 row.  
It's all the morning I try to set this filter without success, can you help me?

(I found how to select the last item, but not the last (n) item ....)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [18 September 2022 12:42 UTC](https://discourse.nodered.org/t/array-last-n-item/67883/2 "2022-09-18T12:42:54Z")

</div>

`const x = msg.payload.slice(-30);`  
should return the last 30 indices.

> **[Array.prototype.slice() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice)**
>
> The slice() method returns a shallow copy of a portion of
> an array into a new array object selected from start to end
> (end not included) where start and end represent
> the index of items in that array. The original array will not be modified.

---

<div class="post-metadata">

**Author:** ![Giamma](https://avatars.discourse-cdn.com/v4/letter/g/e47774/32.png) [@Giamma](https://discourse.nodered.org/u/Giamma)\
**Post date:** [18 September 2022 12:48 UTC](https://discourse.nodered.org/t/array-last-n-item/67883/3 "2022-09-18T12:48:14Z")

</div>

Thank you @E1cid, thank you very much ....

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [2 October 2022 12:49 UTC](https://discourse.nodered.org/t/array-last-n-item/67883/4 "2022-10-02T12:49:11Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
