Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
var test=["a","b","c"];
console.log(JSON.stringify(test));
I am stuck to doing this. i want output like this
{"a","b","c"}
But I am getting like this
["a","b","c"]
Can someone help me?
Both JavaScript and JSON can represent data using a dictionary-style (as in objects using {} characters) or an array-style (a list of objects with the [] characters).
With dictionary-style objects:
You use the {key1:value1, key2:value2} format
You always have key and value for any entry
{"a":0, "b":1} is valid
{"a", "b"} is not valid
With an array:
You use the ["a", "b", "c"] format
You only have values (and their position), no keys
So then, {"a", "b", "c"} is meaningless in both JavaScript and JSON.
You are defining an array in JavaScript:
var test=["a","b","c"];.
That is why the JSON.stringify(test) shows an array with [] and not an invalid syntax such as {"a","b","c"}.
You can't convert an Array to JSON object but what you can do is create an object, store in it the array and then convert it.
something like this:
var obj = {test: ["a", "b", "c"]};
console.log(JSON.stringify(obj));
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 months ago.
Improve this question
I have a string which represents an array. The items in the array can be characters, or arrays. So a string might look like: [a,b,[[],[d]],[],[[[]]],[[c,[t,s]],b]]
And I want to parse that out so it's a proper array with subarrays etc
So "[a,b,[c,[d,e],[]]]" would become:
[ 'a',
'b',
[ 'c',
['d','e'],
[]
]
]
Is there an easy way to do this in JavaScript? Eg some kind of array equivalent of JSON.parse? I tried JSON.parse but it throws an error (not unexpectedly).
You would have to process it to convert it into a format that JSON.parse would be able to handle. Your test case is simple so it is possible with an easy regular expression.
const str = "[a,b,[c,[d,e],[]]]";
const obj = JSON.parse(str.replace(/([a-z]+)/gi,'"$1"'));
console.log(obj);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I want to get data from the object without for loop
this is my object
data = [{name:"A",value:[java:45,c++:50]},{name:"B",value:[java:12,c++:47]},{name:"C",value:[java:15,c++:32]}]
Expected result:
result_name = ["A","B","C"]
result_java = [45,12,15]
The structure of your array data is invalid. You wrote nested array value in square brackets yet the items are written as key-value pairs.
There is no magical way you can obtain multiple data from an array without iteration/looping. (unless you already know the indices, but this defeats the purpose of using arrays)
That said, if you meant to avoid only the "for" loops, you can:
Fix your data as below.
let data = [
{name:"A", value:{java:45,"c++":50}},
{name:"B", value:{java:12,"c++":47}},
{name:"C", value:{java:15,"c++":32}}
];
Run an alternative loop such as below.
console.log(data.map(item => item.name)); // ["A", "B", "C"]
console.log(data.map(item => item.value.java)); // [45,12,15]
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
business=[0:{"name":{"en":'prateek'}},1:{"name":{"ar":'rahul'}}]
How I can extract the value of en and ar from this type of the repeted object in a array
The issue with your question is that you define business as an Array with the use of square brackets, but then proceed to use key value pairs directly within the array (via the usage ":"), which is reserved for objects and not arrays. I'd recommend researching both array and object datatypes, however simply put:
let myArray = [1, 2, 3, ...];
// only stores values which can be retrieved using the values index i.e. myArray[0]
let myObj = {"key1" : "value1", "key2" : "value2"};
// values are stored against keys, and can be accessed via the key i.e. myObj.key1
I think you've confused how objects and arrays should work. My guess is that you'd be better off with this structure:
let businesses = [];
businesses.push({enName: 'prateek', arName: 'rahul'});
console.log(businesses[0], businesses[0].enName, businesses[0].arName);
This way you're using an array to hold a collection of businesses and which are represented by objects. These objects in turn have attributes for enName and arName.
I think this would be a much clearer way of structuring your issue.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
Instead of using associative arrays (objects), what is the best way to associate two values so they are always associated and can both be accessed as separate values? Associative arrays are annoying because their order isn't guaranteed and you can't access them using indexes.
I could create two separate arrays but if I randomize their order for display their association would not match, and if they are separate in code there is a good chance I can make a mistake when recording their values, putting them in the wrong order so they don't perfectly match up.
In Javascript you can use multidimensional arrays:
var data = [
[1, 2],
[3, 4]
];
data[0][0]; // get '1'
data[0][1]; // get '2'
Also if you need to associate data in a more abstract way, ES6 has WeakMaps:
var wm = new WeakMap();
var x = document.createElement("div");
wm.set(x, {foo: 1, bar: 7});
console.log(wm.get(x).bar) // get '7' from object you associated to HTMLElement
I do not now which kind of data you should store but maybe you could try to store data in a single variable in this way:
var data = [];
data[0] = "Test" + "|" + Value;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a javascript in the html to render the html on client. I know how to do it using JSON. My question is: Is it possible without JSON?
For example, my server will reply array such as [["good", "bad"],["first", "second", "third"]] using a servlet. I mention String array in topic, because the xmlhttp.responseText is regarded as a text or string. So in javascript, how do I convert this result into array variable?
Using JSON, my server has to reply
{
"1": ["good", "bad"],
"2": [....]
}
I just wanted to see whether we can avoid this key string.
As already pointed out in the comments, JSON has quote:
two primary data structures: ordered lists (recognized as 'arrays')
and name/value pairs (recognized as 'objects')
JSON objects are written inside curly brackets and can contain multiple key/value pairs.
JSON arrays are written inside square brackets (elements of the array can be basic types like number or string but also objects or arrays).
(from http://www.w3schools.com/json/json_syntax.asp)
So [["good", "bad"],["first", "second", "third"]] is in JSON format and doesn't need to be converted to an object.