python dict.update() 等效于 javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48747589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 08:15:12  来源:igfitidea点击:

python dict.update() equivalent in javascript

javascriptdictionary

提问by user2715898

I want to update the dictionary in javascript- modify the existing values or add new values- same as python dictionary update.

我想在 javascript 中更新字典 - 修改现有值或添加新值 - 与 python 字典更新相同。

dict+ or dict.update() seem not be working. Is it possible to do so in javascript?

dict+ 或 dict.update() 似乎不起作用。在javascript中可以这样做吗?

Thanks in advance!

提前致谢!

data={"abc":{1:2,3:4}}

if (key in d) {
            d[key].update(data[key]);
        }
        else {
            d[key]={};
            d[key]=data[key];
        }

EDIT: The updating dictionary is working fine as follow-

编辑:更新字典工作正常如下-

dg={"abc":{1:2,3:4},"sdc":{1:2,4:5}}
function upd(data) {
    for (key in data) {
        if (key in dg) {
            for (key2 in data[key]) {
                dg[key][key2]=data[key][key2];
            }
        }
        else {
            dg[key]={};
            for (key2 in data[key]) {
                dg[key][key2]=data[key][key2];
            }
        }

but if i try separating the function as-

但如果我尝试将功能分离为-

var old={};
var new={};
function update_dict(old,new) {
console.log(new,old);
    for (key2 in new) {
                old[key2]=new[key2];
            }
    return old;
}
function upd(data) {
for (key in data) {
    if (key in dg) {
        dg[key]=update_dict(dg[key],data[key]);
    }
    else {
        dg[key]={};
        dg[key]=update_dict(dg[key],data[key]);
    }

It is throwing an error saying Uncaught SyntaxError: Unexpected token new. I have really tried a lot of permutations, please suggest. Thanks a lot!

它抛出一个错误说Uncaught SyntaxError: Unexpected token new。我真的尝试了很多排列,请建议。非常感谢!

回答by Jeffrey

You can use the destructuring assignment.

您可以使用解构赋值

const first_dict = { 'key_one': 'value_one', 'key_two': 'value_two' } 
const second_dict = { 'key_one': 'value_from_second_dict', 'key_three': 'value_three' }

const accumulative = {
  ...first_dict,
  ...second_dict
}

console.log(accumulative)

/* 
[object Object] {
  key_one: "value_from_second_dict",
  key_three: "value_three",
  key_two: "value_two"
}
*/

回答by CasualCoder3

You can use Object.assign()to get the job done. It will create a new object with all the keys updated as well as add any new key value pairs to the newly created object.

您可以使用它Object.assign()来完成工作。它将创建一个更新所有键的新对象,并将任何新的键值对添加到新创建的对象中。

const target = {c: 4, d: 5}
const source = {a: 1, b: 2, c: 3};

const newObj = Object.assign(target, source);

console.log(newObj); //=> {a: 1, b: 2, c: 3, d: 5}

You can even pass in a sequence of objects to update the targetobject, check out the MDN linkfor more info

您甚至可以传入一系列对象来更新target对象,查看MDN 链接以获取更多信息

回答by Daniel A. White

  • If you are using a Mapinstance, just call yourMap.set(key, newValue).
  • If you are using a basic JavaScript object, just use either:
    • Dot-notation yourObject.yourProperty = newValue
    • Square brackets yourObject['your-property'] = newValue
  • 如果您正在使用Map实例,只需调用yourMap.set(key, newValue).
  • 如果您使用的是基本的 JavaScript 对象,只需使用:
    • 点符号 yourObject.yourProperty = newValue
    • 方括号 yourObject['your-property'] = newValue

There's no "bulk" update like Python. That will depend on how you structure your data and how deep down you need to go.

没有像 Python 那样的“批量”更新。这将取决于您如何构建数据以及需要深入到什么程度。

回答by Sudhakar Ayyar

function update_dic(a,b){
 for(key in b){
  a[key]     = b[key]
    }
 return a;
}
var  a = {1:45,4:56}
var b  = {1:56,6:67,67:67}
a = update_dic(a,b)
console.log(a)

回答by guillaume.deslandes

Lodash assignfunction should do the trick. See https://lodash.com/docs/4.17.11#assign

Lodashassign函数应该可以解决问题。见https://lodash.com/docs/4.17.11#assign