0

I have following code:

var oldArr = { "filed1" : "abcde", "filed2" : "fghij", "anArray" : [["aaa1","bbb1"],["aaa2","bbb2"],["aaa3","bbb3"]]};
var newArr = oldArr;
var isFirst = false;

               for(var i = 0; i < oldArr.anArray.length; i++){
                    var indexA = oldArr.anArray[i].indexOf("aaa1");
                    var indexB = oldArr.anArray[i].indexOf("bbb1");

                    if(indexA > -1 && indexB > -1){
                        if(i == 0){
                            isFirst = true;
                        }
                    }
                    else{
                        newArr.anArray.push(oldArr.anArrray[i]);
                    }
                }

Everytime I add an field to newArr.anArray it will also add the field to oldArr.anArray

Is my code wrong or is ther a dependence between newArr and oldArr?

2 Answers2

0

You could to this:

var newArr = JSON.parse(JSON.stringify(oldArr));

As stated here (If the object does not contain functions)

Community
  • 1
  • 1
Arg0n
  • 8,283
  • 2
  • 21
  • 38
0

I solved the problem as I use a constructor for both objects. So there is no dependence between those objects.