Why do arrays coerce in a specific way? [duplicate] - javascript

This question already exists:
how does the js coercion work in some cases [duplicate]
Closed 1 year ago.
I'm struggling to understand why this examples below end up with this kind of results.
String([,,,]); // ',,' why?
Number([8,8]); // NaN why?
please explain with details if possible

Note that String calls the toString method on the object, which, for arrays, is equivalent to calling Array#join.
String([,,,]);
[,,,] is an array with 3 empty elements. [,,,].join() will then put two commas as separators between the empty elements, resulting in ",,".
Number([8,8]);
[8,8].join() returns "8,8", which cannot be parsed as a Number, so it produces NaN.

Related

How to use filter to compare to arrays to find missing object? [duplicate]

This question already has answers here:
Array.includes() to find object in array [duplicate]
(8 answers)
Object comparison in JavaScript [duplicate]
(10 answers)
How to determine equality for two JavaScript objects?
(82 answers)
Closed 9 months ago.
I'm trying to use arr.filter() to find the missing element between two arrays, this has been answered plenty of times and i've seen threads like this one Finding missing element in two array for javascript that explain it actually very well. For some reason thought, i cant seem to get this to work.
This is my code
var x = [{Number:1},{Number:2},{Number:3}]
var y = [{Number:1},{Number:2}]
function getDifference(x,y){
x.filter(function(object,index,arr){
console.log(object,index,arr)
if(!y.includes(object)){
// print object
console.log(object) // <-- Prints all 3, should just print the missing one.
}
})
}
getDifference(x,y)
Basically, it just needs to print out the missing object. Which in this case, is {Number:3}
Instead, it prints every object.
I've tried with the code in the thread that i linked above, and still was having trouble. Not sure what i'm not understanding with it.

Searching for a specific array in an array of arrays [duplicate]

This question already has answers here:
Javascript indexOf for an array of arrays not finding array
(6 answers)
Closed 1 year ago.
const a = [[1,1], [2,1], [3,1]];
console.log(a);
console.log(a.includes([1,1]));
> false
Can someone explain to me why the above outputs false? For what its worth, this occurs in any of the search functions (.find, .indexof, etc), as well as if I try [1,1] in a. I'm clearly missing something about how multidimensional array searching works in javascript.
This is basically how equal to operator works in JavaScript.
If we try to compare two arrays
[1]=== [1] // false
equality operator can compare primitive values(number,string Boolean). But in case of arrays and objects it compares for reference. So here we are comparing two different references so it is coming as false.
For same reason you are getting false. You have to write customer logic to compare arrays.

Empty array is shown as object [duplicate]

This question already has answers here:
How can I check if an object is an array? [duplicate]
(51 answers)
Closed 1 year ago.
I wrote a utility function for my nextjs project. In there, I got something that I unexpected. I initialised an empty array which be filled with the data later. When I type check that empty array, it shows an object. I don't want an object. I want an array. Why? Could someone told me why does it happen.
strong text
Yes typeof array is an object in javascript
if you want to check one variable is an object or array then you can use
Array.isArray()
console.log(Array.isArray(arr)) // true
console.log(Array.isArray(obj)) // false

need reference for documentation on ternary operator using push [duplicate]

This question already has answers here:
When is the comma operator useful?
(15 answers)
Closed 1 year ago.
I have the following:
let currentLocalStorage = [];
currentLocalStorage = (initialLoad) ? JSON.parse(localStorage.getItem('tasks')): (currentLocalStorage.push(taskInput.value),currentLocalStorage)
which works but I would like to get a reference or documentation for the following:
: (currentLocalStorage.push(taskInput.value),currentLocalStorage)
so basically we are pushing on to the array and then defaulting to the array. I was surprised that we can do this and was wondering where one we look for the documentation
This is using the comma operator. Because .push() returns the new length of the array, you want to make sure you don't assign that to currentLocalStorage, so you use the comma operator to have that expression evaluate to currentLocalStorage.
So it effectively becomes currentLocalStorage = currentLocalStorage in that case, except now the array has one more item thanks to .push().

Why could I compare two strings with "==" but not two arrays of chars? [duplicate]

This question already has answers here:
How to check if two arrays are equal with JavaScript? [duplicate]
(16 answers)
Closed 9 years ago.
For example:
var one = ['H', 'i'];
var two = ['H', 'i'];
(one == two) returns false
but
(one.join('') == two.join('')) returns true
Why is that?
There is a difference on how equality is defined for strings and arrays - strings are considered equal if their contents are identical, but arrays are considered equal only if it's the same array, and different otherwise even if their contents match.
There are a bunch of reasons why it could be the way it is, for example two reasons:
1) you often don't want array comparison to go through the whole array, because it could be huge and would take a huge time. So the default way shouldn't be dangerous.
2) you can alter array contents while still being 'the same' array; while javascript strings are immutable so any changed string is a new, different object.
When comparing objects, JS wants to see if they are the actual same object, not just an object with the same contents.
I find underscore's isEqual method useful here, but if you want to figure out how it is done library free, just glance at underscores core, which is very easy to read
http://underscorejs.org/#isEqual

Categories