Access property of an object of an object [duplicate]

Access property of an object of an object [duplicate]



This question already has an answer here:



Having an object of this form:


myObj =
id: 1,
values:
internal:
myProp1: true
myProp2: true
,
external:
myProp1: true
myProp2: true



;



I want to be able that when I choose an option, internal or external to be able to set myProp1 to false only for the one option I chose.


internal


external


myProp1



In this case, if I choose internal the object should look like this:


internal


myObj =
id: 1,
values:
internal:
myProp1: false
myProp2: true
,
external:
myProp1: true
myProp2: true



;



I tried to do it like but something I do wrong:


Object.keys(myObj).forEach(element =>
if(element === "values")
element.forEach(innerElement =>
if(innerElement === "internal")
innerElement.myProp1= false;

);

);



Any suggestions?



This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





you can access subobject like accessing object prop myObj.values.internal.myProp1 = false
– jonatjano
Aug 29 at 14:58



myObj.values.internal.myProp1 = false





If you want to use a string as a property name, you could use the [...] notation, here is an example: myObj.values[innerElement].myProp1 = false. You can find more details HERE
– Titus
Aug 29 at 15:00



[...]


myObj.values[innerElement].myProp1 = false




3 Answers
3



you can access subobject like accessing object prop


myObj.values.internal.myProp1 = false



Take care it will throw an error it at least one of the element of the chain (except myProp1) is not defined


myProp1



Like @titus said in comment you can also use the [...] notation


[...]




myObj =
id: 1,
values:
internal:
myProp1: false,
myProp2: true
,
external:
myProp1: true,
myProp2: true


;

// using basic notation
console.log(myObj.values.internal.myProp1)

// using [...] notation to access prop by name
console.log(myObj["values"]["internal"]["myProp1"])

// the [...] notation is generally used when the prop name is in a variable
let propName = "internal"
console.log(myObj["values"][propName]["myProp1"])

// can be mixed
console.log(myObj["values"].internal["myProp1"])
console.log(myObj.values[propName].myProp1)



I think this is what you want. You need to access the variable myObj itself.


Object.keys(myObj).forEach(element=>
if(element === "values")
Object.keys(myObj[element]).forEach(innerElement=>
if (innerElement === "internal")
myObj[element][innerElement].myProp1 = false;

)

)



If you're also taking suggestions, I would advice to use arrays to represent collections instead of objects. That makes the code to find anything inside the array way easier than looping over keys and values.



Smart data structures and dumb code usually work better than the other way.
Switching from objects to arrays for collections reduced the code I had to write for transformations by a huge margin.




const collection = [

"id": 1,
"internal": [
"name": "myProp1", "val": true ,
"name": "myProp2", "val": true
],
"external": [
"name": "myProp1", "val": true ,
"name": "myProp2", "val": true
]
,

"id": 2,
"internal": [
"name": "myProp1", "val": false ,
"name": "myProp2", "val": false
],
"external": [
"name": "myProp1", "val": null ,
"name": "myProp2", "val": null
]

];
const item_1 = collection.find( item => item.id === 1 );
const internal_prop = item_1.internal.find( prop => prop.name === 'myProp1' );
internal_prop.val = false;
console.log( collection );

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ