简体   繁体   English

无法到达已删除的数组对象

[英]Cannot Reach Deleted Array Object

We're working on an app that will show a list of an employee's shifts. 我们正在开发一款可显示员工轮班列表的应用程序。 If a shift has already passed, we want to delete it from the array of shifts and therefore not print it to the DOM. 如果一个班次已经过去,我们想从班次数组中删除它,因此不将其打印到DOM。

We are effectively deleting the object with the delete property of arrays. 我们正在使用数组的delete属性有效地删除对象。 Doing this, we know that deleting will leave the object as undefined . 这样做,我们知道删除将使对象保留为undefined Yet when it prints on screen, the undefines appear (this part makes sense), but when we try to grab them with a function in order to fix this issue (we're going to copy the array without the undefines), we can't reach the undefines. 然而,当它在屏幕上打印时,出现未定义(这是很有意义的),但是当我们尝试使用某个功能来抓取它们以解决此问题时(我们将复制没有未定义的数组),我们可以到达未定义的位置。

How do we snag the undefines? 我们如何抓住未定义的地方?

HTML : HTML

<ul>
    <li ng-repeat="shift in CurrentUser.orderedSchedule = (schedule | orderBy:Time.getSortingDate:false)">
        <p>Start time: {{Time.makeDate(shift.start)}} 
        {{Time.makeTime(shift.start)}}</p>
        <p>end time: {{Time.makeDate(shift.end)}} 
        {{Time.makeTime(shift.end)}}</p>
    </li>
</ul>

UserController.js : UserController.js

$scope.getShifts = function(){
    $scope.schedule = $scope.CurrentUser.user.shifts; //array of objects that gets printed on screen
    var now = new Date();
    for (var indexOfShift in $scope.schedule){ //checks each shift
        var start = $scope.schedule[indexOfShift].start; //date object, the start of the shift
        if(start instanceof Date){
            if(parseInt(start.getTime())-parseInt(now.getTime())<0){
                //if the 'future time' has already passed now
                //remove the shift
                alert(start+" is in the past");
                delete $scope.CurrentUser.user.shifts[indexOfShift]; //deleting shift
                $scope.schedule = $scope.CurrentUser.user.shifts; //trying to update that we removed it, so we can find undefined
            }
        }
    }
    for(var indexOfShift in $scope.schedule){ //looping shifts again to look for undef
        console.log($scope.schedule[indexOfShift]); //this prints all of them but the undefines don't appear
        if($scope.schedule[indexOfShift].start===undefined){ //we never hit this
            alert("found a blank one");
        }
    }
};

And here is a sample of what our user data with the shifts look like (an array of objects): 以下是我们的用户数据随变化的样例(对象数组):

$scope.CurrentUser.user = [
    { 
        'name': 'John Smith',
        'shifts':[
                {'start':new Date(2012, 07, 27, 4, 44),
                'end': new Date(2012, 07, 27, 12, 21)
                },
                {'start':new Date(2014, 09, 09, 20, 02),
                'end': new Date(2014, 09, 10, 7, 06)
                }
        ]
    }
];

Why can't we reach the undefines? 为什么我们达不到定义? Where have they gone? 他们去哪了?

The for (var i in arr) loop will not loop through undefined items. for(var i in arr)循环不会遍历未定义的项目。 Two solutions: 两种解决方案:

  1. Instead of delete arr[indexOfShift], use arr[indexOfShift] = null; 代替删除arr [indexOfShift],请使用arr [indexOfShift] = null;
  2. Instead of for(var i in arr), use a numerical loop as follows: 代替for(var i in arr),使用如下数字循环:

for (var i = 0; i < myLength; i++){ // do something } for(var i = 0; i <myLength; i ++){//做某事}

Use Splice for your case. 在您的情况下使用Splice。

Delete in this case will only set the element as undefined: 在这种情况下,删除只会将元素设置为未定义:

myArray = ['a', 'b', 'c', 'd'] myArray = ['a','b','c','d']

delete myArray[0] 删除myArray [0]

myArray myArray的

result: [undefined, "b", "c", "d"] 结果:[未定义,“ b”,“ c”,“ d”]

Splice actually removes the element from the array: Splice实际上从数组中删除了该元素:

myArray = ['a', 'b', 'c', 'd'] myArray = ['a','b','c','d']

myArray.splice(0, 2) myArray.splice(0,2)

myArray myArray的

result: ["c", "d"] 结果:[“ c”,“ d”]

for more about splice: Microsoft Link 有关拼接的更多信息: Microsoft Link

FOR Example from Mizzila: 对于来自Mizzila的示例:

var myFish =["angel", "clown", "drum", "mandarin", "surgeon"];

//removes 1 element from index 3
removed = myFish.splice(3, 1);
//myFish is ["angel", "clown", "drum", "surgeon"]
//removed is ["mandarin"]

use this in your case. 在您的情况下使用此功能。

 //delete $scope.CurrentUser.user.shifts[indexOfShift]; 
$scope.CurrentUser.user.shifts.splice(indexOfShift, 1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM