-1

Im wondering how to create the equivalent of a 2d array in JQuery

From research i can see that JQuery does not support 2d arrays but i wonder if its possible to create one from arrays within arrays.

What i would like to do is have an array that holds weeks and inside that array hold 7 days.

From research gathered i seem to want something like this:

{
 "Week":
 [
     {
      "Day":"Monday",
      "Day":"Tuesday",
      "Day":"Wednesday",
      "Day":"Thursday",
      "Day":"Friday",
      "Day":"Saturday",
      "Day":"Sunday"
     }
 ]
}

so

week[1,2,3,4,5,6,7,8,9,10]
day[Monday,Tuesday, Wednesday, Thursday, Friday,Saturday, Sunday]

So effectively the week would be index of the row and the day the index of the column.

Is this actually possible? I'm used to java and jQuery is strange to me.

8
  • 3
    You can't do "Day":"Tuesday","Day":"Wednesday" Commented Feb 5, 2014 at 7:56
  • 2
    Same key different values. Not possible Commented Feb 5, 2014 at 7:57
  • 2
    You can do {"Week":["Monday","Tuesday"]}. Commented Feb 5, 2014 at 7:57
  • 1
    You could possible use {day1: 'Monday',} and so on? Commented Feb 5, 2014 at 7:58
  • 2
    Please note that this really has nothing to do with jQuery, and everything with JavaScript. I recommend to read a basic tutorial. Commented Feb 5, 2014 at 8:04

3 Answers 3

1

Arrays within an array using javascript : var a = [[1],[2]].

Accessing the data : a[1][0] -> 2.

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

3 Comments

So this would return the value of the second array?
@KyleT Glad to help :) You should follow Felix Kling's advice though.
@KyleT I think these tutorials are still free : codecademy.com/tracks/javascript. Have fun :)
1

You can generate a multiple array similar as in other programming languages.

the shortest way to generate such an array is:

[[],[]]

as mentioned in the post before.

but for your problem i would recommend that you create a day's-array first like this:

var days = ['Monday', 'Tuesday', '...'];

and after that you refer to this days array like this:

var weeks = [days, days, days, days, ...];

this will save resources because there is only a pointer to the same days array all the time. and not a new days array for each week.

have a nice day.

1 Comment

Nice job, a smart way of doing it :)
1

i'm not sure to have understand what you need to do. In javascript you can create array of array. so if you try to store inside an array the day inside a week you can do this:

var weeks = [
    ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"],
    ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"],
    //...
    ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"],
    ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"],
    ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
]

console.log(weeks[0][0]); //"Monday"
console.log(weeks[0]); //array["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]

or you can use js OBJECT like 2d associative array:

var weeks = {
    "week1" : ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"],
    "week2" : ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"],
    "week3" : ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"],
   //...
    "week8" : ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"],
    "week9" : ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"],
    "week10" : ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"],
}

console.log(weeks.week1[0]); //monday
console.log(weeks.week1); //["Monday",...,"Sunday"]

:) hope it can help.

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.