下划线,清除对象的所有值,但保留键 [英] Underscore, clear all values of an object but keep keys

查看:109
本文介绍了下划线,清除对象的所有值,但保留键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道这是否可行,但是我试图做的是清理"一个对象.基本思想是,我有一个对象(以角度表示)的表格馈送,单击时我想添加一个新行(控制对象中的一个新项目,但我希望它不包含任何值.一些考虑因素是,进入的对象每次都将具有不同的键值,所以我试图保持这种多态性(如果可能).

I don't know if this is possible but what I'm attempting to do is "clean" an object. The basic Idea is I hve a table feeding of an object (in angular) and on click I want to add a new row (a new item in the controlling object, but I want it to have no values in it. I have underscore to play around with. Some considerations are that the object coming in is going to have different key values each time, so I'm trying to keep this polymorphic (if possible).

这就是我到目前为止所拥有的:

So this is what I have so far :

<!-- 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>

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

So the click right (last) function is only shown if you are on the last object looks like so

$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;
}

因此,我正在复制当前级别,然后我希望清除该值并保留键,然后将其推到对象上.

So I'm making a copy of the current level, which I would then like to clean out the values and keep the keys then push onto the object.

是否可能出现这种情况,或者我应该找出新的方法?

Is something like this possible or should I figure out a new approach.

谢谢!

推荐答案

var newObject = angular.copy(oldObject);

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

这篇关于下划线,清除对象的所有值,但保留键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆