[JS]: 数组排序,根据key删除record

  1. Array sort()
    – To sort “xxx” first, the second is “yyy”
var arr = [];
arr.push({
  "xxx":"xx",
  "yyy":"yy",
  "zzz":"zz"
});
arr.sort(function(a,b){
  if(a.xxx === b.xxx){
    return a.yyy >= b.yyy ? 1 : -1;
  }
  return a.yyy >= b.yyy ? 1:-1;
});
  1. Object Array (JSON) delete by key
var persons = [];
var person={name:"yaoMing",sex:"m",age:"26"};
persons.push(person);  //数组最后加一条记录
persons.unshift(person);  //数组最前面加一条记录
persons.pop();//删除最后一项
persons.shift();//删除第一项

//删除
persons.splice(0,1);//开始位置,删除个数
//替换不删除
var self={id:"001",name:"tom",sex:"m",age:"24"};
var brother={name:"Mike",sex:"m",age:"29"};
persons.splice(1,0,self,brother);//开始位置,删除个数,插入对象
//替换并删除
var self={id:"002",name:"tom",sex:"m",age:"24"};
var brother={name:"Mike",sex:"m",age:"29"};
persons.splice(id:"003",0,1,self,brother);//开始位置,删除个数,插入对象

// 根据key删除
var delID = "002";
for(var index = persons.length - 1; index >= 0; index--){
  if(persons[index].id === delID || persons[index].id === ""){
    persons.splice(index,1);
  }
}

this.getDate = function(){
  var date = new Date();
  var year = date.getFullYear();
  var month = date.getMonth() +1;
  var day = date.getDate();
  var hour = date.getHours();
  var minutes = date.getMinutes();
  var seconds = date.getSeconds();
  var myDate = (day < 10 ? "0" + day : day) + "-" +
        (month < 10 ? "0" + month : month) + "-" + year + " " +
        (hour < 10 ? "0" + hour : hour) + ":" + 
        (minutes < 10 ? "0" + minutes : minutes) + ":" + 
        (seconds < 10 ? "0" + seconds : seconds);
  return myDate;        
}

this.showLoading = function(){
  try{
    var div = $("#loading-div");
    if(div.length === 0){
     $("body").append(this.dialog.loading_content);
     div = $("#loading-div");
    }
    div.css('visibility','visible');
    div.show();
    $("body").css("cursor","not-allowed");
  }catch(ex){

  }
}

this.dialog = {
  loading_content : "<div id='loading-div' style='background: rgb(255,255,255);left:0px;top:0px;width:100%;height:100%;right:10px;display:none;visibility:hidden;position:fixed;z-index:1000;opacity:0.5;'>"+
        "<img style='left:50%;top:50%;display:block;position:fixed;transform:translate(-50%,-50%);' alt='loading' src='" +
        GLOBAL.Path + "/image/loading.gif'/>" + "</div>",

  showLoading : function(){
    var div = $("#loading-div");
    if(div.length === 0){
     $("body").append(this.dialog.loading_content);
     div = $("#loading-div");
    }
    div.css('visibility','visible');
    div.show();
    $("body").css("cursor","not-allowed");
    },

  closeLoading : function(){
    $("#loading-div").hide();
    $("#loading-div").css('visibility','hidden');
    $("body").css("cursor","auto");
  }
}

GLOBAL.MyCommon = new Common();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值