Javascript: Object key as key for another Object [duplicate] - javascript

This question already has answers here:
Is it possible to add dynamically named properties to JavaScript object?
(20 answers)
Closed 1 year ago.
I have a constant and I want to use it as a key of another object. However, it does not work. What is the best solution for this?
export const URL_QUERY_PARAMS = Object.freeze({
parentId: "parentId",
groupId: "groupId"
})
const queryParam = { {URL_QUERY_PARAMS.groupId : "test1",
"nodePath" : "test2"}

Use array format to make dynamic key
const queryParam = { "nodePath" : "test2" };
queryParam[URL_QUERY_PARAMS.groupId] = "test1";

Related

How do I parse the following nested object in JavaScript? [duplicate]

This question already has answers here:
Get values from an object in JavaScript [duplicate]
(6 answers)
Closed 2 years ago.
I have a JSON object containing another JSON object like below, the size of which is not fixed. I need to access the values in the inner object.
myObj = {
"name" : "XYZ",
"sex" : "F",
"questions" : {"1":"testquestion1", "2":"testquestion2", "3":"testquestion3"}
}
I need a way to extract the [testquestion1,testquestion2,testquestion3] using a loop.
Thanks in advance!
You don't need a loop, you can use Object.values():
let myObj = {
"name" : "XYZ",
"sex" : "F",
"questions" : {"1":"testquestion1", "2":"testquestion2", "3":"testquestion3"}
}
let result = Object.values(myObj.questions);
console.log(result);

immutably update value of a property in array of object [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.
I have this array of object
const d = [{
a_1: 1,
b_2: 2
}]
How can I update a_1 value to 2 without creating a temporary variable?
const myKey = 'a_1'
const myValue = 2
d.map(obj => ({...obj, obj[myKey]:myValue})) //why this won't work?
Remove the obj from obj[myKey] so that [myKey] is correctly seen as a computed property name.
const transformedDs = d.map(obj => ({...obj, [myKey]:myValue}))

Assign object variable name to a variable javascript or typescript [duplicate]

This question already has answers here:
Get array of object's keys
(8 answers)
Closed 4 years ago.
Can I get a variable name by code,
receivedData = { id : 1, name : 'adam', notes : ''};
how can I get the variable names in the object "id, name, notes", let's say I want to make a table from the received data and I can't hard code the columns names. is there a way to do this so I can dynamically set the columns names?
columnNames = ['id','name','notes'];
Use Object.keys
let receivedData = {
id: 1,
name: 'adam',
notes: ''
};
console.log(Object.keys(receivedData))

How to access a nested object with string paths? [duplicate]

This question already has answers here:
Convert a JavaScript string in dot notation into an object reference
(34 answers)
Closed 6 years ago.
I'm trying to access properties of a JavaScript object by passing a path (string or otherwise):
// In a loop
tableData[i].profile.firstname
where 'profile.firstname' is the path.
Is there a way to access a nested property based on a path in this way?
let firstnamePath = 'profile.firstname'
let firstname = tableData[i][firstnamePath]
Yes, but not with the syntax you've proposed. This is easiest done when your path is an array of strings:
const tableData = (
{ profile: { firstname: 'jim', lastname: 'johnson' }
}
)
const path = [ 'profile', 'firstname' ]
const valueAtPath = path.reduce((_, x) => _[x], tableData)
console.info(valueAtPath)

How do these methods for adding properties to existing objects in JavaScript differ (one works, one doesn't)? [duplicate]

This question already has answers here:
Is it possible to add dynamically named properties to JavaScript object?
(20 answers)
Closed 8 years ago.
If I start with an empty object, designed to hold a description and then an array of values (objects):
var obj = { "description" : "description",
"value" : [ {} ]
};
I can successfully add the required objects dynamically if they're nested together, like so:
obj.value[i] = { "Key1" : Parseddata[i][1],
"Key2" : Parseddata[i][2],
"Key3" : Parseddata[i][3]
};
but how can I add each object into the array separately (that is, not nested together in the same object)? For example, If start with:
obj.value[i] = { "Key1" : Parseddata[i][1] };
and then want to add Key2, Key3 in separate steps?
Try this.
obj.value[i] = {};
obj.value[i]["Key1"] = Parseddata[i][1];
obj.value[i]["Key2"] = Parseddata[i][2];
obj.value[i]["Key3"] = Parseddata[i][3];

Categories

Resources