Applying parseInt on array yields weird result [duplicate] - javascript

This question already has answers here:
Why does parseInt yield NaN with Array#map?
(8 answers)
Closed 6 years ago.
Trying to run a simple javascript to parse single digit ints in a string as follows:
var s = "22123222222213123212322123213222";
var a = s.split("");
var b = a.map(parseInt);
console.log(b);
[2, NaN, 1, 2, 3, 2, 2, 2, 2, 2, 2, 2, 1, 3, 1, 2, 3, 2, 1, 2, 3, 2, 2, 1, 2, 3, 2, 1, 3, 2, 2, 2]
Why is there a NaN for element with index 1?
Fiddle in the console here:
https://jsfiddle.net/po6oy1ws/
EDIT
After getting the correct answer below I felt I had to lookup this "map(Number)" business. Turns out Mozilla has a "gotcha" clause concerning this specific case. Mozilla gotcha case

The parseInt has two parameters
parseInt(string, radix);
And map's callback accepts three parameters:
map(currentValue, index, array)
Therefore the index of the currentValue was passed as a radix to the parseInt function. Use parseInt with radix explicitly:
var s = "22123222222213123212322123213222";
var a = s.split("");
var b = a.map(e => parseInt(e, 10));
Or use Number instead of parseInt:
var s = "22123222222213123212322123213222";
var a = s.split("");
var b = a.map(Number);

Related

sorting an array in javascript, such that new starting and ending items but they have the same order [duplicate]

This question already has answers here:
Rotate the elements in an array in JavaScript
(42 answers)
Closed 1 year ago.
let's say I have this array of number in reverse order:
var days = [6, 5, 4, 3, 2, 1, 0];
and I have this variable:
var startingDay = 4;
How can I sort the array so that the array is like this
console.log(days == [4, 3, 2, 1, 0, 6, 5]);
>> true
??
Thanks!
You can rotate the array by slicing and concatenating the two parts.
var days = [6, 5, 4, 3, 2, 1, 0];
var day = 4;
var idx = days.indexOf(day);
var res = days.slice(idx).concat(days.slice(0, idx));
console.log(res);
You can also use unshift and splice to modify the array in-place.
var days = [6, 5, 4, 3, 2, 1, 0];
var day = 4;
days.unshift(...days.splice(days.indexOf(day), days.length));
console.log(days);

How to keep the array 'structure' inside a string in JavaScript [duplicate]

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));

How to find common elements only between 2 arrays in Angular 2 [duplicate]

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);

Duplicate numbers in array | Javascript

For the life of me I can't figure out how to duplicate the numbers array.
Expected result: [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
Here is my code so far:
const numbers = [1, 2, 3, 4, 5];
var result = numbers.map((number) => {
return number
});
console.log(result);
I can't figure out how you can take the numbers array and then duplicate the array?
I was starting to do if statements - "If number is equal to 1 then return 1" but that would print the numbers like this [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
https://jsfiddle.net/e6jf74n7/1/
Map will map all values one-to-one, that's why it's called "map"; it gives you one value, you return a value that should replace it.
To duplicate a list, concat the list to itself:
const numbers = [1, 2, 3, 4, 5];
var result = numbers.concat(numbers);
console.log(result);
fastest way is to use slice() then concat() to old array.
var arr = [ 1, 2, 3, 4, 5 ];
var clone = arr.slice(0);
var duplicate = arr.concat(clone);
Map won't work in this case just use concat
numbers.concat(numbers);
If you want to concat multiple times then
var concatArr = numbers;
for (var i=0; i < 9 ; i++ ) {
numbers = numbers.concat(concatArr);
}
console.log(numbers);
Concat docs
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/concat

JavaScript: Slice an array into three (roughly) equal arrays [duplicate]

This question already has answers here:
Splitting a JS array into N arrays
(23 answers)
Closed 8 years ago.
How do I slice an array like this:
var a = [1, 2, 3, 4, 5, 6 , 7, 8];
into thirds (ie. three arrays, like this):
[1, 2, 3]
[4, 5, 6]
[7, 8]
Here's what I've got so far:
var first = a.slice(0, Math.ceil(a.length / 3));
var seconds = ???
var third = ???
This works, though it can be cleaned up:
var m, n;
var first, second, third;
m = Math.ceil(a.length / 3);
n = Math.ceil(2 * a.length / 3);
first = a.slice(0, m);
second = a.slice(m, n);
third = a.slice(n, a.length);
First, get the length. Nice and simple: a.length
Next, divide by three and round up. This will be the size of your pieces.
Finally, use a.slice() with appropriate arguments to get the resulting arrays.
Write some code using the above algorithm, and let us know if you have any more specific problems :)

Categories

Resources