Is it possible to use the nested's object property value in js? | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 2

Is it possible to use the nested's object property value in js?

Let a={ Name : "Dinesh", Budget: 19000, Phone :{ Price : a.Budget} } Is supposed to run , if it is then How?

20th Mar 2023, 7:14 AM
Dinesh Pal :)
Dinesh Pal :) - avatar
8 ответов
+ 6
Yes, it is possible to use nested object property values in JavaScript. For example, consider the following object: const person = { name: "John", age: 30, address: { street: "123 Main St", city: "Anytown", state: "CA" } }; To access the value of the street property, which is nested inside the address property, you can use dot notation or bracket notation. Here are some examples: Using dot notation: console.log(person.address.street); // output: "123 Main St" Using bracket notation: console.log(person['address']['street']); // output: "123 Main St" Note that in bracket notation, you need to enclose the property name in quotes. You can also use a variable to access a nested property, like this: const propertyName = 'street'; console.log(person.address[propertyName]); // output: "123 Main St" In summary, you can access nested object property values in JavaScript using either dot notation or bracket notation
20th Mar 2023, 1:34 PM
Hasnain [ACTIVE CHALLENGER]
Hasnain [ACTIVE CHALLENGER] - avatar
+ 2
No, it's not possible to use a nested object's property value in JavaScript the way you've written it. In your example, you're trying to reference the a.Budget property value while defining the Phone property of the a object, but at that point, the a object hasn't been fully defined yet, so the Budget property doesn't exist. Instead, you can define the Phone property separately after defining the Budget property like this: let a = { Name: "Dinesh", Budget: 19000 }; a.Phone = { Price: a.Budget }; if you want to learn javascript. Cetpa Infotech is a good option for someone who wants to learn javascript
22nd Mar 2023, 5:10 AM
Siddharth Verma
Siddharth Verma - avatar
+ 1
Hasnain [ACTIVE CHALLENGER] It is a very good explanation.😎 But I think Dinesh Pal :) was referring to his example. accessing other property values inside the same object. I thought it was a simple thing such as using the 'this' keyword, but it is not as simple as that and is actually quite tricky. easier if it was a function or a class.
20th Mar 2023, 3:19 PM
Bob_Li
Bob_Li - avatar
+ 1
Bob_Li Yes bro, you are right :-)
20th Mar 2023, 5:10 PM
Dinesh Pal :)
Dinesh Pal :) - avatar
+ 1
Yes. The example you gave is correct. It could be a little bit confusing if you want to access that value. But is so simple. If you want to access a.Budget, assume the property Phone as an object.Then console.log(a.Phone.Price); This will output - a.Budget.
20th Mar 2023, 8:22 PM
Samuel Mindaye
Samuel Mindaye - avatar
+ 1
Samuel Mindaye I did the same thing but i got an error which was out of my knowledge :-)
21st Mar 2023, 1:35 AM
Dinesh Pal :)
Dinesh Pal :) - avatar
+ 1
Dinesh Pal :) you can use a getter let a = { budget: 19000, phone: {get price(){return a.budget}} } console.log(a.phone.price) //change budget a.budget = 1 //price also changes console.log(a.phone.price) //but you cannnot set price. it is dependent on budget a.phone.price = 50 //price not changed console.log(a.phone.price)
21st Mar 2023, 2:10 AM
Bob_Li
Bob_Li - avatar
+ 1
Dinesh Pal :) or you can use both getter and setter, but budget and price will always be linked. Changing one will always change the other. let a = { budget: 19000, phone: {get price(){ return a.budget}, set price(n){ a.budget=n}}, } //budget and price are always linked console.log(`a.budget: ${a.budget}`) console.log(`a.phone.price: ${a.phone.price}`) //change budget a.budget = 0 console.log(`a.budget: ${a.budget}`) //price is also changed console.log(`a.phone.price: ${a.phone.price}`) //change price a.phone.price = 5 console.log(`a.phone.price: ${a.phone.price}`) //budget is also changed console.log(`a.budget: ${a.budget}`)
21st Mar 2023, 3:33 AM
Bob_Li
Bob_Li - avatar