1

The following key values present in dict1 are to be updated in dict2 containing same number of key-value pairs as in the beginning.

var dict1={name:"alex",1:"24",office:"germany"} //original dictionary
var dict2=dict1; //copy of original dict1

funct(dict1){
    //code that is used to change the values of dict1 to the following values.
}
console.log(dict1); //dict1:{0:"alex",undefined:24,2:"germany"}

funct(dict1,dict2){
    let {undefined:val} = dict1; //deleting the undefined key-value pair from dict1
    delete dict1["undefined"];
    console.log(dict1);     //dict1:{0:"alex",2:"germany"}
    //how can i update the dict2 with the values present in the dict1?
    //the final dict2 should have following values:
    console.log(dict2); //dict2={0:"alex",1:"24",2:"germany"}
}
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
Kenz
  • 269
  • 1
  • 3
  • 10
  • 1
    `copy of original dict1` - no it isn't – Jaromanda X May 15 '17 at 09:07
  • dict2 is shallow copy of dict1. it is not a deep copy. you'd have to do it manually. run a for loop for keys in dict1 and check if it owns property directly using hasOwnProperty(). this will do the deep copy. check this http://stackoverflow.com/questions/13632999/if-key-in-object-or-ifobject-hasownpropertykey – sbharti May 15 '17 at 09:25
  • @Shaurabh Bharti thank you :) – Kenz May 15 '17 at 14:29

0 Answers0