JavaScript How to overwrite JavaScript object values using Object() overwrite javascript object,javascript overwrite object properties,javascript overwrite object

I am still somewhat new to JavaScript, Node, etc. and come from a Groovy background (which has extremely similar syntax to JavaScript). In Groovy, I can easily do something like this:

def myMap1 = {};
def myMap2 = {};

myMap1["key1"] = "value1";
myMap1["key2"] = "value2";
myMap1["key3"] = "value3";

myMap1.each() { key, value =>
    myMap2[key] = value;
}

This would iterate through the existing map (myMap1) and copy the values for each key to the new map (myMap2), with the same key names. Easy as pie. However, JS has a concept of the prototype, which has been driving me crazy because you have to use hasOwnProperty, the index of the item sort of thing, etc and it results in a lot of code for something that's supposed to be really simple.

Looking at the MDN docs for Object(), it looks like JS has implemented something that allows a developer to access key/value pairs without having to deal with prototyping and all of that stuff, and you can just access the data in the object, and not properties of the object. I now have this:

var existingData = {"foo":"thing","bar":"otherThing","baz":"whatever"};
var update = {"foo":"newStuff","bar":"thisChanged","baz":"arghh"};

for (const [ key, value ] of Object.entries(update)) {
    console.log("Old value is " + existingData[key]);
    existingData[key] = value;
    console.log("Setting " + existingData[key] + " to " + value);
}
Answer:1

You could just do this - looping through an array of the keys:

let myMap1 = {};
let myMap2 = {};

myMap1["key1"] = "value1";
myMap1["key2"] = "value2";
myMap1["key3"] = "value3";

// create an array of the keys in the object myMap1
let myMap1_keys = Object.keys(myMap1);

// loop through the array of object keys
for (let i = 0; i < myMap1_keys.length; i++) {
  myMap2[myMap1_keys[i]] = myMap1[myMap1_keys[i]];
}

I would advise not using a for..in loop i.e. as used in one of the other answers as these are slower than a native for loop (as used above). You can see some details regarding performance here: Comparing JavaScript loops performance (for, do while, for in)

If you are just interested in literally duplicating the object you can do that like so:

let myMap2 = JSON.parse(JSON.stringify(myMap1));

Anouther answer suggested using Object.assign() and anouther suggested using ES6 spread operators but these will 'shallow' copy the object which means the objects are using references to the original object and will only copy the top level of the object - the method above deep copies the object which I usually find is what is required for most use cases and will copy every level of the object.

Answer:2
for (let key in map1) {
    map2[key] = map1[key];
}

BTW why you dont use Object.assign(); ? (notice: it returns a new object)

let update = { "key1": "value1", "key2": "value2" };
let map2 = Object.assign({}, map1, update);

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign

Answer:3

I am trying to create customized, togglable tabs in wordpress, but I am stuck. I would like to change the title part of the tabs to images. Something like that, but in wordpress: http://jsfiddle.net/...

I am trying to create customized, togglable tabs in wordpress, but I am stuck. I would like to change the title part of the tabs to images. Something like that, but in wordpress: http://jsfiddle.net/...

  1. wordpress customize tabs

I'm trying to add a class name to a child component, preserving the original classes that may be set to the component. Here is the code: import React, { Component } from "react"; import PropTypes ...

I'm trying to add a class name to a child component, preserving the original classes that may be set to the component. Here is the code: import React, { Component } from "react"; import PropTypes ...

I am using twitter bootstrap for a site and basically I have a order list within a div element. When the screen get resized to 768px, I want parent's div width, equally distributed among all child ...

I am using twitter bootstrap for a site and basically I have a order list within a div element. When the screen get resized to 768px, I want parent's div width, equally distributed among all child ...

I am trying to answer, when to use import/export and when to use require()/module.exports? But as I try to dig, it seems to get complicated. Here's my understanding require()/module.exports: this ...

I am trying to answer, when to use import/export and when to use require()/module.exports? But as I try to dig, it seems to get complicated. Here's my understanding require()/module.exports: this ...

  1. how code splitting works
  2. how webpack code splitting works