Javascript 如何从一个字典更新另一个字典中的键
dict1中的以下键值将在dict2中更新,其中包含与开头相同数量的键值对Javascript 如何从一个字典更新另一个字典中的键,javascript,dictionary,Javascript,Dictionary,dict1中的以下键值将在dict2中更新,其中包含与开头相同数量的键值对 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. } cons
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"}
}
原始dict1的副本
-不,它不是DICT2是dict1的浅副本。这不是一个深度复制。你必须手动操作。对dict1中的键运行for循环,并使用hasOwnProperty()检查它是否直接拥有属性。这将进行深度复制。检查此@Shaurabh Bharti谢谢:)原始dict1的副本
-不,它不是DICT2是dict1的浅副本。这不是一个深度复制。你必须手动操作。对dict1中的键运行for循环,并使用hasOwnProperty()检查它是否直接拥有属性。这将进行深度复制。检查这个@Shaurabh Bharti谢谢:)