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);
Related
This question already has answers here:
Get a random item from a JavaScript array [duplicate]
(13 answers)
Getting a random value from a JavaScript array
(28 answers)
Closed 6 years ago.
For example
var names = array["bob","tom","jake"];
how could I select a random name from that array and assign it to the variable
var randomName = I don't know what goes here
You should use Math.random method.
var random=Math.floor((Math.random() * names.length));
var randomName=names[random];
Also, in javascript the arrays are declared like this :
var names = ["bob","tom","jake"];
not
var names = array["bob","tom","jake"];
var names = ["bob","tom","jake"];
var random= Math.floor((Math.random() * names.length));
var randomName=names[random];
console.log(randomName);
Use random method
var randomName = names[Math.floor(Math.random()*items.length)];
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.
This question already has answers here:
Adding a new array element to a JSON object
(6 answers)
Closed 7 years ago.
I have JSON as
var newJSON = [{"key":"India","value":"72"},{"key":"India","value":"27"},{"key":"Pakistan","value":"90"},{"key":"Zimbamwe","value":"88"},{"key":"India","value":"100"},{"key":"Pakistan","value":"172"}]
I want desired result as
[{"key":"India","value":"199"},{"key":"Pakistan","value":"262"},{"key":"Zimbamwe","value":"88"}]
Please help me with this
are you sure you want them as {"india","20"}??
or is it {"india":"20"} //this is recommended
then you can do
json["india"]
or json[0]
Update:
var yourObj={key:'india', value:72};
var newObj = {};
newObj[yourObj.key] = yourObj.value;
newObj["Austria"]=100; // adds as a new list object
You could use
var jsondata =[];
var firstvalue = {country:"India",number:"200"};
jsondata.push(firstvalue);
var secondvalue = {country:"Australia",number: "210"};
jsondata.push(secondvalue);
for(var i =0; i< jsondata.length;i++){
console.log(jsondata[i].country);
}
This question already has answers here:
Merge/flatten an array of arrays
(84 answers)
Closed 7 years ago.
I have a array with 3*3 dimension(2d) like this:
a[3][3]=[[1,2,3],[4,5,6],[7,8,9]];
I want formulate this array to access in single array(1d). how I can do this?
like:
b[9]=[0,1,2,3,4,5,6,7,8,9];
you want to convert a 2d array to a flat array. How about:
var a = [[1,2,3],[4,5,6],[7,8,9]];
var merged = [];
merged = merged.concat.apply(merged, a);
see https://stackoverflow.com/a/10865042/1432801
var flatArray = [];
for(var i=0;i<a.length;i++){
for(var j=0;j<a[i].length;j++){
flatArray.push(a[i][j]);
}
}
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];
});