This question already has answers here:
Preserve Nested Array Structure When Converting to String, JavaScript
(2 answers)
Closed 2 years ago.
my question is, if I have an array in javascript, let's say :
let x = [1, 2, 3, [4, 5, 6]]
And I want to transform it into a string that would look like this : y = "[1, 2, 3, [4, 5, 6]]", how should I do it?
I tried this things:
let a = x.toString();
let b = y.toLocaleString();
let c = new String(x);
But the problem with it is that, all of them look like this:
"1, 2, 3, 4, 5, 6"
So, it completely removes the '[]'.
How can I keep the array inside the string like this :
"[1, 2, 3, [4, 5, 6]]"
with the []
Does this helps?
let y = [1, 2, 3, [4, 5, 6]];
var x = JSON.stringify(y);
console.log(typeof(x));
console.log(JSON.stringify(y));
Related
This question already has answers here:
Variable name as a string in Javascript
(20 answers)
Closed 1 year ago.
I have several arrays that I json_encode form php and two variables that I get from an inputs, lets say :
let a = [1,2,3,4,5];
let b = [1,2,3,4,5];
let c = [1,2,3,4,5];
let d = [1,2,3,4,5];
let e = [1,2,3,4,5];
let f = [1,2,3,4,5];
let name = document.getElementById("some_element").value
let number = document.getElementById("some_other_element").value
I wanna check if the name is the same as the array name and then use the array with the name for example a[number].
I am not quite sure of how to do this. Any help would be really welcome.
Use an object:
const arrays = {
a: [1, 2, 3, 4, 5],
b: [1, 2, 3, 4, 5],
c: [1, 2, 3, 4, 5],
d: [1, 2, 3, 4, 5],
e: [1, 2, 3, 4, 5],
f: [1, 2, 3, 4, 5],
};
const name = "a";
const number = 1;
console.log(arrays[name][number]);
This question already has answers here:
Split array into chunks
(73 answers)
Closed 2 years ago.
Suppose that you have an array of doubles in Javascript:
double_arr = [1, 2, 3, 4, 5, 6, 7, 8]
What is the most efficient way to convert it into an array of arrays with 2 doubles like above:
double_arr = [[1,2], [3,4], [5,6], [7,8]]
You can iterate skipping one index in each iteration, like this:
const double_arr = [1, 2, 3, 4, 5, 6, 7, 8];
const result = [];
for (let i = 0; i < double_arr.length; i += 2)
result.push([ double_arr[i], double_arr[i+1] ]);
console.log(result);
This question already has answers here:
Split array into chunks
(73 answers)
How to put each element of array into table row - table data?
(1 answer)
Closed 3 years ago.
sorry I don't speak English very well.
I have a table with several elements, I want to return all the X elements a new table inside a table.
For example:
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
let newArr = [
[1,2,3],
[4,5,6],
[7,8,9],
[10,11,12],
];
This is the way I am currently doing this :
const size = shuffleImages.length / 4
while (shuffleImages.length > 0) {
columns.push(shuffleImages.splice(0, size))
}
I would like to understand how to do this without using while and instead use for example .map or is there just an easier way to do this?
You need to split the array into small array using map and than slice it :
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
var chunkSize = 3;
const groups = arr.map( function(e,i){
return i%chunkSize===0 ? arr.slice(i,i+chunkSize) : null;
}).filter((e) =>e);
console.log(groups);
This question already has answers here:
Simplest code for array intersection in javascript
(40 answers)
Compute intersection of two arrays in JavaScript [duplicate]
(4 answers)
Closed 5 years ago.
var array1 = [1, 2, 3, 4, 5, 6];
var array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9];
1.)If elements are equals,show that common elemenst in output
2.)The output(common elements) should be array form
Use Array#filter method and inside filter function use Array#indexOf or Array#includes methods to check second array includes the element.
var array1 = [1, 2, 3, 4, 5, 6];
var array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9];
var res = array1.filter(function(v) { // iterate over the array
// check element present in the second array
return array2.indexOf(v) > -1;
// or array2.includes(v)
})
console.log(res);
This question already has answers here:
Compare 2 arrays which returns difference
(10 answers)
Closed 5 years ago.
This is isn't really a question, it's a solution but I wanted to post it because I've seen it come up frequently. Feel free to suggest improvements though. I'll update my Fiddle with the results.
Using jQuery, this compares 2 arrays and outputs the differences in the two.
var array1 = [1, 2, 3, 4, 5, 6];
var array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9];
var foo = [];
var i = 0;
jQuery.grep(array2, function(el) {
if (jQuery.inArray(el, array1) == -1) foo.push(el);
i++;
});
alert(" the difference is " + foo);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
The library Underscore.js is very helpful for stuff like this.
http://underscorejs.org/#difference
_.difference([1, 2, 3, 4, 5], [5, 2, 10]);
=> [1, 3, 4]