0

This is working great in PHP, how can I accomplish the same in JS/jQuery?

$statsArr['Status'][$s_id]['count'] = ($statsArr['Status'][$s_id]['count'] ?? 0) + 1;
SeaBass
  • 1,584
  • 4
  • 20
  • 46
  • Any other html/js code? – Luka Čelebić Sep 13 '17 at 01:13
  • No, I just want to figure out how I can do it in JavaScript with same variable/array names? Does JS/jQuery have something similar to the PHP null coalesce operator? – SeaBass Sep 13 '17 at 01:14
  • Question already exists https://stackoverflow.com/questions/6613952/is-there-a-null-coalescing-elvis-operator-or-safe-navigation-operator-in-javas and https://stackoverflow.com/questions/476436/is-there-a-null-coalescing-operator-in-javascript – Johnny Dew Sep 13 '17 at 01:17
  • @JohnnyDew Thanks! Like this? `var statsArr['Status'][s_id]['count'] = statsArr['Status'][s_id]['count'] + 1 || 0` It gave me `missing ; before statement` – SeaBass Sep 13 '17 at 01:29
  • I removed var and it didn't give me the error but now I get this `TypeError: statsArr.Status is undefined` – SeaBass Sep 13 '17 at 01:41
  • 1
    Possible duplicate of [Is there a null-coalescing (Elvis) operator or safe navigation operator in javascript?](https://stackoverflow.com/questions/6613952/is-there-a-null-coalescing-elvis-operator-or-safe-navigation-operator-in-javas) – stealththeninja Sep 13 '17 at 02:47

1 Answers1

1

You have to define each array as an array. Then you won't have the missing ; before statement error.

var s_id = 0;
var statsArr = Array();
statsArr['Status'] = Array();
statsArr['Status'][s_id] = Array();
statsArr['Status'][s_id]['count'] = statsArr['Status'][s_id]['count'] + 1 || 0;
// the result will be 0 since statsArr['Status'][s_id]['count'] was not defined previously
Johnny Dew
  • 971
  • 2
  • 13
  • 29