Add new value to an existing object with a variable content as the key?

Related searches

I have an object 'unit', whose keys are different teams. Teams' keys are employees. Each employee is an object with its own fields. I'm looping through some files, getting each employee object. Once I get an employee object, I want to place into its proper team within the unit object. For example:

var indiv = {'Rich':{'a':3,'b':4,'c':5}};
var teamname = "TeamA";
var unit = {};
unit[teamname] = indiv;

//[object Object] {
//   TeamA: [object Object] {
//     Rich: [object Object] { ... }
//   }
// }

Now, how can I add the following element to this object?

var indiv2 = {'Tom':{'a':6,'b':8,'c':10}};

So that the result is:

// [object Object] {
//   TeamA: [object Object] {
//     Rich: [object Object] { ... },
//     Tom: [object Object] { ... }
//   }
// }

Any clues? Is the only option turning TeamA into an array of objects?


One way of doing it is as below. With unit[teamname] = {}; you save an empty object under the key teamname. Then you add the single elements to this object under the keys Rich and Tom

var rich = {'a':3,'b':4,'c':5}
var tom = {'a':6,'b':8,'c':10}
var name1 = "Rich"
var name2 = "Tom"
var unit = {};
var teamname = "TeamA";
unit[teamname] = {};
unit[teamname][name1] = rich;
unit[teamname][name2] = tom;

How to add a new item to an object at a specific position with vanilla , Loop through the original object. If the index variable equals the position you want to insert the new key/value pair into, push that to the new´┐Ż The addProperty() returns a new object. It doesn't update the variable referenced by the first object. You have to assign a second object variable the output from addProperty(variable('obj_var'), item().item.key, item().item.value). Then assign that second variable back to 'obj_var'. Flow won't let you assign directly to the same var.

Assuming you have

const indiv = {'Rich':{'a':3,'b':4,'c':5}}
const indiv2 = {'Tom':{'a':6,'b':8,'c':10}}

You can use Object.assign

const indiv = {'Rich':{'a':3,'b':4,'c':5}}
const indiv2 = {'Tom':{'a':6,'b':8,'c':10}}
const unit = { teamA: {} }
Object.assign(unit.teamA, indiv)
Object.assign(unit.teamA, indiv2)

Objects, A property is a “key: value” pair, where key is a string (also called a let user = new Object(); // "object constructor" syntax let user = {} Makes it easier to add/ remove/move around properties, because all lines become alike. In real code we often use existing variables as values for property names. Hi, maybe I'm too stupid but I looked through the web for two days now and I can't find a solution. I created a PSobject and added some properties and values. it looks like this:

If you don't want/can't change indiv, indiv2 structure, you can do it this way.

Get the key from indiv2 :

let key = Object.keys(indiv2)[0]

Get values:

let values = Object.values(indiv2)[0]

And now you can do it like this:

unit[teamname][key] = values

How to add a new property to an object-type variab, This site uses cookies for analytics, personalized content and ads. How to add a new property to an object-type variable in Apply to Each? created key:values using addProperty to an object variable within a loop? a new object created by "addProperty" and the existing object initiated as variable. There can be an existing key in the map whose value was null and now updated to new value. To improve over this use second overloaded version of replace () i.e. public boolean replace(K key, V oldValue, V newValue) It will update the value of given key to newValue if its old value matches exactly to the given oldValue.

You want Object destructuring

const both = {'Rich':{'a':3,'b':4,'c':5},'Tom':{'a':6,'b':8,'c':10}};
const teamName = "TeamA";
let unit = { [teamName] : {} };
unit[teamName] = {...both}

FormData.append(), a new value onto an existing key inside a FormData object, or adds the " Content-Disposition" header used to vary from browser to browser. You can combine declaring an object variable with assigning an object to it by using the New keyword with the Set statement. For example: Set MyObject = New Object ' Create and Assign Setting an object variable equal to Nothing discontinues the association of the object variable with any specific object. This prevents you from accidentally changing the object by changing the variable.

Working with objects, A property's value can be a function, in which case the property is known Like all JavaScript variables, both the object name (which could be a For example, in the above code, when the key obj is added to the myObj , JavaScript will call the obj.toString() method, and use this result string as the new key. Is there a way to keep adding content to a variable while the foreach loop is running? The aim would be to have a variable containing data that could then be used for other purposes. It works by outputting to a text file but seems a bit messy.

The difference between FormData.set and append () is that if the specified key already exists, FormData.set will overwrite all existing values with the new one, whereas append () will append the new value onto the end of the existing set of values. Note: This method is available in Web Workers.

Adding object methods. If you need to add a script method to an object, you can do it with Add-Member and a ScriptBlock. You have to use the this automatic variable reference the current object. Here is a scriptblock to turn a object into a hashtable. (same code form the last example)

  • Is there any way to do this with Rich and Tom being variables? As in var name1 = "Rich"; var name2 ="Tom" (for the line unit[teamname].Rich)
  • Yes, just use bracket notation ( unit[teamname][name1]) instead of dot notation unit[teamname].Rich
  • That gives the same result as my last code example. My question asked how can I write first an employee to the TeamA object and then add another employee