0

I'm new to underscore so pardon my ignorance.

Is there a quick and easy way to do the following:

men:{gerald:{"name":"Gerald", "age":50},mike:{"name":"Mike", "age":50},charles:{"name":"Charles", "age":50}}

Where name is Mike - set at position 1 (index 0)

I just want to shuffle the items so the first item is set by the name I choose.

Any help more than welcome.

Chin
  • 12,582
  • 38
  • 102
  • 152
  • Do you need a random shuffle? If no, just find the one you want and swap it with the first one? – Ray Toal Dec 01 '11 at 08:56
  • Thanks - how do I do that? I'm new to named object collections - always worked in arrays = I'd do collection[0] = object – Chin Dec 01 '11 at 08:56
  • That's not valid json. You should _never_ see `{{` in JSON. – Eric Dec 01 '11 at 09:20
  • Sorry, I've corrected it now. I have no problem ordering an array. but am struggling with the data structure above. – Chin Dec 01 '11 at 09:32
  • @Chin: It doesn't make any sense to order an associative array... – Eric Dec 01 '11 at 09:35
  • Yes I know, I've just got to do something quick and dirty. Not my design.. http://stackoverflow.com/questions/5199901/how-to-sort-an-associative-array-by-its-values-in-javascript – Chin Dec 01 '11 at 09:40
  • Your question makes no sense. You want to make the first item "mike". An associative array has no first element. You need to rephrase your question. – Eric Dec 01 '11 at 22:20

2 Answers2

1

Since _.suffle shuffles all elements, you'd need to splice out all elements but the predefined one, shuffle them, and concatenate the shuffled elements to an existing array which contains the predefined element.

Something like:

var orig = [1, 2, 3, 4, 5],
    shuffled = [];

shuffled[0] = orig[2]; // 3 is the predefined element

shuffled = shuffled.concat( _.shuffle( orig.slice(0,2).concat( orig.slice(3) ) ) );

which gets the array without the predefined element, shuffles it, and concatenates it to the shuffled array, which contains the predefined variable as its first element.

pimvdb
  • 151,816
  • 78
  • 307
  • 352
0

You don't need underscore.js. Array.sort will do fine:

myJson.men.sort(function(a, b) { return b.name == "Mike" })

This isn't really model usage of sort, but it does the job.


Also, your JSON is invalid. You need square brackets around the array, not curlies:

{
    "men": [{
        "name": "Gerald", "age": 50
    },{
        "name": "Mike", "age": 50
    },{
        "name": "Charles", "age": 50
    }]
}


Edit:

You can't "order" that JSON. What you're asking to do is order an associative array. This doesn't make any sense. If order of items is important, you want an array, not an object.

Eric
  • 95,302
  • 53
  • 242
  • 374
  • This is the point - its not an array but a object of objects. – Chin Dec 01 '11 at 09:29
  • Yes, I agree. However, that's how it's coming and now I need to change the order. I can do all the converting etc, I know - but I was wondering whether there was a easy way to reset the order of the associative array. – Chin Dec 01 '11 at 09:38
  • @Chin: It doesn't have any order - there is no concept of order in an associative array! – Eric Dec 01 '11 at 09:38