JSON shuffle. How?

2,150 views
Skip to first unread message

Rolf -nl

unread,
Apr 10, 2009, 5:39:23 AM4/10/09
to MooTools Users
Hello group,

I think I'm missing something (maybe because today is a friday).

In a text file I have a shirt JSON list of let's say products:

{
"products": [
{
"id": 0,
"name": "Product name 1",
"manufacturer": "Just a manufacturer",
"text": {
"descriptionShort": "Bla about product 1.",
"descriptionLong": "Just a little more bla bla for product 1."
},
"images": [
{"src": "product1-image1.jpg"},
{"src": "product1-image2.jpg"},
{"src": "product1-image3.jpg"}
]
},
{
"id": 1,
"name": "Product name 2",
"manufacturer": "Another manufacturer",
"text": {
"descriptionShort": "Bla about product 2.",
"descriptionLong": "Just a little more bla bla for product 2."
},
"images": [
{"src": "product2-image1.jpg"},
{"src": "product2-image2.jpg"}
]
}
]
}

Now I want to shuffle/randomize this after loading so the page shows a
different thumbnail grid everytime.
Can't seem to find out how to do that. Maybe the JSON is incorrect?

Byebye,
Rolf

ryan

unread,
Apr 10, 2009, 5:58:16 AM4/10/09
to MooTools Users
json.products.getRandom();

?
Ryan

Rolf

unread,
Apr 12, 2009, 12:00:42 PM4/12/09
to MooTools Users
I don't want a random item, but I want to shuffle the order.
I loop through the whole thing and show it as thumbnails, but I don't
want it to be in the same order each time.

ryan

unread,
Apr 12, 2009, 12:10:23 PM4/12/09
to MooTools Users
Ah ok - I misunderstood - what about using something like:
http://jsfromhell.com/array/shuffle

Along with Array.map:
http://www.mootools.net/docs/Native/Array#Array:map

Ryan

Eneko Alonso

unread,
Apr 12, 2009, 3:31:38 PM4/12/09
to mootool...@googlegroups.com
Then what you need is a second object "products2".
With a loop, pick a random product from the original array and move it
to the second, until the original it is empty.

Then show products2 on screen.

Iain

unread,
Apr 14, 2009, 11:23:37 AM4/14/09
to MooTools Users
I'm assuming you're getting this data from a database, why not shuffle
it in the query?
mysql example: "SELECT * FROM products ORDER BY RAND()"

Rolf

unread,
Apr 15, 2009, 4:07:58 AM4/15/09
to MooTools Users
Hi lain,

Nah, this is not coming from an SQL database.. it's only a small list
of items, new items are added once/twice a year orso.
So it's a plain text json file. I will try some of the other
suggestions.

I was expecting a simple/quick already existing function as in PHP
etc. (instead of creating a second array myself etc.)

Ken Marfilla

unread,
Apr 15, 2009, 5:18:26 AM4/15/09
to MooTools Users
Array.implement({
shuffle : function()
{
var v = $unlink(this);
for(var j, x, i = v.length; i; j = parseInt(Math.random() * i), x
= v[--i], v[i] = v[j], v[j] = x);
return v;
}
});

json.products = json.products.shuffle();

Rolf

unread,
Apr 15, 2009, 9:30:12 AM4/15/09
to MooTools Users
Yup, my idea.

Cheers all

CroNiX

unread,
Apr 15, 2009, 6:43:17 PM4/15/09
to MooTools Users
Just use json_decode(), then shuffle(), then json_encode() in php.

Rolf

unread,
Apr 16, 2009, 6:25:05 AM4/16/09
to MooTools Users
there's no php/mysql involved, else it would be simple (either random
select with sql or shuffle with php) ;)

But anyway- the solution is already posted: just adding a shuffle
function to the array functions of mootools. I always thought there
was a standard shuffle method already in JS.

This topic can be closed :)

Rolf

Reply all
Reply to author
Forward
0 new messages