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

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]);

Related

JavaScript positional property access in Objects [duplicate]

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]])

JSON Object key value pair function [duplicate]

This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed 3 years ago.
Why i am unable to pass "abc" to someKey
function convertToKeyValuePair(someKey,someValue){
var map = {someKey : someValue};
return JSON.stringify(map);
}
print(convertToKeyValuePair("abc","xyz"));
O/P = {"someKey":"sdfdf"}
Expected O/P = {"abc":"xyz"}
As you’re passing key dynamically so you’ve to use bracket around it like:
{ [someKey]: someValue }
You want dynamic keys.
let key = 'yo';
let obj = {key: 0}; // creates {"key": 0}
let objDynamic = {[key]: 0}; // creates {"yo": 0};
console.log(obj);
console.log(objDynamic);
As answered by the comment of #junvar, passing keys to objects without quotes is syntactic sugar and both of the following examples will give the same result:
{ "someVar": someValue }
{ someVar: someValue }
To use the value of a variable as a key you have to use square brackets as in:
{ [someVar]: someValue }

Property Name gets lost? [duplicate]

This question already has answers here:
Variable as the property name in a JavaScript object literal? [duplicate]
(3 answers)
Closed 4 years ago.
I am working on a pre-boot camp coding problem. I am passing in an object (ourDog) and a key (fiendly) into a function that creates a new property (key:value pair) and assigns a value of true to that key.
Looking at the output, the property is added, but the key does not have the assigned value of 'friendly', it has the parameter name of key. I expected the key:value pair to (friendly: true). Here is the code.
var ourDog = {
"name": "Camper",
"legs": 4,
"tails": 1,
"friends": ["everything!"]
};
function addProperty(anObject, key) {
anObject.key = true;
return anObject;
};
var output = addProperty(ourDog, 'friendly');
console.log(output);
{name: "Camper", legs: 4, tails: 1, friends: Array(1), key: true}
Change it from dot notation to brackets.
function addProperty(anObject, key) {
anObject[key] = true;
return anObject;
};
Dot notation is used to access an object's properties when the property is a valid JavaScript identifier. Since 'friendly' is a string, you need to use brackets.

How to add a new property in object in javascript [duplicate]

This question already has answers here:
Is it possible to add dynamically named properties to JavaScript object?
(20 answers)
Closed 6 years ago.
I have one obj like
var obj = {
name : "Jenny",
roll : "1000"
}
Now I want to add a new property in obj called grade.
after that my object looks like
var obj = {
name : "Jenny",
roll : "1000",
grade :"7"
}
I am not getting any method from obj obj[op]=value. How to achive that.
It is an object so you can not do obj[op] = value
Instead you can do obj.op = value
In this case, obj.grade = 7 will serve the purpose.
You can do this.
Object.defineProperty(obj, 'grade', {
value: '7'
});

Cannot understand object with array as key [duplicate]

This question already has answers here:
Difference between ES6 object method assignment: a, 'a', and ['a']?
(2 answers)
Closed 6 years ago.
I've found some wild code on the web i don't understand:
return Object.assign({}, state, {
[action.subreddit]: posts(state[action.subreddit], action)
})
What is [action.subreddit] doing? I thought that object keys had to be strings but this appears to be an array?
I'm hoping to understand mechanically how this code works.
thank you!
That's not an array as key, it's the es6 way to use a variable (/ a computed property) as the key.
Consider this:
var a = "foo";
function getKey() {
return "myKey";
}
var obj = {
[a] : "bar",
[getKey()] : "baz"
};
console.log(obj.foo); // bar
console.log(obj.myKey) // baz
So [action.subreddit] just sets the key's name to whatever value action.subreddit is holding.

Categories

Resources