2

I am trying to add values into an array but for some reason it is not working. I am new to JavaScript.

Here is my code:

eventsArray = new Array();

$.each(xmlJsonObj.feed.entry, function(index, value){
    eventsArray[index] = new Array('title' = value.title, 'date' = value.date[1]);
});

So basically I am pulling out some values from the json object and want to save them as key-value pairs in an array (multidimensional as each event has several values).

This array will later be sorted by date.

I am currently getting the following error:

ReferenceError: Left side of assignment is not a reference.

I am new to JavaScript and don't really understand whats wrong. Tried to look at some examples but still can't see a good example of creating two dimensional arrays with JavaScript (or objects, as everything in JS is an object) in a loop like this.

I would be very thankfull for any help or tips.

4 Answers 4

5

The cause of the error message is this:

'title' = value.title

That would mean that you are trying to assign a value to a literal string. The rest of the code (except from the other one just like it) is actually valid syntax, even if that is not what you are trying to do, so that's why you get the error message on that part of the code.

To have a collection of key-value pairs you would use an object instead of an array, and you can create it like this:

eventsArray[index] = { title: value.title, date: value.date[1] };
Sign up to request clarification or add additional context in comments.

3 Comments

Accepted aaaanswer!!! ;) Was looking for the first assignment to spot the error, did miss the second new array(), thanks. ;)
So do I still need to declare the array? Or is it ok not to declare it manually in JS?
@alwaysStuck: You still need to create the array object before you can put anything in it. Also, although you don't need to declare variables in Javascript, it's a good practice to always do that.
2

May be simplest one,

var eventsArray = new Array();

$.each(xmlJsonObj.feed.entry, function (index, value) {
    eventsArray[index] =  { 'title': value.title, 'date': value.date[1] };
});

1 Comment

@Guffa I am assuming eventsArray is an array declared above .each loop
1

It works if you change your code to:

var eventsArray = new Array();

$.each(xmlJsonObj.feed.entry, function(index, value){
    eventsArray.push({ title : value.title, date: value.date[1] });
});

Comments

1

You should use objects for this:

eventsArray[index] = {};
eventsArray[index].title = value.title;
eventsArray[index].date = value.date[1];

The problem you have is that you try to assign value.title value to String. Arrays in JS didn't work in that way. Also Arrays didn't support String keys, this is why you may need Object.

If your date property is TIMESTAMP for example you can sort it like:

eventsArray.sort( function( a, b ) {
    return a.date - b.date;
}); 

1 Comment

but can I sort it? Its very important for me to be able to later sort it according to the date value and display them in order according to date. Is this possible with objects? I know its possible with arrays.

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.