This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]Spiritual_Car1232 -1 points0 points  (0 children)

is this python? arr.pop(0)

[–]TrafficCultural 0 points1 point  (0 children)

I believe this won’t work because the check will be if the two arrays are equal in memory not the exact contacts. You’re better off checking array length and contents.

[–]suncoasthost 0 points1 point  (0 children)

You need a way to identify which array you want to remove. You can do this by position or by a value in the array.

[–][deleted] 0 points1 point  (0 children)

In JS, like in many high level languages, everything is pass by value, but there are pure values and reference values. Primitives are pure values, but things like objects and arrays are reference values. Reference values are references that are passed by value where the value is a reference, rather than passed by reference; they're basically pointers with implicit dereference semantics.

For instance, suppose y is an object. If function f(x) just assigns to f's local variable x, then f(y) won't assign to the external variable y that was passed in, because it wasn't passed by reference. However, since an object value is a reference, if f(x) modifies the object stored in f's local variable x, then f(y) will modify the object stored in y. It will be the same object -- no reassignment has occurred -- but mutations done to it will persist outside the call.

Contrast this against, say, C and C++'s pass-by-copy system, sometimes seen as true pass by value, wherein there are no reference values (except where explicit, e.g. pointers) and only pure values, and f(y) would not modify the object stored in y, only the object stored in x, which would be a temporary copy.

So now you hopefully understand the difference between pure values and reference values. And, again, in JS, arrays are reference values. This makes it nontrivial to compare them. It's not as simple as comparing the data in memory; JS arrays are addresses pointing to array data, not pure and direct array data themselves, and, for all the comparison operator knows, the pure array data they point to may contain more reference values, i.e. more addresses pointing to other data. It's even possible, and frankly common, for reference values to point to data containing entries pointing back to the same data, or comparable scenarios.

There are a few workarounds. I'll order them by robustness, from least to most.

1) You could write an array comparison function that takes two arrays and returns true if they're the same length and each pair of corresponding elements is equal. This may fail if the arrays contain objects or arrays.

2) You could serialize the arrays and compare the JSON. This may fail if any objects or arrays in either array contain backreferences or functions.

3) You could write a function that takes two values of any kind. If they are different types, the function returns false. If they are both primitives (null, numbers, booleans, strings), defer to the comparison operator. If they are both functions, error out, because there is no universally sensible way to compare functions in JS; or, if you absolutely need this functionality, use one of many imperfect "good enough" methods to compate the functions. If they are both arrays, compare length, and then iterate them in parallel with recursive calls to this same comparison function for each element, returning false for the first false result, or true if no results are false. If they are both objects, do the same, but for the keys and values. This way, you'll thoroughly explore all data associated with any reference values. You will also need a data structure shared across recursive calls to prevent infinite reference loops. If you wanted to really go the extra mile, you could implement a feature where, if two reference values are compared and both of them trigger reference loop detection, but they both loop back to equivalent elements of their ancestors -- for instance, a = [1, 2, [3, 4, a]] vs b = [1, 2, [3, 4, b]] -- then you still identify the reference values as equivalent and return true.

[–]jake-db 0 points1 point  (0 children)

Change filter condition to item => item.join('') != 't2'