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,

7
  • 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. Commented Mar 28, 2014 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. Commented Mar 28, 2014 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? Commented Mar 28, 2014 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? Commented Mar 28, 2014 at 15:34
  • Using "object array" instead of "array" doesn't make your question clearer. Fix your code! Commented Mar 28, 2014 at 15:36

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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"}
    }
];

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.