This question already has answers here:
Converting a string to JSON object
(10 answers)
Closed 3 years ago.
Is there any in build function in javascript that let's me convert string "[[16, [8, 2], 4], 2, 80]" to an array of array or int [[16, [8, 2], 4], 2, 80]
You can use:
JSON.parse("[[16, [8, 2], 4], 2, 80]")
Related
This question already has answers here:
Convert array to JSON
(12 answers)
Closed 6 months ago.
This post was edited and submitted for review 5 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Array:
myArr = [[1, 2, 3], [4, 5, 6]]
Expected output:
newArr = "[[1, 2, 3], [4, 5, 6]]"
I have tried:
myArr.toString()
String(myArr)
myArr = `${myArr}`
What I got by doing the above methods:
'1,2,3,4,5,6'
I gather what you would like to achieve is more or less serialization. We could use JSON.stringify in JavaScript to serialize an Array.
const array = [[1, 2, 3], [4, 5, 6]];
const serializedArray = JSON.stringify(arr));
To deserialize the Array, JSON.parse could be used.
const originalArray = JSON.parse(serializedArray));
JSON.strinify is what you need.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
const myArr = [[1, 2, 3], [4, 5, 6]];
const myArrString = JSON.stringify(myArr);
console.log(`Here is my string: ${myArrString}`);
use JSON.stringify(myArr);
you can find out more on below link
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
This question already has answers here:
Check if an array includes an array in javascript
(3 answers)
Closed 10 months ago.
Welcome,
I need to find whether array contains array
I tried two ways(indexOf and includes),but both returns a negative result.
let myarray = [
[4, 2],
[2, 2],
[2, 2],
[2, 4],
[2, 2],
];
console.log(myarray.includes([4, 2]));//Returns false
console.log(myarray.indexOf([4, 2]));//Returns -1
I then thought of having a foreach loop,and checking the array(using ==),
But for that even console.log([4, 2]==[4, 2]);//Returns false
Please Help-I know this is simple for you,
Use some and Array.isArray. some will return true if any one condition in the callback function is true and use Array.isArray to check if the element is an array
let myarray = [
[4, 2],
[2, 2],
[2, 2],
[2, 4],
[2, 2],
];
const hasNestedArray = myarray.some(elem => Array.isArray(elem));
console.log(hasNestedArray)
This question already has answers here:
How to sort 2 dimensional array by column value?
(14 answers)
Sort array of objects by string property value
(57 answers)
Closed last year.
My values in a nested array like in the code and I want to sort it by the item name, but the array remain unchanged after the sort.
let arr1 = [
[88, "Bowling Ball"],
[2, "Dirty Sock"],
[3, "Hair Pin"],
[5, "Microphone"],
[3, "Half-Eaten Apple"],
[7, "Toothpaste"] ]
arr1.sort( (a, b) => a[1] - b[1]);
console.log(arr1);
This question already has answers here:
How to create windowed slice of array in JavaScript?
(3 answers)
Closed 3 years ago.
I want solve a function called aperture which accept a number and an array that should return new array that should be composed of subarrays the size of the number with consecutive elements, for example aperture:
(3, [1, 2, 3, 4, 5]); // [[1, 2, 3], [2, 3, 4], [3, 4, 5]]
Create an empty array whose length is equal to the given number and use reduce() on it.
const aperture = (num,arr) => [...Array(num)].reduce((ac,_,i) => {
ac.push(arr.slice(i,num+i));
return ac;
},[])
console.log(aperture(3,[1,2,3,4,5]))
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)
Finding matches between multiple JavaScript Arrays
(13 answers)
Closed 5 years ago.
Let's take two arrays for example:
a = [1, 2, 3, 4, 5]
b = [4, 5, 6, 7, 8]
Now there is duplicates as we see 4 and 5. How to make new array from them instead of getting rid of them. What is the easiest solution?
So new array should be like:
newArray = [4, 5]
Thank you guys in advance!
You can do it using Array.filter() and Array.includes()
let a = [1, 2, 3, 4, 5];
let b = [4, 5, 6, 7, 8];
let arr = a.filter(function(x){
return b.includes(x);
})
console.log(arr);