2013-10-09 37 views
0

在數組中刪除索引我有一個數組:檢查where條件使用由JavaScript

var xx = [ 
{text:'-1', done:true}, 
{text:'1', done:false}, 
{text:'2', done:true}, 
{text:'3', done:false}, 
{text:'4', done:false}]; 

我想更新索引,就像如果文本爲3,然後我想刪除整個索引({text:'3', done:false},)push(添加)新行。

我該怎麼做?

+0

這是什麼複雜的?刪除項目後是否要更新文本索引?描述在刪除一個項目後你的數組應該是什麼樣子。 – user10

+0

循環訪問數組以找到要刪除的條目的索引,使用拼接刪除它,然後推送一個新條目。有什麼我失蹤? PS:如果你想保留xx [3]爲空,而不是拼接它,你可以做xx [3] = null(或者undefined)。不推薦! – Tibos

+0

是否要在數組末尾添加新行,或者是否要移除已刪除的項目? – leaf

回答

1

如何定製跨瀏覽器解決方案?

function findIndexBy(a, fn) { 
    var i = 0, l = a.length; 
    for (; i < l; i++) { 
     if (fn(a[i], i)) { 
      return i; 
     } 
    } 
    return -1; 
} 

用法:

// make sure that "xx" exists as an array 
if (xx && Object.prototype.toString.call(xx) === '[object Array]') { 
    // get the row's index 
    var idx = findIndexBy(xx, function (row) { 
     return row.text === '3'; 
    }); 
    // check whether the row has been found 
    if (idx !== -1) { 
     // you can now update the "text" property 
     xx[idx].text = 'some text'; 
     // or remove the entire row 
     xx.splice(idx, 1); 
     // then add a new one 
     xx.push({ text: 'some text', done: false }); 
    } 
} 
+0

完成,謝謝傢伙的! –

1
var find = function(arr, text) { 
    for(var i = 0; i < arr.length; i++) { 
     if (arr[i].text == text) { 
     return i; 
     } 
    } 
} 

var foundIndex = find(xx, '3'); 
while(foundIndex != undefined) { 
    arr.splice(foundIndex, 1) 
    foundIndex = find(xx, '3'); 
} 

請記住,使用「刪除」關鍵字不從數組中刪除元素 - 從這個原因,我用拼接方法

+0

arr.length是未定義的! –

+0

對不起,它不工作,它是刪除其他所有索引,而不是確切的索引 –

1
var xx = [ 
{text:'-1', done:true}, 
{text:'1', done:false}, 
{text:'2', done:true}, 
{text:'3', done:false}, 
{text:'4', done:false}]; 
function update(id,new_el_done){ 
    var i=0; 
    var deleted_something=false;  
    for(;i<xx.length;i++){ 
     if(deleted_something==true) xx[i].text = (parseInt(xx[i].text) - 1).toString();continue; 
     if(xx[i].text == id) xx.splice(i,1); i--; deleted_something=true; 
    }); 
    xx.push({text:i.toString(),done:new_el_done}); 
} 
//how to use it 
update("3",true); //delete id 3 and then push new one with "true" as done value 

我不知道爲什麼你想要這種行爲。刪除應該只是刪除。在這種情況下,請忽略我的推線。然後推只是推。它是分開完成的。 因爲您的需求會詢問此行爲,所以我將它們組合在「更新」功能中。

0
xx = []; 
xx[-1] = true; 
xx[1] = false; 
xx[2] = true; 
xx[3] = false; 
xx[4] = false; 

// update index: 4 => 5 
xx[5] = xx[4] 
delete xx[4] 

// delete at index: 3 
delete xx[3] 

// add at index: 8, value: true 
xx[8] = true 
0

最後我用所有的答案,現在我知道了:

//值= []來了很多價值

$scope.updateTodo = function() { 
    $.ajax({ 
     url: '/Home/Update', 
     type: 'GET', 
     async: false, 
     data: { todoName: $scope.todoName, todoAge: $scope.todoAge, todoId: $scope.todoId }, 
     contentType: 'application/json; charset=utf-8', 
     dataType: 'json', 
     success: function (data) { 
      var foundIndex = find(value, $scope.todoId); 
      if (foundIndex != undefined) { 
       value.splice(foundIndex, 1) 
       $scope.Sample = value; 
       $scope.Sample.push(data.data); 
      } 
     } 
    }); 
}; 

var find = function (value, stuId) { 
    for (var i = 0; i < value.length; i++) { 
     if (value[i].StudentId == stuId) { 
      return i; 
     } 
    } 
}