Like "(M [key] || (M [key] = [])) push (elem);" work?

I know that

(M[key] || (M[key] = [])).push(elem); 

- a common way to solve a common problem

"Click this element at the end of the array, if the array exists, and if it does not exist, initialize the array and then click the element"

But how will the syntax break? This is how I see it:

Availability || means that everything that is to the left and to the right of this operator is interpreted as logical. Therefore, M[key] and (M[key] = []) are either logical or operators that translate to Boolean. In any case, I don’t see how you can push the value (M[key] || (M[key] = []) . What is happening here?

+5
source share
5 answers

This is a really very common JavaScript template.

You need to add the item to the list associated with the key, creating a new list if it does not exist.

To get the list associated with the key, the code:

 M[key] 

but this returns undefined if key does not exist.

Javascript || (logical or) operator has very useful semantics: if the left side is true, return it, otherwise evaluate and return the right side. Expression:

 M[key] || (M[key] = []) 

therefore, it returns the list associated with M[key] , if present, otherwise the part is calculated (M[key] = []) . This works because the array (even when empty) is true in JavaScript, and undefined is false.

Assignment operator = (which is a normal operator in JavaScript and can be used in the middle of expressions) performs the assignment, and then returns the value assigned to it.

Thus, all this simply returns the list associated with the key, or creates a new empty list if the key was unknown in M

Pressing an element into the <array>.push(x) , and the part to the left of the point can be any expression.

 (M[key] || (M[key] = [])).push(x); 

therefore, add x to the list returned by the left expression.

"The extended version will look like this:

 if (!M[key]) { M[key] = []; } M[key].push(x); 

Remember that using objects as dictionaries in JavaScript requires some caution due to inherited members. For example, with M = {} and key = "constructor" this code will fail.

+3
source

|| is a short circuit operator. This means that if the first half is β€œtrue,” she never does the second half. Therefore, if M[key] has a value (true value), the value used is what we do. Otherwise, the second part is executed.

The recorded long form, you can:

  if (M[key]) M[key].push(elem); else { M[key] = []; M[key].push(elem); } 

So you can understand why a shorter idiom developed.

0
source

Use your order of operations! Follow the brackets.

 (M[key] || (M[key] = [])).push(elem); 

Break it. (M[key] || (M[key] = []))

|| is an OR statement. Therefore, if M[key] exists, push new element to it. If not, create an array and add it.

He appreciates a true comparison. (true OR else)

0
source

Well, a lot is going on here.

First, how arrays work in javascript. You can access or assign an array element using ARRAY_NAME [KEY_VALUE].

Thus, M [Key] will return the object if it exists.

Based on this code, M is an array of arrays. Therefore, when M [Key] evaluates this, it will return an array or null.

When the expression a || b is evaluated in javascript, it is a short circuit and is also evaluated from left to right - that is, if a is true b is never evaluated - if a is false, then b is evaluated. (This is often found in many languages ​​like C).

Since M [Key] will return an object if it contains a part after || never evaluated, and an array on this key is used to input a new element.

If nothing exists in M ​​[Key], then the b-part is evaluated, and the b-part in this case first assigns an empty array ( [] ) to a place in M ​​[Key] (as we noted before M [Key] can also be used to assignment). Then this new empty array is used (since it is the result of evaluating the expression inside () ) to input a new element.

https://en.wikipedia.org/wiki/Short-circuit_evaluation

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

0
source

I understand it this way: undefined in javascript since boolean is false.

 (M[key] || (M[key] = [])).push(elem); 

performed as follows: if M [key] is not undefined - false, the result of the expression is M [key].

if M [key] undefined is false, the second part of the logical operator -OR is executed and M [key] gets an empty array as a value. The result of the expression is M [key], but now it has an empty array as a value.

Then we add a new value to the array M [key]: M [key] .push (elem);

I think this is useful javascript-operators-and-truthy-falsy

0
source

Source: https://habr.com/ru/post/1239592/


All Articles