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;
Related
This question already has answers here:
How can I add a key/value pair to a JavaScript object?
(26 answers)
Closed 2 years ago.
const obj = {name:'James',age:25};
const address = [{add1:'USA'},{add2:'UK'}]
Expecting : obj={name:'James',age:25,tag:[{add1:'USA'},{add2:'UK'}]};
Tried does not work for me
Object.entries(address).forEach(([key,value]) => { obj[key] = value })
let obj = {name:'James',age:25};
let address = [{add1:'USA'},{add2:'UK'}];
obj['tag'] = [...address];
const resultObj = obj;
console.log(resultObj);
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);
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"}
This question already has answers here:
Javascript array length incorrect on array of objects
(3 answers)
Closed 8 years ago.
I don't understand why is array so strange when I add element by key.
For example:
var test = new Array();
test['a'] = 'aaa';
test['b'] = 'bbb';
test.length == 0; // true. Why?
And when I run:
test.map(function(el){
console.log(el);
});
// nothing print. Why?
When I add element with push method everything works OK. Thanks
Arrays in javascript are not associative, you can't set keys like that i.e.
var test = [];
test.push('aaa');
test.push('bbb');
// OR
test[0] = 'aaa';
test[1] = 'bbb';
test.length = 2
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] = [];