我有一本字典dict1:{Name:"Alex", 3: "61"}
。我創建了一個包含相同鍵值對的副本字典dict2
。然後我將dict2作爲參數發送給一個函數,然後轉換爲這些值:dict2={1: "Alex",undefined: "61"}
。如何從JavaScript中的另一個字典的鍵更改字典的鍵?
有人可以幫我編寫代碼來代替undefined:61
到原始字典中的代碼嗎?最終的字典必須是:dict={1: "Alex",3: "61"};
dict1={Name:"Alex", 3: "61"};
dict2={Name:"Alex", 3: "61"}; //created a copy of original dictionary
function(dict2){
//some function that converts dict2 to the following value.
}
console.log(dict2); //dict2={1: "Alex",undefined: "61"}
//now i want to write the code to substitute the undefined key-value pair to the one present in the original.
//my final dict has to be as follows:
dict={1: "Alex",3: "61"};
是這方面的工作? – Kenz
該方法返回OP中描述的預期結果。你也可以使用'JSON.stringify()','.replace()''JSON.parse(JSON.stringify(dict2).replace(/ undefined /,3))' – guest271314