1
我已经阅读了很多关于自定义排序的内容,大多数似乎都使用大于或小于运算符来确定是否返回一个对象或另一个对象。基于2个属性的排序功能
但是,我的情况有点不同。这里是我的数组:
var myStuff = [{name:"one",direction:"receiving"},{name:"two",direction:"sending"},{name:"one",direction:"sending"},{name:"two",direction:"receiving"}];
我需要的是有数组结束混为一谈这样的(名字一和二(接收),然后一和二(发送)):
var myStuff = [{name:"one",direction:"receiving"},{name:"two",direction:"receiving"},{name:"one",direction:"sending"},{name:"two",direction:"sending"}];
我已经把这种做法的方式放在一起,但是有更优雅的东西吗?就像一个不错的自定义JavaScript排序函数。
var temp = myStuff.split(",");
$.each(temp, function(key,value){
if(value.name == "one" && value.direction == "receiving")
myStuff[0] = value;
if(value.name == "two" && value.direction == "receiving")
myStuff[1] = value;
if(value.name == "one" && value.direction == "sending")
myStuff[2] = value;
if(value.name == "two" && value.direction == "sending")
myStuff[3] = value;
});
这不是一种排序方式,这是[] .map()的用例... – dandavis