I have a multi-dimensional array like this:
1 2 3
4 5 6
Now I need to convert this array into a string like 1,2,3;4,5,6.
Can any one suggest how to do this, please?
simply use the join method on the array.
> [[1,2,3],[4,5,6]].join(';')
'1,2,3;4,5,6'
It's lucky that you simply don't have to consider how the apply the join method on the inner lists, because a list is joined by comma by default. when a list is coerced into a string, it by default uses commas to separate the items.
As it was already mentioned by qiao, join() is not recursive.
But if you handle the recursion yourself you should acquire the desired result, although in a rather inelegant way.
var array = [[1,2,3],[5,6,7]];
var result = [];
array.forEach(
function(el){
result.push(
el.join(",")
);
});
result.join(";");
If you need to serialize an array into a string and then deserialize it later to get an array from the string you might want to take a look at JSON:
http://www.openjs.com/scripts/data/json_encode.php
Try this:
array.toString();
See here for reference: http://www.w3schools.com/jsref/jsref_tostring_array.asp
See answer by qiao for a much nicer approach to multidimensional arrays like this.
Related
This question already has answers here:
Easy way to turn JavaScript array into comma-separated list?
(22 answers)
Closed 2 years ago.
I have 4 different values, when looping through an array. Is it possible to add the values together like you would in Java's StringBuilder append?
I want something like this when doing a console.log():
28.0334307,-25.872523799999996, 28.031527552564445,-25.87632233243363
Now I am just getting it one for one like this when doing a console.log():
28.0334307
-25.872523799999996
28.031527552564445
-25.87632233243363
Here is my code:
var coordinates = [28.0334307, -25.872523799999996, 28.031527552564445, -25.87632233243363]
for(var item in coordinates)
{
console.log(item);
}
you can get a string in-line separated by a comma with join() method, try this:
var coordinates = [28.0334307, -25.872523799999996, 28.031527552564445, -25.87632233243363]
console.log(coordinates.join(', '));
Try this:
var coordinates = [28.0334307, -25.872523799999996, 28.031527552564445, -25.87632233243363]
console.log(coordinates.join(' '));
var coordinates = [28.0334307, -25.872523799999996, 28.031527552564445, -25.87632233243363]
console.log(coordinates.join(' '));
The browser consoles displays an array like that in to be more readable. It's not an actual structural representation of how an array is. What you want is basically a string created by joining the elements of the array.
Array.join method can be used for this:
coordinates.join("'")
Use JavaScript array join() method to display values separated by comma.
The join() method returns the array as a string.
The elements will be separated by a specified separator. The default separator is a comma (,).
In my web application I receive a JSON string from the server which I keep in the greetings variable:
var greetings = '{"2":"hoi","3":"hi","1":"salam"}'
Please notice how the greetings start with the index 2 and the value hoi.
Now I want to parse the JSON and the result is the following:
JSON.parse(greetings) // {1: "salam", 2: "hoi", 3: "hi"}
The order has changed, it seems like JSON.parse orders the result by key.
Is there a way to keep the order of the original string intact?
{
"2":"hoi",
"3":"hi",
"1":"salam"
}
is not an array, its an object. Objects don't have any order.
If the order is important, you need to switch to an actual array.
You generally cannot rely on the order of indices in an object. Use an array of key/value pairs instead.
As you can see the keys are parsed to (numeric) indices, which is why they are ordered that way. You could hack around this by prefixing your keys and then stripping those later:
console.log(JSON.parse('{"i2":"hoi","i3":"hi","i1":"salam"}'))
I have one array [1,2,3,-4,-1,4] and want it to be sorted in order [-4,1,-1,2,3,4]. i am separating positive and negative array in new array and sorting by index.Is there any simple way to print?
Assuming that you mean [-4,-1,1,2,3,4]: You could use JavaScript's sort()-method. You can read about this method here.
underscorejs - How to remove array?
I have an array of objects. I would like to remove an array. Please refer my code below for more details,
array = [{a:10,b:20},{c:10,b:20},{a:10,d:20}];
Expected output: {a:10,b:20},{c:10,b:20},{a:10,d:20}
As I understand You need output without []. To manage that first stringify array, next cut first and last letter.
var array = [{a:10,b:20},{c:10,b:20},{a:10,d:20}];
var str=JSON.stringify(array);
console.log(str.substring(1,str.length-1));
Final string has no [] signs but this is not valid JSON structure. In JSON must be one root element like [] or {}.
I have been using the following code:
formData.objectiveDetails.push(emptyObjectiveDetail);
This pushes a new emptyObjectiveDetail object onto the end of an array called objectiveDetails.
If for example the array of objectiveDetails contains 13 objects then how could I remove the one at position 5? I assume I could make this null but what I want to do is to completely remove it so the length of the array becomes 12.
This might be off topic but I have been considering adding underscore.js. Is this something that could be done with underscore?
formData.objectiveDetails.splice(5, 1)
First argument is the array index and the second the number of items to remove starting from that index.
You can use Splice to remove the object from the array. Something like this:-
formData.objectiveDetails.splice(5, 1)
Using underscore.js
objectiveDetails = _.without(objectiveDetails, _.findWhere(arr, {id: 5}));