How can I convert a JavaScript object to an array - javascript

I have the following javascript object, in an array. I am using PHP's json_encode to get this value
[ { January=100}, { February=100} ]
I am trying to convert the following objects to
["January",100],["February",100]
I have searched around a lot and couldn't find an answer.
Any help would be really appreciated. Thanks!

Assuming you're trying to do this in Javascript, it can be done very easily using a library like Lodash:
server_array.map(_.toPairs)
Using native Javascript map function, with lodash's toPairs

Ignoring the incorrect syntax of your example javascript object code...
This is how to convert a javascript object to an array:
var newArray = []
for (var i in yourObject)
if(yourObject.hasOwnProperty(i))
newArray.push([i,yourObject[i]])
The above will yield the results your looking for.

Related

cloning a 2d array using JSON.stringify()

How are 2d arrays cloned using json.stringify and json.pasrse ?I came across this function which does but couldn't get the technique.What are some other methods to achieve the same?
Note: The JSON.stringify() method converts a JavaScript value to a JSON string.
let g2 = arrayclone(this.state.gridfull);// note gridfull is 2d array
function arrayclone(arr) {
return JSON.parse(JSON.stringify(arr));
}
You could use ES6 deestructurtig:
[...arr]
Or you could you Array.from:
Array.from(arr)
Or you could use concat:
[].concat(arr)
Or you could use slice:
arr.slice()
arr.slice(0)
You could create an empty array and iterate over your original one pushing elements to the new one.
I think that's pretty much it.
Hope it helps.

Turn JSON string with duplicate keys into JSON string arrays

I'm trying to generate a JSON string which will contain all of my filters, but I constantly stuck with duplicate keys. So, I want to find a solution that turns the duplicate keys into a JSON array.
For example, I have this JSON object:
{
"filter-1": "value-1",
"filter-1": "value-2",
"filter-2": "value-3",
"filter-3": "value-4"
}
And I want to turn it into this:
{
"filter-1": ["value-1", "value-2"],
"filter-2": "value-3",
"filter-3": "value-4"
}
Can someone point me in the right direction? I would appreciate solutions in JavaScript but any method would be more than welcome! Thanks in advance!
The duplicate key-pairs are causing overwrite issues.
Javascript objects doesn't allow duplicate keys.
var testObj = JSON.parse('{"filter-1":"value-1","filter-1":"value-2","filter-2":"value-3","filter-3":"value-4"}'); will overwrite the first key-pair (filter-1: value-1) when it parses the second key-pair (filter1: value-2) since both key-pairs have the same key.
However, JSON specification (not Javscript objects) does not specifically mention whether duplicate keys are allowed or not. You may wish to write your own parsing function to handle the duplicate keys.
You will have to change the format of your JSON since keys in JS objects must be unique.
Then you can hard coded or use libraries like jquery or underscorejs to group them out.
https://jsfiddle.net/p5fkjcwt/1/
var objects =
{
0: {"filter": "filter-1", "value":"value-1"},
1: {"filter": "filter-1", "value":"value-2"},
2: {"filter": "filter-2", "value":"value-3"},
3: {"filter": "filter-3", "value":"value-4"}
}
var result = _.groupBy(objects,"filter")
console.log(result)

can we use arr[key:value] in javascript?

In so many Javascript libraries and Angular plugin, I have seen the use of JS array like this:
var arr = ['size':20, 'Active', 'role':'user'];
(This is just an example how they use key and values)
I searched for javascript array but it looks different so how can we use this type of array?
Short answer: You can't write it like this because of the javascript syntax. You can still achieve this, but you shouldn't.
Long answer:
Arrays are considered as objects in Javascript. So you can do this for example :
var array = [];
array['size'] = 20;
console.log(array['size']);
It will work because the array works like any other objects. But you shouldn't do it. It will just confuse your code. Use a plain object to do that:
var obj = {'size':20, 'Active': undefined, 'role':'user'};

Transforming this JSON via underscore or a javascript utility library?

I have some JSON that looks as so:
[{"target": "mydata.12.2", "datapoints": [[763.7, 1368821100], [762.1, 1368821160], [223.11, 1368821220], [886.54, 1368821280], [112.3, 1368821340]]}]
I'd like to remove the first key and surrounding array brackets so it reads:
{"datapoints": [[763.7, 1368821100], [762.1, 1368821160], [223.11, 1368821220], [886.54, 1368821280], [112.3, 1368821340]]}
My javascript parsing skills leave a bit to be desired and I was hoping someone could help. Perhaps underscore.js has something that could assist here?
No need for a library. Supposing your first array is called obj1, you just have to do
var obj2 = obj1[0];
delete obj2['target'];
Note that you seem to have no JSON here, just plain javascript objects.
Supposing you really want to start from JSON and get some JSON at end, parse then stringify :
var obj2 = JSON.parse(json1);
delete obj2['target'];
var json2 = JSON.stringify(obj2);

How to convert this string to some array?

I am using one 3rd party plugin which uses stringify and gives me something like:
["ProjectB","ProjectA","Paris"]
It was an array but it used stringify and serialized into this format.How do I get back my array from this? Now I could very well use split and then remove 1st and last character from every string and get it but I don't want to do that manually. Is that any built in utility that can do that for me?
Assuming you have like var str = '["ProjectB","ProjectA","Paris"]';
Try using,
var array = JSON.parse(str); //will return you an array
As #AlexMA pointed out: JSON.parse is not supported in old browsers so you are better off using jQuery version like below,
var array = $.parseJSON(str);
You can use
JSON.parse(your_arr_str);
or jquery
$.parseJSON(your_arr_str);

Categories

Resources