This question already has answers here:
How do you JSON.stringify an ES6 Map?
(16 answers)
Closed 3 years ago.
I have the following code:
const x = new Map([['key', 'value']])
Now, I want to stringify this. I used the following:
JSON.stringify(x)
...which gave me an empty object.
How do you actually perform the stringification on new Map()? Is it something like you first turn it into an object and then stringify it?
I am expecting the output to be {"key": "value"}.
You could get the array from the map and stringify the result.
const x = new Map([['key', 'value']])
console.log(JSON.stringify(Array.from(x)));
If you want to access direct value then you can use this in loop.
console.log(JSON.stringify(x.get('key')));
Related
This question already has answers here:
Why Doesn't Array Concatenation Work in Javascript? [closed]
(3 answers)
Closed 2 years ago.
I am getting below like values at runtime. Below is for only example. I will have same array structure at runtime. I want to merge them.
let finalSearchResult =[];
data [{"a":1000,"a":1000001,"a":10000002,"D":5000000}]
data [{"P":1000,"Q":1000001,"R":10000002,"S":5000000}]
finalSearchResult.concat(finalSearchResult,data);
but its not working. When I am printing finalSearchResult its coming as null.
The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.
please refer to:
Array.prototype.concat documentation
Therefore:
finalSearchResult = finalSearchResult.concat(data);
Will be the correct way to concat the arrays.
This question already has answers here:
JavaScript associative array to JSON
(5 answers)
Closed 4 years ago.
I am trying to pass an array through jQuery's ajax. The problem is that when I try to pass the array created in JavaScript to JSON, it returns something empty. I even try console.log, but when I try to convert it to JSON there is nothing. Here is a representation of how I do it:
var data = [];
data['name'] = 'test';
data['mail'] = 'test';
data['pass'] = 'test';
console.log(JSON.stringify(data)); // result []
Every array is an object. You are assigning object properties with the data['name'] = 'test' syntax. Arrays are indexed with integers and they "must" be in sequence. try a[0] = 'foo'. or Array.push
This question already has answers here:
How to check if object property exists with a variable holding the property name?
(11 answers)
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 5 years ago.
I'm attempting to run a list of strings through an object. When I do it individually it works, but when I pass it through as a string it doesn't work. How would I fix this?
// this doesn't work
var a = "IntegrationItem1";
var data = faq.a;
// but this works
var data = faq.IntegrationItem1;
What's causing the first example to not work? Is the variable data seeing it as faq."IntegrationItem1" instead of faq.IntegrationItem1?
You can access properties of the object using it's names:
var a = "IntegrationItem1";
var data = faq[a];
what you need is faq["IntegrationItem1"] => faq[a]
This question already has answers here:
Javascript get Object property Name
(4 answers)
Closed 6 years ago.
I have the following JSON data: {"success":"You are welcome"} that I have named json in my JavaScript code.
When I want to alert You are welcome I do json.success. So now the problem I am facing is that, what about if I want to alert success. Is there any way to get it?
So now the problem I am facing is that, what about if I want to alert
success. Is there a need way to get it ?
If your object is
var obj = {"success":"You are welcome"};
You can get the array of keys as
var keys = Object.keys(obj);
and then print it as
console.log( keys[ 0 ] ); //or console.log( keys.join(",") )
var obj = {"success":"You are welcome"};
var keys = Object.keys(obj);
console.log(keys[0]);
You mean something like this?
keys = Object.keys(json_object)
key_to_use = keys[0];
Try this code
alert(Object.keys({"success":"You are welcome"})[0]);
Since you're able to do json.success, you don't have "JSON data", you have a Javascript Object. JSON, or JavaScript Object Notation, is no more than the serialization of a Javascript object.
As other answers have stated, you can use Object.keys() to list the fields of an object.
Object.keys() can be called on any JavaScript object to get back a list of keys.
This question already has an answer here:
JSON Stringify Removing Data From Object
(1 answer)
Closed 6 years ago.
I am trying to set up an associative array in JavaScript and then store it in HTML5 Local Storage:
var student = [];
student["david"] = "He is doing very well";
localStorage['student'] = JSON.stringify(student);
var data = JSON.parse(localStorage['student']);
alert(data.david);
There is no such thing as an "associative array" in JavaScript. You are using a regular array which requires the use of numeric indexes, and adding arbitrary properties to it, which are ignored when you serialize the array to JSON.
The JSON-stringified result will be: "[]".
You need to use a real object:
var student = {}