# How do I do this?

**URL:** <https://forum.freecodecamp.org/t/how-do-i-do-this/57432>\
**Category:** General\
**Created:** [November 20, 2016, 8:06pm UTC](https://forum.freecodecamp.org/t/how-do-i-do-this/57432 "2016-11-20T20:06:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Simeon979](https://avatars.discourse-cdn.com/v4/letter/s/6f9a4e/32.png) [@Simeon979](https://forum.freecodecamp.org/u/Simeon979)\
**Post date:** [November 20, 2016, 8:06pm UTC](https://forum.freecodecamp.org/t/how-do-i-do-this/57432/1 "2016-11-20T20:06:57Z")

</div>

How do I work on a property(array) inside an object, without modifying the array; something like this:

```

var State = function(state) {
  this.state = state;
}
var index = [0,1,2];
var newState = new State(["E", "E", "E"]);
index.forEach(function(pos) {
  newState.state[pos] = pos;
  console.log(newState.state);
});

```

---

<div class="post-metadata">

**Author:** ![ryan-jd](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ryan-jd/32/1573_2.png) [@ryan-jd](https://forum.freecodecamp.org/u/ryan-jd)\
**Post date:** [November 21, 2016, 8:32pm UTC](https://forum.freecodecamp.org/t/how-do-i-do-this/57432/2 "2016-11-21T20:32:51Z")

</div>

Hey, you are declaring newState before the forEach therefore it is storing the previous value each time. If instead you move the newState declaration to inside the forEach it will create a new array each time as you requested:

```auto
var State = function(state) {
  this.state = state;
}
var index = [0,1,2];

index.forEach(function(pos) {
  var newState = new State(["E", "E", "E"]);
  newState.state[pos] = pos;
  console.log(newState.state);
});

```

More generally, if you want to work on an array inside an object without modifying it, then you can simply call .slice() on the array. For example:

```auto
var State = function() {
  this.array1 = [1,2,3];

}
var newState = new State;
var arr1 = newState.array1.slice() // now array1 will not be modified if you use arr1

```

---

<div class="post-metadata">

**Author:** ![michealhall](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@michealhall](https://forum.freecodecamp.org/u/michealhall)\
**Post date:** [November 21, 2016, 8:39pm UTC](https://forum.freecodecamp.org/t/how-do-i-do-this/57432/3 "2016-11-21T20:39:43Z")

</div>

> [@ryan-jd](#):
>
> More generally, if you want to work on an array inside an object without modifying it, then you can simply call .slice() on the array.

If you want a copy of an array, you can also use the cool new spread operator in ES6 …

```auto
var arr1 = [...newState.array1];

```

I’m a fan of the new syntax.

[MDN Spread Operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator#A_more_powerful_array_literal)

Of course, as with `.slice()` if there are any objects referenced in the array elements, you will not be copying those, merely pointing to them in the new array.

---

<div class="post-metadata">

**Author:** ![Simeon979](https://avatars.discourse-cdn.com/v4/letter/s/6f9a4e/32.png) [@Simeon979](https://forum.freecodecamp.org/u/Simeon979)\
**Post date:** [November 25, 2016, 10:28pm UTC](https://forum.freecodecamp.org/t/how-do-i-do-this/57432/4 "2016-11-25T22:28:13Z")

</div>

thanks for both of your help

---

<div class="post-metadata">

**Author:** ![jwilkins.oboe](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jwilkins.oboe/32/179497_2.png) [@jwilkins.oboe](https://forum.freecodecamp.org/u/jwilkins.oboe)\
**Post date:** [January 16, 2021, 6:09am UTC](https://forum.freecodecamp.org/t/how-do-i-do-this/57432/5 "2021-01-16T06:09:55Z")

</div>

Closed on January 16, 2021, 6:09am UTC


