0

我有一个数组:

 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(添加)新行。

我怎样才能做到这一点 ?

4

5 回答 5

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 });
    }
}
于 2013-10-09T08:36:23.773 回答
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');
}  

请记住,使用“删除”关键字不会从数组中删除元素——因此我使用了拼接方法

于 2013-10-09T07:36:50.897 回答
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

我不知道你为什么想要这种行为。删除应该只是删除。在这种情况下,请忽略我的 1 行推。然后推只是推。它是单独完成的。我将两者结合在“更新”功能中,因为您的要求要求这种行为。

于 2013-10-09T07:38:54.970 回答
0

最后我用了所有的答案,现在我明白了:

//value=[] 来很多值

$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;
        }
    }
}
于 2013-10-09T12:36:08.837 回答
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
于 2013-10-09T07:43:39.133 回答