0

I have below code block:

const arr = [
  {name: 'Peter', folderLevel: 0}, 
  {name: 'Sara', folderLevel: 1},
  {name: 'William'}
 ];

let newArr = arr.map(el => {
   if (el.folderLevel) {
     el.folderLevel = el.folderLevel + 1;
     return el;
   } else {
     return el;
   }
 });

When I console log the newArr, the result is

[
  {name: 'Peter', folderLevel: 0}, 
  {name: 'Sara', folderLevel: 2},
  {name: 'William'}
 ];

I am wondering why inside the object name with 'Peter', the folderLevel wasn't added 1?

JavaScript Rookie
  • 153
  • 1
  • 3
  • 10
  • 2
    your check on el.folderLevel is returning '0', which javascript interprets as false. – Kevin C. Ferron Jun 16 '21 at 14:18
  • 3
    `0` is classed as falsy, you need to do `if ('folderLevel' in el)` instead. – Keith Jun 16 '21 at 14:18
  • As the other comments say, 0 is interpreted as false in JS. You can learn more in here, https://softwareengineering.stackexchange.com/questions/198284/why-is-0-false – Kevin Oswaldo Jun 16 '21 at 14:19
  • 2
    [Duplicate](//google.com/search?q=site%3Astackoverflow.com+js+if%280%29) of [JS: check existence of a var that equals 0](/q/9183788/4642212). Use `el.hasOwnProperty("folderLevel")` instead. – Sebastian Simon Jun 16 '21 at 14:20
  • 1
    `return el; } else ` can be removed – mplungjan Jun 16 '21 at 14:21
  • 1
    `console.log(0 == false)` returns true. – Jeremy Thille Jun 16 '21 at 14:23
  • @JeremyThille That’s not really relevant. `Boolean(0) === false` is. The abstract equality operator and the `if` condition expression do very different things. – Sebastian Simon Jun 16 '21 at 14:25
  • Here is the code you need to not modify the folderLevel in the original array: `let newArr = JSON.parse(JSON.stringify(arr)) newArr.forEach(el => { if ('folderLevel' in el) el.folderLevel++ })` – mplungjan Jun 16 '21 at 14:32

1 Answers1

0

Problem comes from your statement:

if (el.folderLevel)

In fact, in JavaScript, 0 is like null. So if your value is egual to 0, your condition 'if value' will return false.

What you need to do, in your case, is to put this condition:

if (el.folderLevel !== undefined)
Cpavrai
  • 46
  • 2
  • 2
    `0 is like null` is a misleading statement. It is falsy, which means it evaluates to false if not checked correctly. null is falsy too, but that still does not mean they are the same. – MauriceNino Jun 16 '21 at 14:21
  • Typescript allows you to do `const a:string = null;` but not `const a:string = 0;` 0 and "null object" are not the same thing, although both falsy. – Jeremy Thille Jun 16 '21 at 14:24
  • @JavaScript Rookie, You should use ` window.alert();` to locate the values in the course of debugging. For example, use of Statement ` window.alert(el.folderLevel);` as a first statement in the `if` would have helped you solve this problem all by yourself. – Raky Jun 16 '21 at 14:27
  • 2
    @Raky `alert` is inappropriate for debugging. Use a proper debugger instead or at least log values with `console.log`. – Sebastian Simon Jun 16 '21 at 14:28
  • @Raky as Sebastian Simon said, `alert` is not a good debugging tool. It's bad habit to use it because it changes the flow of your application with as it pauses the execution. You could get misleading results due to timing with it. It's also not really good for logging complex values like objects. There is basically no reason to use it for proper debugging. There is very little reason to use it at all. A [debugger](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) is the correct tool for debugging. – VLAZ Jun 16 '21 at 14:32