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

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

Related

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

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";

JSON object string to convert an array using map [duplicate]

This question already has answers here:
Merge multiple objects inside the same array into one object [duplicate]
(2 answers)
Closed 3 years ago.
Here is my response from my server.
var my_array = [{"1":"1"},{"2":"8"},{"4":"13"},{"5":"19"},{"6":"22"}]; //from server
I want to convert it like
var new_array = { 1 : "1", 2 : "8", 4 : "13", 5 : "19" , 6 : "22"}
But how to convert it using map function
new_array = my_array.map(function (a) {
return ???
});
Use reduce with spreading - you can't map an array to an object. Spreading also allows for multiple properties in each object.
var new_array = my_array.reduce((a, c) => ({ ...a, ...c }), {});
You could also use Object.fromEntries after flatMapping the entries:
var new_array = Object.fromEntries(my_array.flatMap(Object.entries));

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

Get information from a JavaScript object that has a number as an index [duplicate]

This question already has answers here:
Unable to access JSON property with "-" dash [duplicate]
(5 answers)
Closed 6 years ago.
How can I retrieve information from a JavaScript object that has a numeric value as an index?
"element_count": 69,
"near_earth_objects": {
"2016-10-29": [ ... ]
I need to access the data inside that "2016-10-29" Array.
I have no problem accessing the other elements like this:
$.getJSON(Call, function(data1){
console.log(data1.element_count);
});
Like this to mix notation and retrieve sub-array element in a property of your Json object :
var data = {
"element_count": 69,
"near_earth_objects": {
"2016-10-29": ["subelement1", "subelement2", "subelement3"],
"2016-10-30": ["subelement11", "subelement12", "subelement13"]
}
};
console.log(data.near_earth_objects["2016-10-29"][1]);
x = {
"near_earth_objects" :
{
"2016-10-29" : 1
}
}
console.log(x["near_earth_objects"]["2016-10-29"])

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