带下划线的重新排序集合
我是新来强调所以请原谅我的无知。
有没有一种快速简便的方法可以执行以下操作:
men:{gerald:{"name":"Gerald", "age":50},mike:{"name":"Mike", "age":50},charles:{"name":"Charles", "age":50}}
其中名称是 Mike - 设置在位置 1(索引 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于
_.suffle
会打乱所有元素,因此您需要拼接除预定义元素之外的所有元素,对它们进行打乱,并将打乱后的元素连接到包含预定义元素的现有数组。类似于:
它获取没有预定义元素的数组,对其进行洗牌,并将其连接到
shuffled
数组,该数组包含预定义变量作为其第一个元素。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:
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.您不需要 underscore.js。
Array.sort
会做得很好:这并不是真正的排序模型用法,但它可以完成工作。
此外,您的 JSON 无效。您需要在数组周围使用方括号,而不是花括号:编辑:
您无法“排序”该 JSON。您要求做的是订购一个关联数组。这没有任何意义。如果项目的顺序很重要,那么您需要一个数组,而不是一个对象。
You don't need underscore.js.
Array.sort
will do fine: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: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.