Form array of property names found in a JavaScript Object [duplicate] - javascript

This question already has answers here:
Get array of object's keys
(8 answers)
Closed 7 years ago.
I have the following object
var columns = {ContributionType: "Employer Contribution",
Employee1: "0",
Employee2: "0",
Employee3: "0"
};
From this I need to form an array with they property keys alone like following
var keys=["ContributionType", "Employee1", "Employee2", "Employee3"];
The number of properties is dynamic
Question:
How can I achieve this using lodash or pure JavaScript?

Object.keys()
var columns = {ContributionType: "Employer Contribution",
Employee1: "0",
Employee2: "0",
Employee3: "0"
};
var keys = Object.keys(columns);
console.log(keys);

var arr=[];
for (var key in columns)
{
//by using hasOwnProperty(key) we make sure that keys of
//the prototype are not included if any
if(columns.hasOwnProperty(key))
{
arr.push(key);
}
}

Related

Get column (field) names from javascript Array [duplicate]

This question already has answers here:
How to list the properties of a JavaScript object?
(18 answers)
Closed 6 months ago.
I am looking to pull the list of column(field) names from this array. So that I end up with an array containing ['EntityId', 'RiskDescription', 'ThirdColumn', 'FourthColumn'].
var risks = [];
risks.push({
EntityId: this.EntityID
RiskDescription: this.RiskDescription
ThirdColumn: this.ThirdColumn,
FourthColumn: this.FourthColumn
});
You can use Object.keys to get the list of keys in that object
var risks = [];
risks.push({
EntityId: "value",
RiskDescription: "value",
ThirdColumn: "value",
FourthColumn: "value"
});
console.log(Object.keys(risks[0]))

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

How to construct object path dynamically [duplicate]

This question already has answers here:
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Closed 4 years ago.
I have a JSON object and I am iterating through it. I am using different values from different levels of it.
But I am not able to create path dynamically to reiterate the object.
var data= {
"algoName": "textClassification",
"hyperParams": {
"mode": {
"data_type": "string",
"default_value": "supervised",
"required": true,
"description": "The training mode",
"allowedValues": [
"supervised",
"unsupervised"
]
}
}
}
var key1;
for(var key in data.hyperParams) {
key1=key;
}
var text1 = "data.hyperParams" + key1
for(var key in text1) {
console.log(text1[key]);
}
Thank you T.J. Crowder. I modified code as following and it is working now.
for(var key in data.hyperParams[key1]) {
console.log(data.hyperParams[key1][key]);
}

How to create an object with the passed object's property names and values? [duplicate]

This question already has answers here:
What is the most efficient way to deep clone an object in JavaScript?
(67 answers)
How do I correctly clone a JavaScript object?
(81 answers)
Closed 5 years ago.
I have an object like result: object which has the below parameters in it.
calcTimeHY2: "50",
calcTimeHY3: "70",
color: "GREEN",
width: "3",
channels: Array(1)
I am writing a general method to create similar result object.
I have called getClonedResult(result)
getClonedResult(result){
let temp: any;
var propertyNames = Object.getOwnPropertyNames(result);
for (var name in propertyNames) {
temp = propertyNames[name];
if (!temp instanceof Array) {
//Then I want to create similar object as above
//How to do it.
}
}
}
I have both arrays and single values in the same object, how to clone to a new object.

Finding values of a mapping/dict [duplicate]

This question already has answers here:
How to get all properties values of a JavaScript Object (without knowing the keys)?
(25 answers)
Closed 9 years ago.
I have map/dictionary in Javascript:
var m = {
dog: "Pluto",
duck: "Donald"
};
I know how to get the keys with Object.keys(m), but how to get the values of the Object?
You just iterate over the keys and retrieve each value:
var values = [];
for (var key in m) {
values.push(m[key]);
}
// values == ["Pluto", "Donald"]
There is no similar function for that but you can use:
var v = Object.keys(m).map(function(key){
return m[key];
});

Categories

Resources