-1

I have a code below, every time after its get reduce its referring my same array, how to resolve this

var myobjData = data[0];

var obj = myobjData;
var arr = ['index', 'label', '__id', 'id', '__parent', '__proto__'];

var colObjLength = shorten(arr, obj);

function shorten(arr, obj) {
  arr.forEach(function(key) {
    delete obj[key];
  });
  console.log(obj);
  return obj;
}

the data[0] gets affecting because of this code. it should not affect the data[0] value

Bourbia Brahim
  • 14,459
  • 4
  • 39
  • 52
anu
  • 17
  • 4

1 Answers1

0

Use the use the dojo/_base/lang .clone() method in oroder to create a clone of the obj, then do what ever you want to this last , so the original object will'nt be affected ,

NOTE : it's usless to delete key proto because it define the prototype of your object see here

Below a working snippet

require(["dojo/_base/lang"], function(lang) {

  var arr = ['index', 'label', '__id', 'id', '__parent', '__proto__'];

  var obj = new Object();
  obj.index = "index";
  obj.label = "label";
  obj.__id = "1";
  obj.id = "id";
  obj.__parent = "__parent";
  
  //perform clone 
  var colObjLength = lang.clone(obj);

  colObjLength = shorten(arr,colObjLength);
  
  console.log("Original object",obj);
  console.log("Cloned object",colObjLength);




  function shorten(arr, obj) {
    arr.forEach(function(key) {
      delete obj[key];
    });
    return obj;
  }

});
<script src="//ajax.googleapis.com/ajax/libs/dojo/1.10.4/dojo/dojo.js"></script>
Bourbia Brahim
  • 14,459
  • 4
  • 39
  • 52