# Change an array in a function

**URL:** <https://discourse.nodered.org/t/change-an-array-in-a-function/22292>\
**Category:** General\
**Created:** [26 February 2020 17:40 UTC](https://discourse.nodered.org/t/change-an-array-in-a-function/22292 "2020-02-26T17:40:14Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [26 February 2020 22:01 UTC](https://discourse.nodered.org/t/change-an-array-in-a-function/22292/6 "2020-02-26T22:01:56Z")

</div>

Well as it still not very clear what you want to do with your function, we can make arbitrary examples how things can be done.

```auto
//array of objects
let mod = [{"naam":"peter", "age":5 },{"naam": "jan", "age":5 },{"naam": "klaas", "age":5 },{"naam": "pieter", "age":5 },{"naam": "miep", "age":5}];

//name to search
let targetname = "jan"

//output does not exist yet cos we don't know can we find the name
let output = null

// lets modify the array by using .map method
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
mod = mod.map(obj => {
   // every object in array explored to find the target
    // if naam propert has value we are searching
   if(obj.naam === targetname){
       obj.age ++ //lets make that person one year older (change).
       output = obj // as we have success of, lets put that object to output variable 
   }
   return obj 
})

//lets write changed array to debug tab to see what just happened
node.warn('changed array: '+JSON.stringify(mod))

//as mod array is modified, you may wan to store it in context but that's another story
//context.set("mod",mod)

// send message if we have found person in array and made changes
if(output !== null){
    //send changed object
    msg.payload = output
    // or send whole modified arrray
    //msg.payload = mod
    return msg;
}

```

---

_[View the full topic](https://discourse.nodered.org/t/change-an-array-in-a-function/22292)._
