1

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.

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, do you by chance know why am I getting an empty first element of the array? so that events looks like [empty × 3, Array(1), Array(2), ... ] with console.log()
because of events[Number(row.event_id)].push(row). The smallest event_id is 3, but as an array, events should have values in index 0, 1, 2 but these are missing. So there is empty x 3
just use var events = {} will solve your problem but it is not an array anymore. Still, it can be parsed into multidimensional array in PHP server. Besides, it seems to be a more appropriate approach than your current approach.
1

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#

Comments

1

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.