Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python dict.update() equivalent in javascript

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

dict+ or dict.update() seem not be working. Is it possible to do so in 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!

like image 316
user2715898 Avatar asked Feb 12 '18 13:02

user2715898


People also ask

What is the equivalent of Python dictionary in JavaScript?

To write the equivalent of Python dictionary's get method in JavaScript, we can use hasOwnProperty .

What is a dict called in JavaScript?

While JavaScript doesn't natively include a type called “Dictionary”, it does contain a very flexible type called “Object”. The JavaScript “Object” type is very versatile since JavaScript is a dynamically typed language.

Should I use dict () or {}?

With CPython 2.7, using dict() to create dictionaries takes up to 6 times longer and involves more memory allocation operations than the literal syntax. Use {} to create dictionaries, especially if you are pre-populating them, unless the literal syntax does not work for your case.

What does Python 3's dictionary update () method do?

The method update() adds dictionary dict2's key-values pairs in to dict. This function does not return anything.


2 Answers

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"
}
*/
like image 170
Jeffrey Avatar answered Oct 21 '22 21:10

Jeffrey


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.

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}

Note that Object.assign() modifies the first argument which is why we're passing an empty object above.

You can even pass in a sequence of objects to update the target object, check out the MDN link for more info

like image 28
CasualCoder3 Avatar answered Oct 21 '22 22:10

CasualCoder3