0
 var roots = [], children = {};

    // find the top level nodes and hash the children based on parents
    for (var i = 0, len = arry.length; i < len; ++i) {
        var item = arry[i],
            p = item.Parent,
            target = !p ? roots : (children[p] || (children[p] = []));
         // I am not able to understand this line what it does
        // target = !p ? roots : (children[p] || (children[p] = []));
        target.push({ value: item });
    }

what I understand that if p is null then childrens for that parent should be empty but why then need to use of || expression that is used in this code

(children[p] || (children[p] = [])

Sheel
  • 847
  • 2
  • 8
  • 20

6 Answers6

3

Step by step

  • target = !p ? x : y means if not p then target = x. Else target = y
  • (children[p] = []) means assign an empty array to children[p]
  • (children[p] || (children[p] = [])) means if children[p] is not null then return that. Else assign an empty array to children[p] then return it

Combining it

  • If p is null or undefined => target = roots
  • Else
    • If children[p] is NOT null then target = children[p]
    • Else children[p] = [] then target = children[p] which is an empty array

Equivalent

if (!p) {
  target = roots;
} else if (children[p]) {
  target = children[p];
} else {
  children[p] = [];
  target = children[p];
}
Community
  • 1
  • 1
molamk
  • 4,076
  • 1
  • 13
  • 22
2

|| is the Logical OR operator or the Conditional operator.It returns the first or second operand depending on whether the first is truthy or falsey. A truthy value means anything other than 0, undefined, null, "", or false.

This root:(children[p] || (children[p] = []) means that if children[p] is truthy then root is children[p] else root will be children[p]=[]. children[p] will be assigned an empty array rather than a falsey value

ellipsis
  • 12,049
  • 2
  • 17
  • 33
1

If children[p] is not defined (or this value is false, undefined, null, 0...), is setting with an new array.

The Logical OR operator (||) returns the value of its second operand, if the first one is falsy, otherwise the value of the first operand is returned.

e.i.

"foo" || "bar"; // returns "foo"
false || "bar"; // returns "bar"

Reference

R3tep
  • 12,512
  • 10
  • 48
  • 75
1

It is a conditional (ternary) operator ?: with an inverted check of p, which is the parent.

If p not exists, then take roots, otherwise take children of the parent or assign an empty array to it, as default value, and take it.

target = !p                                // condition
    ? roots                                // true part
    : (children[p] || (children[p] = [])); // else part
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

It is a more terse way to describe...

if (!children[p]) {
  children[p] = [];
}
alex
  • 479,566
  • 201
  • 878
  • 984
0

There is a ternary operator to check if p is not the item of parent then set the target to children and p elements array otherwise set to new empty array.

Ali
  • 57
  • 6