Discuss Scratch
- Pepperoni505
-
100+ posts
Is there JavaScript lists?
Hello. I was wondering that since you can Make variables in JavaScript Like:
I was wondering if you can make lists in JavaScript. Please tell me! Thanks.
var postExample = "Question";
- Pepperoni505
-
100+ posts
Is there JavaScript lists?
Thanks dude! And how do you get a specific item's info form the list?var array = ["item", "item2", "etc"]
- Pepperoni505
-
100+ posts
Is there JavaScript lists?
How do you name the list?var array = ["item", "item2", "etc"]
- WooHooBoy
-
1000+ posts
Is there JavaScript lists?
Thanks dude! And how do you get a specific item's info form the list?var array = ["item", "item2", "etc"]
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
All of your needs.
- ChocolatePi
-
1000+ posts
Is there JavaScript lists?
Here's a complete demo:
You can look up more online!
var list = [1, 2, 3] // Make a list and give it values 1 2 3 list[0] // item 1 of list (note that js arrays are 0 based - the first item is 0 instead of 1 list[0] = 'hi' // replace item 1 of list with 'hi' list.splice(0, 0) // delete item 1 of list list.length // length of list
You can look up more online!
- CatsUnited
-
1000+ posts
Is there JavaScript lists?
They're not called Lists though, they're called Arrays, and they're far more powerful then normal Scratch lists. You can add 2 dimensional lists, likeThanks dude! And how do you get a specific item's info form the list?var array = ["item", "item2", "etc"]
var array = ["item", ["item2", "item2.1"],"etc"]
- ev3coolexit987654
-
1000+ posts
Is there JavaScript lists?
//example of array(2d) var qwertyuiop = [["1,1","1,2"],["2,1","2,2"]]; var place11 = qwertyuiop[0][0];
Last edited by ev3coolexit987654 (Sept. 9, 2015 00:53:59)
- Jonathan50
-
1000+ posts
Is there JavaScript lists?
This is because, like Snap!, Arrays are first class.They're not called Lists though, they're called Arrays, and they're far more powerful then normal Scratch lists. You can add 2 dimensional lists, likeThanks dude! And how do you get a specific item's info form the list?var array = ["item", "item2", "etc"]var array = ["item", ["item2", "item2.1"],"etc"]
Also unlike lists, arrays have a fixed amount of items.
You can't add or delete items, though items can be undefined or you can set an item that is undefined to another item.
If you initialize the Array like
var myArray = [, , , , , ,]; // an array with 7 items
Also notice that unlike Scratch, arrays are stored as values in normal variables, since they are first class.
Last edited by Jonathan50 (Sept. 7, 2015 01:48:18)
- liam48D
-
1000+ posts
Is there JavaScript lists?
Also notice that unlike Scratch, arrays are stored as values in normal variables, since they are first class.
So with Scratch you'd do this to “make” your list:
delete (all v) of [your list v]Direct translation to JavaScript:
add [1] to [your list v]
add [2] to [your list v]
add [banana] to [your list v]
var yourList; yourList = []; yourList.push(1); yourList.push(2); yourList.push("banana");
With JavaScript you'd do this to make your Array:
var yourList = [1, 2, "banana"];
set [your list v] to (list [1] [2] [banana] :: list)
- Pepperoni505
-
100+ posts
Is there JavaScript lists?
What's JavaScript code for this block?Also notice that unlike Scratch, arrays are stored as values in normal variables, since they are first class.
So with Scratch you'd do this to “make” your list:delete (all v) of [your list v]Direct translation to JavaScript:
add [1] to [your list v]
add [2] to [your list v]
add [banana] to [your list v]var yourList; yourList = []; yourList.push(1); yourList.push(2); yourList.push("banana");
With JavaScript you'd do this to make your Array:Direct translation to Scratch(blocks, or rather, Snap!):var yourList = [1, 2, "banana"];set [your list v] to (list [1] [2] [banana] :: list)
(item (3 v) of [myList v])
- powerpoint56
-
1000+ posts
Is there JavaScript lists?
Bracket notation. What's JavaScript code for this block?(item (3 v) of [myList v])
var myArray = ["apple", "pear", "banana"]; myArray[0] === "apple" && myArray[1] === "pear"
- Pepperoni505
-
100+ posts
Is there JavaScript lists?
But how does it know which item to get info out of?Bracket notation. What's JavaScript code for this block?(item (3 v) of [myList v])var myArray = ["apple", "pear", "banana"]; myArray[0] === "apple" && myArray[1] === "pear"
- liam48D
-
1000+ posts
Is there JavaScript lists?
It depends on the order you put then in your array. If you put “apple” into your array first, you can access it by using myArray, because it's the zeroth item in the array.But how does it know which item to get info out of?Bracket notation. What's JavaScript code for this block?(item (3 v) of [myList v])var myArray = ["apple", "pear", "banana"]; myArray[[]0] === "apple" && myArray[1] === "pear"
Like ChocolatePi said, arrays are zero-based - that means that the first item is arr[0], the second item is arr[1], the third item is arr[2], etc.
Super edit bump: Forgot to change the very first myArray[0]. rip
Last edited by liam48D (Oct. 1, 2015 08:33:02)
- nathanprocks
-
1000+ posts
Is there JavaScript lists?
FTFY.It depends on the order you put then in your array. If you put “apple” into your array first, you can access it by using myArray[0], because it's the zeroth item in the array.But how does it know which item to get info out of?Bracket notation. What's JavaScript code for this block?(item (3 v) of [myList v])var myArray = ["apple", "pear", "banana"]; myArray[0] === "apple" && myArray[1] === "pear"
Like ChocolatePi said, arrays are zero-based - that means that the first item is arr[0], the second item is arr[1], the third item is arr[2], etc.
- liam48D
-
1000+ posts
Is there JavaScript lists?
Wat. oh…. I see xD I'll fix thatFTFY.It depends on the order you put then in your array. If you put “apple” into your array first, you can access it by using myArray[0], because it's the zeroth item in the array.But how does it know which item to get info out of?Bracket notation. What's JavaScript code for this block?(item (3 v) of [myList v])var myArray = ["apple", "pear", "banana"]; myArray[0] === "apple" && myArray[1] === "pear"
Like ChocolatePi said, arrays are zero-based - that means that the first item is arr[0], the second item is arr[1], the third item is arr[2], etc.
EDIT: It was the [1], [2], [3] not showing up he fixed
Last edited by liam48D (Sept. 7, 2015 14:14:33)
- Smitop
-
100+ posts
Is there JavaScript lists?
But this code works… Also unlike lists, arrays have a fixed amount of items.
var array = [1,2,3];
alert("array is" + array);
array[100] = 100;
alert(array);
- Pepperoni505
-
100+ posts
Is there JavaScript lists?
Thats True.But this code works… Also unlike lists, arrays have a fixed amount of items.var array = [1,2,3];
alert("array is" + array);
array[100] = 100;
alert(array);