JavaScript positional property access in Objects [duplicate] - javascript

This question already has answers here:
How to access the first property of a Javascript object?
(23 answers)
Accessing a JavaScript's object property without knowing that property name
(3 answers)
JavaScript: Get first and only property name of object
(7 answers)
How do I access properties of a javascript object if I don't know the names?
(8 answers)
Closed 3 years ago.
I have the following JavaScript object:
{
"_embedded": {
"dealerListItemDToes": [
{
...
},
{
...
}
]
}
}
Property called 'dealerListItemDToes' will always be at the given position in the object but its name can vary depending on the HTTP requests.
How can I access the property 'dealerListItemDToes' and retrieve its content without referencing its name?

Since it's the only property of the _embedded object, you could access the [0]th item in an array of object entries:
const obj = {
"_embedded": {
"dealerListItemDToes": [
{
// ...
},
{
// ...
}
]
}
};
console.log(
Object.entries(obj._embedded)[0]
);

You can try like this
let data = {
"_embedded": {
"dealerListItemDToes": [{
"h": 1
}]
}
}
console.log(data._embedded[Object.keys(data._embedded)[0]])

Related

Not able to get JSON value dynamically [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 4 years ago.
I am working on the following snippet. Why am I not able to get the
let node = 'Em2';
console.log(data.node.c2);
work? As you can see I am able to get data while passing data.Em2.c1 but a dynamic format like this let node = 'Em2'; console.log(data.node.c2); I am getting this error
TypeError: Cannot read property 'c2' of undefined
Code:
var data ={
"Em1": { "c1":"#FFF", "c2":"#EEE" },
"Em2": { "c1":"#DDD", "c2":"#ooo" }
}
let node = 'Em2';
console.log(data.Em2.c1);
console.log(data.node.c2);
var data ={
"Em1": { "c1":"#FFF", "c2":"#EEE" },
"Em2": { "c1":"#DDD", "c2":"#ooo" }
}
let node = 'Em2';
console.log(data.Em2.c1);
console.log(data.node.c2);
Use square braces [] to access object member via variable
var data ={
"Em1": { "c1":"#FFF", "c2":"#EEE" },
"Em2": { "c1":"#DDD", "c2":"#ooo" }
}
let node = 'Em2';
console.log(data.Em2.c1);
console.log(data[node].c2);
Similar question: how to access object property using variable
Instead of console.log(data.node.c2), try console.log(data[node][c2])

How to use an object key in another object's path? [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 5 years ago.
I have the following objects:
var obj1 {
key1: "path"
};
var obj2 {
path: "done"
};
I want to get "done" (obj2.path) in the scope of obj1 key1.
So the key "path" shall not be reached by obj2.path, but by obj1.key1.
Something like: obj2.(obj1.key1)?
i hope you can understand, sorry for my english :)
You can try using the [] notation to access property, which derived from obj1 key.
var obj1= {
key1: "path"
};
var obj2= {
path: "done"
};
console.log(obj2[obj1.key1]);

Javascript - Using Object literal with a number [duplicate]

This question already has answers here:
Using integer keys with dot notation to access property in javascript objects
(3 answers)
Closed 6 years ago.
Let's say I have an object:
var elObject = {
one: {
name: "Oliver"}
}
I can access name by doing elObject.one.name and everything is great, but let's say I have this instead:
var elObject = {
1: {
name: "Oliver"}
}
Suddenly, I can't access name through elObject.1.name anymore since I'm using 1 instead of 'one'. Is there a a special escape or something I'm supposed to use with object literal and digits?
You can declare the plain object with number 1 using it as string. Once is not allowed to have a property name starting with number, you can access it using bracket notation.
Example and findle below.
var x = {
'1' : {
name: 'Joao'
}
};
alert(x);
try {
alert(x['1'].name);
}
catch(e){
alert(e.message);
}
https://jsfiddle.net/b4c34wLv/

Javascript acces json path with additional characters [duplicate]

This question already has answers here:
How can I access object properties containing special characters?
(2 answers)
Closed 7 years ago.
I have a JSON Structure like this:
{
"SOAP: Envelope": {
"SOAP: Header": "",
"xmlns: SOAP": "http: //schemas.xmlsoap.org/soap/envelope/",
"SOAP: Body": {
"ns0: Z_ZBC_USAGE_GET_DATA.Response": {
"IT_AREA_RANGE": "",
"ET_USAGE": {
"item": [...]
}
}
}
}
}
In my JS I try to access to the items
reports.data = data.SOAP:Envelope.SOAP:Body.ns0:Z_ZBC_USAGE_GET_DATA.Response.ET_USAGE.item;
This didn't worked because : signs are not allowed. What should be the correct expression to get the items?
Just access properties like array keys:
reports.data = data['SOAP:Envelope']['SOAP:Body']['ns0:Z_ZBC_USAGE_GET_DATA.Response'].ET_USAGE.item;

Creating an object with dynamic keys [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed 7 years ago.
Here is a function building an object dynamically:
function onEntry(key, value) {
console.log(key) // productName
console.log(value) // Budweiser
const obj = { key: value }
console.log(obj) // { key: "Budweiser" }
}
Expected output is
{ productName: "Budweiser" }
But property name is not evaluated
{ key: "Budweiser" }
How to make property name of an object evaluated as an expression?
Create an object, and set its key manually.
var obj = {}
obj[key] = value
Or using ECMAScript 2015 syntax, you can also do it directly in the object declaration:
var obj = {
[key] = value
}

Categories

Resources