Define object with variables for property names [duplicate] - javascript

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Use a concatenated (dynamic) string as JavaScript object key? [duplicate]
(6 answers)
Shorthand way to construct JS object with variable property name [duplicate]
(2 answers)
Closed 9 years ago.
I have a problem in declaring a inicialiting an object. When I define an object and pass by reference a string does not recognize me and fails. The object is as follows:
markerGroups = {"america": [], "europa": [], "asia": [],"africa": [], "oceania": [] };
Well, it works correctly, but if I change, for example, "america" ​​putting var amer = "america"​​, like this:
var amer = "america";
markerGroups = {amer: [], "europa": [], "asia": [],"africa": [], "oceania": [] };
It does not work. What i have to do for resolve this?

In JavaScript, you don't need to quote your object keys. So amer: [] is creating the literal key "amer".
You need to use the [] method to do this:
var amer = "america";
markerGroups = {...};
markerGroups[amer] = [];

something like this;
var markerGroups = {}
var amer = "america";
markerGroups[amer] = [];

Related

How to make name of an object key the value of a variable? [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 4 years ago.
var orderName = document.getElementById("orderName").value;
var order = ["5", "6"];
var Orders: {
orderName: order
}
JSON.Stringify(Orders); // returns {"ordername":[5,6]}
//Expected output would be {"Hamburger": [5,6]}
How can I make it so when i call JSON.Stringify(Orders) it returns the value of the element?
Use the square bracket property notation:
var orderName = "Hamburger";
var order = ["5","6"];
var obj = { [orderName]: order };
console.log(obj);

JSON.stringify multidimensional [duplicate]

This question already has answers here:
JavaScript associative array to JSON
(5 answers)
JSON.stringify doesn't work with normal Javascript array
(6 answers)
Closed 4 years ago.
I am trying to send a javascript object to PHP using JSON.stringify and I cannot find a solution. Here`s an example of what I have:
var small_array = [],
final_array = [];
small_array["ok"] = "ok";
small_array["not_ok"] = "not_ok";
final_array.push(small_array);
console.log(JSON.stringify(final_array));
The output is "[[]]"
Any guidance on this one? Thanks
You're adding non-array-entry properties to the array. That's fine in JavaScript, but JSON doesn't have the notion of non-array-entry properties in an array (JSON arrays are just ordered sequences, whereas in JavaScript arrays are fully-fledged objects that provide special treatment to certain kinds of properties — more in my [ancient] blog post A Myth of Arrays).
For those property names (keys), you'd want a plain object, not an array:
var obj = {}, // Note {}, not []
final_array = [];
obj["ok"] = "ok";
obj["not_ok"] = "not_ok";
final_array.push(obj);
console.log(JSON.stringify(final_array));
There are no associative arrays in javascript. They are called objects:
const smallObject = {
ok: "not ok",
not_ok: "ok"
};
const finalArray = [smallObject];
console.log(JSON.stringify(finalArray));
Objects in javacript are defined like this var obj = {}
var small_array = {},
final_array = {};
small_array["ok"] = "ok";
small_array["not_ok"] = "not_ok";
final_array = (small_array);
console.log(JSON.stringify(final_array));
VM1623:9 {"ok":"ok","not_ok":"not_ok"}

converting Javascript array of properties into an object with correct ordering of properties [duplicate]

This question already has answers here:
Does JavaScript guarantee object property order?
(13 answers)
Sorting a JavaScript object by property name
(7 answers)
Closed 6 years ago.
In my Javascript application, I have an array with few properties var sampleArray = ["Number_Info", "Additional_Ids","Additional_Ids_2","_summary"];Now I need to convert this array into an object containing properties in the same order, as shown below :
var sampleObject = {
"Number_Info" : {},
"Additional_Ids" : {},
"Additional_Ids_2" : {},
"_summary" : {}
}
I tried the following :
var orderingObj = {};
for(var i=0; i < sampleArray.length; i++ ){
orderingObj[sampleArray[i]] = {}
}
But here I am getting object properties in different order.
var orderingObj = {
"Additional_Ids_2" : {},
"Additional_Ids" : {},
"Number_Info" : {},
"_summary" : {}
};
How can I fix it? From earlier posts the solutions were there to sort by names but here I have a specific order in which I want to order my properties.

Create Arrays in JavaScript like in PHP [duplicate]

This question already has answers here:
Create an empty object in JavaScript with {} or new Object()?
(10 answers)
Closed 8 years ago.
can I use following (PHP) Syntax to create an array in JavaScript?
$arr = ("foo" => "bar", "foobar" => "test");
I dont want to use following syntax (in JS):
var arr = [];
arr["foo"] = "bar";
arr["foobar"] = "test";
Thank you very much!
var arr = {foo:"bar", foobar:"test"};
var foo = arr.foo;
var foobar = arr.foobar;

Any better way to make a object into another object in javascript [duplicate]

This question already has answers here:
How can I merge properties of two JavaScript objects dynamically?
(69 answers)
Closed 9 years ago.
Oldobject = {'name':'Joe','age':12};
newObject = {'address':'XXX'};
I want to get result like :
{'name':'Joe','age':12,'address':'XXX' }
I use
Oldobject.address = newObject
Is there any graceful way to do this ?
for (var property in Oldobject) {
newObject[property] = Oldobject[property];
}
Use a for... in loop, like:
for( var property in newObject ){
OldObject[property] = newObject[property]
}
This will put all of the stuff from newObject into oldObject.

Categories