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

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

Related

Sort array of array on string value in Javascript [duplicate]

This question already has answers here:
Sort array of objects by string property value
(57 answers)
Closed 6 years ago.
I want to sort the array of array on length of string, as follows
var arr = [
[1951, "Mayfer"],
[1785, "Actinac Capsules"],
[1007, "Ferri Injection"],
[1101, "Cetaphil"]
];
var sortedArr = sortOnLengthOfString(arr);
>> sortedArr = [
[1951, "Mayfer"],
[1101, "Cetaphil"],
[1007, "Ferri Injection"],
[1785, "Actinac Capsules"]
]
Is there a solution with lodash preferably? Or plain Javascript?
I haven't found duplicate question yet. Can someone find it? Please note, the sorting I am asking for, is for array of arrays and not array of objects.
You can use Array#sort at this context,
var obj = [
[1951, "Mayfer"],
[1785, "Actinac Capsules"],
[1007, "Ferri Injection"],
[1101, "Cetaphil"]
];
obj.sort(function(a,b){
return a[1].length - b[1].length
});
console.log(obj);
//[[1951, "Mayfer"],[1101, "Cetaphil"],[1007, "Ferri Injection"],[1785, "Actinac Capsules"]]

Add multiple options in array with javascript [duplicate]

This question already has answers here:
How can I add a key/value pair to a JavaScript object?
(26 answers)
Closed 7 years ago.
I have this var
var allwords = {};
And i need push more options in this array like:
allwords.push = {"Ctrl-Space": "autocomplete"};
allwords.push = {"Ctrl-pause": "closewindow"};
And look like this:
allwords = {"Ctrl-Space": "autocomplete", "Ctrl-pause": "closewindow"};
How can't i do?
push is for Array objects. For traditional objects, assign the properties manually like
var allwords = {};
allwords["Ctrl-Space"] = "autocomplete";
allwords["Ctrl-pause"] = "closewindow";
console.log(allwords);

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

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

Select all JSONObjects from JSONArray containing [duplicate]

This question already has answers here:
How to filter object array based on attributes?
(21 answers)
Closed 8 years ago.
I have a JSONArray containing JSONObjects like this {firstname:'John', lastname:'Doe'}.
Is there a way to select all JSONObjects having firstname == 'John' directly or the only way is to loop the array, test the field firstname and save all matching objects in another array?
Thanks
The filter method should do the trick.
var jsonArrayString = '[{"firstname": "John","otherProp": "otherValue"},{"firstname": "other Name", "otherProp": "otherValue"}]';
var jsonArray = JSON.parse(jsonArrayString);
var filteredArray = jsonArray.filter(function(element) {
return element.firstname === "John";
});
console.log(filteredArray);
Advice: The filter method is not supported in <= IE 8. but there is a polyfill in the MDN article.
You can filter them out:
var people = JSONArray.filter(function(obj) {
return obj.firstname == "John";
});
FYI: You have an array containing objects.

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