Is there a way to set attribute to sub objects in a class?

Advertisements I would like to be able to do this: >>> obj = Example(‘hi’, ‘hello’) >>> obj.a ‘hi’ >>> obj.sub_obj.b ‘hello’ I try this but I get AttributeError: ‘dict’ object has no attribute ‘b’: class Example: def __init__(self, a, b): self.a = a self.sub_obj = {} self.sub_obj.b = b I see a similar question but… Read More Is there a way to set attribute to sub objects in a class?

How to access the individual contents of a nested object(state form) in React?

Advertisements I have a nested object that is usually rendered when I have finally transferred the state through navigation (transferring the product items to the payment page). This question is a continuation from my previous question for the logic of my shopping cart functionality: How to transfer product items to the payment page after clicking… Read More How to access the individual contents of a nested object(state form) in React?

Create a nested object from Array javascript

Advertisements I have the array like this\ [ "/server/api/v2.0/terms-conditions/latest", "/server/api/v2.0/tools/extension/rewrite", "/server/api/v2.0/users/presigned-url", "/server/api/v2.0/users/detail", "/server/api/v2.0/users/detail", "/ai/fingerprint", "/ai/fingerprint", "/server/api/v2.0/tools/web-tools/product-description-generator", "/analytics/api/v2.0/user-stats/analytic-clarity", "/server/api/v2.0/tools/generate/readability-score-1", "/server/api/v2.0/tools/generate/readability-score-2", "/analytics/api/v2.0/user-stats/analytic-article", ] and I want an output like\ { "server":{ "api":{ "v2.0":{ "terms-condition":{ "latest":[ "/server/api-1/v2.0/terms-condition/latest""m" ] }, "tools":{ "web-tools":{ "product-description-generator":[ "/server/api/v2.0/tools/web-tools/product-description-generator" ] }, "generate":{ "readability-score-1":[ "/server/api/v2.0/tools/generate/readability-score-1" ], "readability-score-2":[ "/server/api/v2.0/tools/generate/readability-score-2" ] }, "extension":{ "rewrite":[ "/server/api/v2.0/tools/extension/rewrite" ] }… Read More Create a nested object from Array javascript

How to push an object coming from postman to nested object?

Advertisements I’m having a problem in pushing these data to an nested object. Here is what I put on postman as JSON format: "productId":"621256596fc0c0ef66bc99ca", "quantity":"10" here is my code on my controller module.exports.createOrder = async (data) => { let product = data.productId; let oQuantity = data.quantity let totAmount = data.totalAmount return User.findById(product).then(user =>{ user.userOrders.products.push({productId:product}) user.userOrders.products.push({quantity:oQuantity})… Read More How to push an object coming from postman to nested object?

Nest Object query mongoDB

Advertisements I have a collection of documents containing information about movies by following format: { "_id": "100063", "_rev": "1-90d4068b3946fe809a9c9022f1e1c354", "title": "The Love She Sought", "year": 1990, "rating": null, "runtime": "100 min", "genre": [ "Drama" ], "director": "Joseph Sargent", "writer": [ "Jon Hassler (novel)", "Ron Cowen (teleplay)", "Daniel Lipman (teleplay)" ], "cast": [ "Angela Lansbury", "Denholm… Read More Nest Object query mongoDB

Nested Object Loop in JS

Advertisements I’m trying to add a series of values to a nested object, having some trouble with the loop in the following code. Any help would be really appreciated. let settings = {}; function write(id, values) { if(!settings[id]) settings[id] = {}; for(var x = 0; x < Object.keys(values).length; x ++) { settings[id][values[x]] = values[values[x]]; }… Read More Nested Object Loop in JS