下划线,清除对象的所有值但保留键

时间:2015-01-22 19:32:05

标签: javascript angularjs

我不知道这是否可能,但我试图做的是"清洁"一个东西。基本的想法是我有一个对象的表格(角度)和点击我想添加一个新的行(控制对象中的一个新项目,但我希望它没有值。我有下划线一些考虑因素是每次进入的对象都会有不同的键值,所以我试图保持这种多态(如果可能的话)。

所以这就是我到目前为止:

<!-- model here for faces, could potentially not be seperate model and read off a length -->
        <div class="inputRepeater" ng-repeat="face in inputFaces" ng-show="face.isCurrent" >
            <div class="trialGrid" ng-init="$faceIndex = $index">
                <table st-table="rowCollection" class="table table-striped">
                    <thead>
                    <tr>
                        <!-- table headers here - this can change if keys are not table exactly table headers with a bit of logic to change table headers to correct titles -->
                        <th ng-repeat="(key, val) in rowCollection[0]">{{key}}</th>
                    </tr>
                    </thead>
                    <tbody>
                    <!-- limit length by current face index + 1 -->
                    <tr ng-repeat="row in rowCollection | limitTo: $faceIndex + 1" ng-class=" { 'curentRowTab2' : $index == $faceIndex }">
                        <td ng-repeat="item in row">{{item}}</td>
                    </tr>
                    </tbody>
                </table>
            </div>
            <div class="stateTab2Progress"><div class="tab2ShiftLeft" ng-click="faceLeft($index)" ng-show="!$first"><i class="fa fa-angle-left"></i></div><div class="progessTitle">Title</div><div class="progessCurrent">Current Progression {{$index + 1}}/{{inputFaces.length}}</div><div class="tab2ShiftRight" ng-show="!$last" ng-click="faceRight($index)"><i class="fa fa-angle-right"></i></div><div class="tab2ShiftRight" ng-show="$last" ng-click="faceRightLast($index)"><i class="fa fa-angle-right"></i></div></div>
            <div class="state2Buttons">Buttons Also Built dynamically</div>
            <button class="tab2Finish" ng-click="startTab3()">Finish</button>
        </div>
    </div>

因此,只有当您在最后一个对象上时,才显示单击右键(最后)功能

$scope.faceRightLast = function(index){
    //copy current row and empty so we keep a polymorphic format, then add it onto current object, thus adding new empty object
    var storeRow = $scope.inputFaces[index];
    //push new face on face tracking array
    $scope.inputFaces.push({'isCurrent' : false });

    storeRow - clear values out?

    //after we have a fresh row change the is current
    $scope.inputFaces[index].isCurrent = false;
    $scope.inputFaces[index + 1].isCurrent = true;
}

所以我正在制作当前级别的副本,然后我想清除这些值并保持键然后按下对象。

这样的事情是可能的还是我应该找出一种新的方法。

谢谢!

2 个答案:

答案 0 :(得分:2)

var newObject = angular.copy(oldObject);

for(var key in newObject){
    if(newObject.hasOwnProperty(key)){
        newObject[key] = null;
    }
}

答案 1 :(得分:1)

var newObject = _.object(_.keys(oldObject));

_.object(_.keys({'a':1,'b':2,'c':3}))
// Object {a: undefined, b: undefined, c: undefined}