RWS
RWS

Reputation: 560

JS or jQuery approach to arrange json into multidimensional array

I'm struggling with a frontend processing of a response from a server that spews out the event reviews in the season. There can be multiple reviewers for the same event and the simplified version of response looks like this:

[{"id":"3","reviewer_id":"4","event_id":"3","review":"a"},
{"id":"19","reviewer_id":"3","event_id":"4","review":"b"},
{"id":"20","reviewer_id":"1","event_id":"4","review":"b"}]

I want to make and array with events where array index would be defined by event_id, therefore I do something like this:

var events = new Array();//define the recipient array
$.each(response, function(index, row) {
    if (!(jQuery.isArray(events[Number(row.event_id)]))) {//if a variable by this index is not array then...
        var events [Number(row.event_id)] = new Array();// ...declare it as array, I get error thrown here: "Uncaught SyntaxError: Unexpected token ["
    } 
    events[Number(row.event_id)].push(row);//push current row into appropriate recipient array member
});

As noted in the code, I have an error thrown

Uncaught SyntaxError: Unexpected token [ in line 4.

Any help would be appreciated.

Upvotes: 1

Views: 53

Answers (3)

yqlim
yqlim

Reputation: 7098

Because you put a var in front of it.

var events [Number(row.event_id)] = ...

Remove the var and the error will go away

events [Number(row.event_id)] = ...

The reason is that keyword var is used to define a new variable, and when defining a new variable, you cannot create a property to that variable (such as Number(row.event_id)).

Besides, events has already been defined earlier. You should not define events again for your function to work correctly.

var response = [{"id":"3","reviewer_id":"4","event_id":"3","review":"a"},
{"id":"19","reviewer_id":"3","event_id":"4","review":"b"},
{"id":"20","reviewer_id":"1","event_id":"4","review":"b"}];

var events = new Array();//define the recipient array
$.each(response, function(index, row) {
    if (!(jQuery.isArray(events[Number(row.event_id)]))) {//if a variable by this index is not array then...
        events [Number(row.event_id)] = new Array();// ...declare it as array, I get error thrown here: "Uncaught SyntaxError: Unexpected token ["
    } 
    events[Number(row.event_id)].push(row);//push current row into appropriate recipient array member
});

console.log(events);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

By the way

JavaScript does not support multidimensional array. Only objects.

Upvotes: 2

phuzi
phuzi

Reputation: 13069

The problem appears to be this line

var events [Number(row.event_id)] = new Array();

Which appears to be trying to declare a variable called events (which is already declared above) and simultaneously assign an new array to a specific index.

Remove var and it should resolve the syntax error.

events [Number(row.event_id)] = new Array();

Upvotes: 1

31piy
31piy

Reputation: 23859

You have syntax error on this line:

var events [Number(row.event_id)] = new Array();

You need to change this to:

events[Number(row.event_id)] = new Array();

You don't need var declaration for adding a property in an object, or adding an element in an array. That's a syntax error. Following is a working example of your code:

var response = [{
    "id": "3",
    "reviewer_id": "4",
    "event_id": "3",
    "review": "a"
  },
  {
    "id": "19",
    "reviewer_id": "3",
    "event_id": "4",
    "review": "b"
  },
  {
    "id": "20",
    "reviewer_id": "1",
    "event_id": "4",
    "review": "b"
  }
];

var events = new Array();

$.each(response, function(index, row) {
  if (!(jQuery.isArray(events[Number(row.event_id)]))) {
    events[Number(row.event_id)] = new Array();
  }
  events[Number(row.event_id)].push(row);
});

console.log(events);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>https://stackoverflow.com/questions/49666110/js-or-jquery-approach-to-arrange-json-into-multidimensional-array/49666247#

Upvotes: 1

Related Questions