Is there a way to add dynamically new property inside an object which also add new property dynamically?

add multiple properties to object javascript
typescript add property to object dynamically
how to add/remove properties to object dynamically in javascript
how to add new property to object in javascript
add property to array of objects javascript
add property to object javascript spread
add property to object javascript es6
how to add key/value pair in javascript object dynamically

I need an object with property which I add dynamically and inside this property another one which I want to add dynamically too.

const paths = ["POOL-p64/ld3/1/eee", "POOL-p64/ld3/1/afp_new"];
const peer = "EVO-Cluster-at-EVO-57";
const values = [
  {peer: "EVO-Cluster-at-EVO-57", path: "POOL-p64/ld3/1/eee", name: "14", isAllocated: false},
  {peer: "EVO-Cluster-at-EVO-57", path: "POOL-p64/ld3/1/afp_new", name: "3", isAllocated: false}
];

const objectINeeded = {};

paths.forEach(path => {
    objectINeeded[peer][path] = values.filter(brick => brick.isAllocated === false && brick.path===path)
  //another one works but don't add a new property just replace previous
  //objectINeeded[peer] = {
  //  [path] : values.filter(brick => brick.isAllocated === false && brick.path === path)
  //}
})

console.log(objectINeeded)

Can you check the below code, we move the array values to object properties using a map, then finally resassign the structure to the same variable and get the output.

const paths = ["POOL-p64/ld3/1/eee", "POOL-p64/ld3/1/afp_new"];
const peer = "EVO-Cluster-at-EVO-57";
const values = [
  {peer: "EVO-Cluster-at-EVO-57", path: "POOL-p64/ld3/1/eee", name: "14", isAllocated: false},
  {peer: "EVO-Cluster-at-EVO-57", path: "POOL-p64/ld3/1/afp_new", name: "3", isAllocated: false}
];
let out = {};
paths.map(function(x, i) {
  let value = values.filter(brick => brick.isAllocated === false && brick.path===x);
    out[x] = value ? [values[i]] : []
});
out = {[peer]: out}
console.log(out);

How to Set Dynamic Property Keys with ES6, You should create a custom class and expose bindable properties instead of However, when that is not possible within the application architecture, the use of an Dynamic. Class. Problem. You want to bind properties on a destination object to you must also implement the IEventDispatcher interface and its methods. Here's a method that accepts both a property name and a property value then adds them both to an ExpandoObject: public ExpandoObject CreateDynamicCustomer(string propertyName, string PropertyValue) { dynamic cust = new ExpandoObject(); ((IDictionary<string, object>)cust)[propertyName] = PropertyValue; return cust; }

I think this is what you want .

I added some values to the original array to see more edge cases.

Two added objects should be filtered out (peer: "EVO-Cluster-at-EVO-571" and path: "POOL-p64/ld3/1/eeea").

Also I added two objects, which share the same peer and path, so that they are pushed to the array of objectINeeded[<peer>][<path>].

The result is an object with one peer key ("EVO-Cluster-at-EVO-57") which has two path keys ("POOL-p64/ld3/1/eee" and "POOL-p64/ld3/1/afp_new") of which the first one has two objects and the second one has only one object.

This is only a more generalized answer, as it allows for multiple peers

console.clear()

const paths = [
  "POOL-p64/ld3/1/eee", 
  "POOL-p64/ld3/1/afp_new"
];
const peers = ["EVO-Cluster-at-EVO-57"];
const values = [
  {
    peer: "EVO-Cluster-at-EVO-57", 
    path: "POOL-p64/ld3/1/eeea", 
    name: "14", 
    isAllocated: false
  },
  {
    peer: "EVO-Cluster-at-EVO-571", 
    path: "POOL-p64/ld3/1/eee", 
    name: "14", 
    isAllocated: true
  },
  {
    peer: "EVO-Cluster-at-EVO-57", 
    path: "POOL-p64/ld3/1/eee", 
    name: "14", 
    isAllocated: false
  },
  {
    peer: "EVO-Cluster-at-EVO-57", 
    path: "POOL-p64/ld3/1/eee", 
    name: "15", 
    isAllocated: false
  },
  {
    peer: "EVO-Cluster-at-EVO-57", 
    path: "POOL-p64/ld3/1/afp_new", 
    name: "3", 
    isAllocated: false
  }
];

// Filter the original Array so that only those with the right properties are used
const filtered = values.filter(brick => (brick.isAllocated === false) 
                               && (paths.indexOf(brick.path) !== -1)
                               && (peers.indexOf(brick.peer) !== -1)
                              );

// 'Map' the filteres array to an object where the 'peer' and 'path' values are used as keys
const objectINeeded = filtered.reduce((map, obj, org) => {
  if (!map[obj.peer]) { map[obj.peer] =  {}; }
  if (!map[obj.peer][obj.path]) { map[obj.peer][obj.path] =  []; }
  map[obj.peer][obj.path].push({...obj});
  return map;
}, {});

console.log(objectINeeded)

Flex 3 Cookbook: Code-Recipes, Tips, and Tricks for RIA Developers, How to change the value of an object property. Dynamic Web Coding · Tutorials · JavaScript · PHP · Site Map To add a new property to an object, specify the object name followed by: a dot, the name of the new property, Square bracket syntax is also necessary when the property name is variable; for example, if it is  How to dynamically create reactive properties in Vue. One of the best features that JavaScript frameworks are known for is their reactivity and Vue is no exception. In this tutorial, we’ll be looking at how to add reactive properties in a component’s instance after initialization.

try this code.

let objectINeeded2 = {};
objectINeeded2[peer] = {};
paths.forEach((x) => {
    objectINeeded2[peer][x] = values.filter(brick => brick.isAllocated === false && brick.path === x);
})
console.log(objectINeeded2);

How to Add, Modify, or Access JavaScript Object Literal Properties, You use the new operator with a constructor function to create a new object. the objects for which it is the prototype also get the new property. Can add or remove properties dynamically to individual objects or to the entire set of objects. In Java, you specify the superclass within the class definition. Once we have added properties directly, we can also add properties to our object in a more dynamic fashion using the AddPropertymethod we have provided for you. One example of why you might do this is to add properties to your object from another source of data. We will add the Languageproperty. // Add properties dynamically to expando

Details of the object model, Demonstrate how to dynamically add variables to an object (a class instance) at runtime. a new object is created with an additional member variable and also Object)" will have the apparent capability to add new properties. a property of a class instance, that name within that instance gets that value. Add a RadDockLayout with a few RadDockZone controls inside it. Just like in the Dynamically Creating RadDock Controls topic, add a property for storing the current dock states: At the top of the code behind for your Web page, add two new using statements (C#) or Imports statements (VB) for System.Collections.Generic and Telerik.Web.UI: C#

Add a variable to a class instance at runtime, It is possible for more than one program to define dynamic properties on the same object. In these cases, avoid name conflicts. Dynamic property names must be  After displaying the items, we should create a button that can be pressed to add a new element to the list. Let's create the Add Button together with the javascript code, that will be used for the ajax call. To be able to serialize this form, we should also add an id to the form in the create file.

Dynamic Properties - Adding Properties to an Instance, How to create membership rules to automatically populate groups, and a rule This article details the properties and syntax to create dynamic If a user or device satisfies a rule on a group, they are added as a This list can also be refreshed to get any new custom extension properties for that app. In the current application that I am building, I came across this scenario where I had to add properties to an object during runtime. Basically it is like this: I had to display some data of a parent object on a grid – but the number of columns to display depended on the number of child objects the parent had.

Comments
  • You never did set the objectINeed[peer] = {} and thus you cannot access it
  • Thank you for help :)
  • You are missing the filters
  • @yunzen this solution does not use filter maybe it can be done with the same, the end object structure is achieved with the current solution!
  • @NarenMurali I think the asker does want a little bit more than he is telling us. The example does not use any edge cases, mutliple appearances etc.
  • @yunzen have modified my answer to be more specific to the users question containing the filter condition, can you have check to be safe?