【发布时间】:2017-03-18 01:30:50
【问题描述】:
也许有人可以帮忙。我自己无法弄清楚这一点:( 我有 json 对象的列表:
{"widget_foto": {"photos": [
{
"picThumb": "../../preview_stuff/img/widget-3-1.png",
"picOrig": "../../preview_stuff/img/widget-3-1.png",
"galeryUrl": "",
"shareUrl": "",
"priority": 0
},
{
"picThumb": "../../preview_stuff/img/widget-3-2.png",
"picOrig": "../../preview_stuff/img/widget-3-2.png",
"galeryUrl": "",
"shareUrl": "",
"priority": 2
},}}
我需要按优先级排序对象,我已经做了,但我不知道如何随机化优先级为 0 或对象优先级是否相等的对象。
这是我目前的代码。
$(function(){
var url="../json/foto.json";
$.getJSON(url, function(json){
var fotoWidget = json.widget_foto;
var widget3 = ".col-widget-3" + " ";
//Populate content
var byPriority = fotoWidget.photos.slice(0);
byPriority.sort(function(a,b){
return b.priority - a.priority;
});
$.each(byPriority, function(index, value){
$(widget3 + ".widget-3-" + (index + 1)).css({"background-image": "url(images/" + value.picOrig + ")"});
});
});
任何帮助都将不胜感激。
【问题讨论】:
-
添加一个 'sortorder' 属性,将其设置为
.priority + "." + random_number()然后对其进行排序。如果您以后不需要它,请在排序后将其删除。 stackoverflow.com/questions/1527803/…