删除Array中的特定值

时间:2015-07-14 05:27:44

标签: javascript arrays

例如,我有一个对象数组:

var myarray = [{ id: 01, name: alison, active: false},
               { id: 04, name: drex, active: true},
               { id: 08, name: farel, active: true},
               { id: 09, name: dinkoy, active: true}];

我想删除active == falseid == 09

中的一些对象

任何代码建议?

6 个答案:

答案 0 :(得分:5)

使用filter。更多here

myarray = myarray.filter(function(e){
    return (e.active !== false) && (e.id !== 09);
});

我建议您使用underscore.jssugar.js执行以下常见任务:

underscore.js

myarray = _.reject(myarray, function(e){
    return (e.active !== false) && (e.id !== 09);
});

sugar.js

myarray.remove(function(e){
    return (e.active !== false) && (e.id !== 09);
});

Raw JS循环:

for (i = myarray.length-1; i >= 0; i--)  {
    if (myarray[i].active === false || myarray[i].id === 09) myarray.splice(i, 1);
}

答案 1 :(得分:2)

您可以使用var hEvent: THandle; Status: DWORD; Starting: Boolean; procedure HandleCommsEvent(pbxh: LongInt; Comms_status: DWORD; Parm1: DWORD); stdcall; begin //4 cases for event of DLOPEN LogAddLine(pbxh, 'HandleCommsEvent happend'); case Comms_status of DEVLINK_COMMS_OPERATIONAL, DEVLINK_COMMS_NORESPONSE, DEVLINK_COMMS_REJECTED: begin if Starting then begin Status := Comms_status; SetEvent(hEvent); end; end; // Case of Packets were generated by IP Office System unit ,but Not recieved by Devlink DEVLINK_COMMS_MISSEDPACKETS: begin LogAddLine(pbxh,'Connected MISSEDPACKETS ,Packets were generated by IP Office System unit ,but Not recieved by Devlink '); end; //Case of NO Response from From System Unit end; end; procedure TfrmMain.btnConnectClick(Sender: TObject); var vPass, vAddress: String; begin hEvent := CreateEvent(nil, TRUE, FALSE, nil); try with frmSetup.tblConnections do begin First; while not Eof do begin if FieldByName('IPEnabled').AsInteger = 1 then begin vPass := FieldByName('IPPassword').AsString; vAddress := FieldByName('IpAddress').AsString; Edit; FieldByName('pbxh').AsInteger := fNextHandle; FieldByName('connected').AsInteger := 0; Post; Status := DEVLINK_COMMS_NORESPONSE; Starting := True; ResetEvent(hEvent); DLOpen(fNextHandle, PChar(vAddress), PChar(vPass), nil, nil, HandleCommsEvent); WaitForSingleObject(hEvent, 10000); Starting := False; if Status = DEVLINK_COMMS_OPERATIONAL then begin DLRegisterType2CallDeltas(fNextHandle, HandleEvent); LogAddLine(fNextHandle, 'Connected Done'); Edit; FieldByName('connected').AsInteger := 1; Post; end else begin DLClose(fNextHandle); case Status of DEVLINK_COMMS_NORESPONSE: begin LogAddLine(fNextHandle, 'Connected NORESPONSE There Are Problem In network '); end; DEVLINK_COMMS_REJECTED: begin LogAddLine(fNextHandle, 'Connected REJECTED,Password was incorrect'); end; end; end; end; Inc(fNextHandle); end; Next; end; finally CloseHandle(hEvent); end; end; 功能:

filter

或使用简单的while循环,如下所示:

myarray = myarray.filter(function(e){
    return (e.active !== false) && (e.id !== 09);
});

维护索引在这种实现中可能会变得棘手,因此请选择您认为更方便的内容。

答案 2 :(得分:2)

这样的事情:

SELECT * FROM MEMBERID_1M WHERE ROWNUM <1000

正如你所看到的,使用了倒计时循环,因为我们应该仔细修改迭代数组:)

答案 3 :(得分:1)

如果您正在使用JQuery,那么您可以使用'each'函数进行循环,然后拼接active == false的对象。

答案 4 :(得分:0)

您可以执行foreach循环并检查此条件id == 09//Your array.. var myarray = [{ id: 01, name: 'alison', active: false}, { id: 04, name: 'drex', active: true}, { id: 08, name: 'farel', active: true}, { id: 09, name: 'dinkoy', active: true}]; //Perform a for-each loop on the array.. for(var key in myarray) { //if object->id = 9 OR object->active = false, remove the object from the array if( (myarray[key].id == 9) || (myarray[key].active == false) ) { delete myarray[key]; } }

这里是代码段

方法1 - JSFiddle Link

//Your array..
var myarray = [{ id: 01, name: 'alison', active: false},
               { id: 04, name: 'drex', active: true},
               { id: 08, name: 'farel', active: true},
               { id: 09, name: 'dinkoy', active: true}];

for(var key in myarray) {
    //if object->id = 9 OR object->active = false, remove the object from the array
    if( (myarray[key].id == 9) || (myarray[key].active == false) ) {
        myarray.splice(key, 1);
    }
}

注意:此方法不会影响数组的长度 - 即使删除项目后,长度仍为4

方法2 - JSFiddle Link

var jsonObj = {};
for (var i = 0 ; i < sampleArray.length; i++) {
    jsonObj["position" + (i+1)] = sampleArray[i];
}

这很好用,数组的长度也会受到影响 - 2

希望它有所帮助。

答案 5 :(得分:0)

您要求修改原始数组,使用过滤器创建新数组。其他使用的方法是 forEach reduce reduceRight

由于 forEach 会记住原始数组,因此在删除成员时您不需要做任何特别的事情:

Decimal budget;
Decimal cost;

if (!Decimal.TryParse(txtBudget.Text, out budget)) {
  btnSubmit.Visible = false; 
  //TODO: probably you have to show message that txtBudget has incorrect value
}
else if (!Decimal.TryParse(txtCost.Text, out cost)) {
  btnSubmit.Visible = false; 
  //TODO: probably you have to show message that txtCost has incorrect value
}
else {
  //TODO: you may find useful to check if cost < 0 or/and budget < 0 etc.
  btnSubmit.Visible = budget >= cost;  
}

reduceRight 从长度变为0,因此在修改数组时更合适,所以:

myarray.forEach(function(v, i, arr) {
  if (v.active == false || v.id == 09) arr.splice(i, 1);
});

但是 reduce 就像 forEach 一样,它会在成员被删除时跟踪索引,所以:

myarray.reduceRight(function(p, c, i, arr) {
   if (c.active == false || c.id == 09) arr.splice(i, 1);
},null);     

也有效。