Finding values of a mapping/dict [duplicate] - javascript

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

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

Convert array to object with key and value in javascript [duplicate]

This question already has answers here:
Convert an array to an array of objects
(4 answers)
Closed 5 years ago.
How covert or add array elements into object.
Now
arr = [
["1/1/2017",113],
["3/11/2017",58],
["3/12/2017",70],
["3/13/2017",76]
]
Should be
arr = [
{date:"1/1/2017", count:113},
{date:"3/11/2017", count:58},
{date:"3/12/2017", count:70},
{date:"3/13/2017", count:76}
]
arr.map(function(arr1) {
return {
date : arr1[0],
count : arr1[1]
}
})

How to remove duplicated objects from an Array in javascript [duplicate]

This question already has answers here:
Remove duplicate values from JS array [duplicate]
(54 answers)
Closed 6 years ago.
I am using above code and removing the items based on id
I am getting id is not defined while comparing i+1 item when i reaches to maximum array length.
Appreciate your help....
var arr = [{"id":"0-block-0","left":206.5,"top":0},{"id":"0-block-1","left":446.5,"top":0},{"id":"0-block-2","left":474.5,"top":16},{"id":"0-block-2","left":686.5,"top":0}];
Expecting outout = [{"id":"0-block-0","left":206.5,"top":0},{"id":"0-block-1","left":446.5,"top":0},{"id":"0-block-2","left":686.5,"top":0}]
Array.prototype.unique = function(){
var passNum = this.length;
var _self = this;
// to repeat loops n*n times
while(passNum>0){
for(var i = 0;i<this.length;i++) {
if(this[i].id==this[i+1].id){
var _indx = this.indexOf(this[i]);
this.splice(_indx,1);
}
}
passNum = passNum-1;
}
return this;
};
You can do this with ES2015 Sets.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
EDIT: If you can't use ES2015, there is a polyfill available.

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

Categories

Resources