1

i'm working on a project in which i need to shuffle an object array on DOM ready while also preserving keys.

Example:

     var o = [
    {"key_1": {
        "bruce wayne": "batman"
    }},
    {"key_2": {
        "peter parker": "spiderman"
    }},

    {"key_3": {
        "bruce banner": "hulk"
    }}
];

After:

var o = [
    {"key_3": {
        "bruce banner": "hulk"
    }},
    {"key_1": {
        "bruce wayne": "batman"
    }},

    {"key_2": {
        "peter parker": "spiderman"

    }}
];

i've tried doing this a few different ways but haven't been successful. at this point i'm also not sure if this is the best approach for this.

Additional Info: the application has to iterate through the array, showing one key set at a time.

-appreciate any direction,

unfollow
  • 225
  • 1
  • 5
  • 12
  • `key_1`, `key_2` and `key_3` are just attributes of the array, here. If you meant an Object, its not possible, as the keys are internally hashed and stored. We cannot the order of their hashes. – thefourtheye Mar 28 '14 at 15:29
  • 2
    Your arrays have syntax errors. It seems you want objects instead of arrays or, since objects have no order, array of objects. – Oriol Mar 28 '14 at 15:32
  • If you have an array of objects, then you can shuffle the array like any other array. If you have an object, then you can't shuffle its properties because they are not ordered anyway. So, what exactly *do* you have? – Felix Kling Mar 28 '14 at 15:34
  • 1
    Despite the syntax error mentioned by Oriol. Do you really need to shuffle the object? Wouldn't it be sufficient to just shuffle an array that contains the keys and use this shuffled array to access the values in the object? – t.niese Mar 28 '14 at 15:34
  • Using "object array" instead of "array" doesn't make your question clearer. Fix your code! – Felix Kling Mar 28 '14 at 15:36
  • What's this? you mean `var o = { "key_1": { "bruce wayne": "batman", }, "key_2": { "peter parker": "spiderman", }, "key_3": { "bruce banner": "hulk", } }` ? – Kei Minagawa Mar 28 '14 at 15:38
  • thanks for the help and not being repetitive at all. i got it. – unfollow Mar 28 '14 at 15:51

3 Answers3

1

First fix o, it was not correct:

var o = [
    {"key_1": {
        "bruce wayne": "batman"
    }},
    {"key_2": {
        "peter parker": "spiderman"
    }},

    {"key_3": {
        "bruce banner": "hulk"
    }}
];

Now using the shuffle from this answer:

function shuffle(o){ 
    for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
    return o;
};

You call it like this:

o = shuffle(o);
Community
  • 1
  • 1
Raul Guiu
  • 2,374
  • 22
  • 37
0

You will need an actual array (instead of an syntax error):

var o = [
    {
        "key": 1,
        "bruce wayne": "batman"
    },
    {
        "key": 2,
        "peter parker": "spiderman"
    },
    {
        "key": 3,
        "bruce banner": "hulk"
    }
]

You can shuffle that easily and the keys are preserved.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0

Your arrays have syntax errors. It seems you want objects instead of arrays or, since objects have no order, array of objects:

var o = [
    {
        "key_1": {"bruce wayne": "batman"}
    },
    {
        "key_2": {"peter parker": "spiderman"}
    },
    {
        "key_3": {"bruce banner": "hulk"}
    }
];
shuffle(o);

Where shuffle is the function defined in this answer or in this one.

Now your array will be something like

[
    {
        "key_3": {"bruce banner": "hulk"}
    },
    {
        "key_2": {"peter parker": "spiderman"}
    },
    {
        "key_1": {"bruce wayne": "batman"}
    }
];
Community
  • 1
  • 1
Oriol
  • 274,082
  • 63
  • 437
  • 513