0

I am fairly new to javascript and for some reason my program doesn't work. I have a dictionary using the key set to a string and the value euqal to an array full of booleans, I.E:

dict = {x: [true, false]}

but for some reason when I run the dictionary through an if statement, like

if (dict[x] == [true, false]) {do something }

it fails to trigger. I was wondering what I am doing wrong

Aymen
  • 1,476
  • 18
  • 28

3 Answers3

1

Comapre using Array.every

  • You can use Array.every to check if all items in an array verify a certain condition
  • With that you can check if all items in arrayOne are equal to the item at index i from arrayTwo

Here's an example

const isEqualArrays = (a, b) => a.length === b.length && a.every((x, i) => x === b[i]);
const isEqualDicts = (a, b) => isEqualArrays(a.x, b.x);

const first = { x: [true, false, true] };
const second = { x: [true, false, true] };
const unequal = { x: [true, true, true] };

console.log(isEqualDicts(first, second));
console.log(isEqualDicts(first, unequal));

Use JSON.stringify

Another way that works with your example, is to convert the objects ("dicts") into strings and then compare those strings. It looks like this

const compareDicts = (a, b) => JSON.stringify(a) === JSON.stringify(b);

const first = { x: [true, false, true] };
const second = { x: [true, false, true] };
const unequal = { x: [true, true, true] };

console.log(compareDicts(first, second));
console.log(compareDicts(first, unequal));
Community
  • 1
  • 1
molamk
  • 4,076
  • 1
  • 13
  • 22
0

You can not compare complex structures like arrays and objects because the equality checking is actually looking at specific memory locations. Every time you create a new object or array, that variable is pointing to a new, unique place in memory. You would have to iterate through the keys/indices that hold primitive values and check each one for equality with your other structure.

Extra credit: the square and curly braces are called Array literals and Object literals. They are essentially syntactic sugar for new Array or new Object.

Additionally, the reason you can check equality on strings, booleans, and numbers is because each value of each type (true, false, 7, 'hello world', 'blue', 42), actually points to a unique place in memory. For example: when you write

var str = 'yellow'

the variable str is now pointing to a place in memory that is associated with the six characters that comprise the word ‘yellow’. This is the actual reason that equality checking is possible.

var str2 = 'yel' + 'low'

Results in the same combination of characters, which results in a reference to the same memory address as what str’s valueOf function points to. Therefore:

str === str2 // true

I hope that helps. There’s much more down this rabbit hole, but this should get you started.

Ben Steward
  • 2,338
  • 1
  • 13
  • 23
0

Arrays and Objects are not primitive types but reference types in JavaScript.

This means that when comparing arrays and objects, you are actually comparing the references to these entities, and not their content. When you assign this array to another variable, you are simply making that variable reference the same content, but you are not copying this content:

const a = [1, 2, 3];
const b = [1, 2, 3];

console.log(a !== b);

const c = a;

console.log(a === c);

c[0] = 10;

console.log(a);

In your case, if you want to make sure your array contains true followed by false, you can use Array.every() or more simply just use array indexing to check the elements since the array is short:

const a = [true, false];

if (a.every((x, i) => x === [true, false][i])) {
  console.log('same content');
}

if (a[0] === true && a[1] === false) {
  console.log('same content');
}
jo_va
  • 13,504
  • 3
  • 23
  • 47