如何修改对象数组中的json对象

时间:2016-06-15 04:14:22

标签: javascript arrays angularjs json asp.net-web-api

我有一个对象数组

<dependency>
    <groupId>org.jboss.logging</groupId>
    <artifactId>jboss-logging</artifactId>
    <version>3.3.0.Final</version>
</dependency>

然后有一个数组,其中包含上一个屏幕中选定的用户

<dependency>
    <groupId>org.glassfish.jersey.ext</groupId>
    <artifactId>jersey-spring3</artifactId>
    <version>2.23</version>
    <exclusions>
        <exclusion>
            <groupId>org.glassfish.hk2.external</groupId>
            <artifactId>bean-validator</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.3.0.RELEASE</version>
    <exclusions>
        <exclusion>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-c3p0</artifactId>
    <version>5.1.0.Final</version>
    <exclusions>
        <exclusion>
            <artifactId>jboss-logging</artifactId>
            <groupId>org.jboss.logging</groupId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.2.0.Final</version>
    <exclusions>
        <exclusion>
            <artifactId>jboss-logging</artifactId>
            <groupId>org.jboss.logging</groupId>
        </exclusion>
    </exclusions>
</dependency>

如果在// This is should be taken from database $scope.users = [{"$id":"1","UserID":3,"Name":"A","Selected":false},{"$id":"2","UserID":4,"Name":"B","Selected":false},{"$id":"3","UserID":5,"Name":"C","Selected":false},{"$id":"4","UserID":6,"Name":"D","Selected":false}] 中找到$scope.usersSelected = [{"$id":"3","UserID":5,"Name":"C","Selected":true,"$$hashKey":"object:83"},{"$id":"4","UserID":6,"Name":"D","Selected":true,"$$hashKey":"object:84"}] Selected属性,我想更新它们。所以我迭代$scope.users,然后在$scope.usersSelected $scope.usersSelected中搜索UserID

UserID

$scope.users属性不会更新。上面的代码出了什么问题?

其次,我需要对上面数组中的数据进行解释,为什么当我从数据库填充数据时总是for (var i = 0; i < $scope.usersSelected.length; i++) { var obj = $.grep($scope.users, function (e) { return e.UserID == $scope.usersSelected[i].UserID; }); obj.Selected = true; } ,当我从另一个屏幕获取数据时,Selected?这些值来自何处?

注意:我正在使用AngularJS和ASP.NET Web API 2从数据库中获取数据。

2 个答案:

答案 0 :(得分:3)

测试工作绝对正常

 $scope.users = [ {
        "$id" : "1",
        "UserID" : 3,
        "Name" : "A",
        "Selected" : false
    }, {
        "$id" : "2",
        "UserID" : 4,
        "Name" : "B",
        "Selected" : false
    }, {
        "$id" : "3",
        "UserID" : 5,
        "Name" : "C",
        "Selected" : false
    }, {
        "$id" : "4",
        "UserID" : 6,
        "Name" : "D",
        "Selected" : false
    } ];

    $scope.usersSelected = [ {
        "$id" : "3",
        "UserID" : 5,
        "Name" : "C",
        "Selected" : true,
        "$$hashKey" : "object:83"
    }, {
        "$id" : "4",
        "UserID" : 6,
        "Name" : "D",
        "Selected" : true,
        "$$hashKey" : "object:84"
    } ]
    var users = $scope.users;
    var usersSelected = $scope.usersSelected;
    for (var i = 0; i < users.length; i++) {
        for (var j = 0; j < usersSelected.length; j++) {
            debugger
            if (users[i].UserID == usersSelected[j].UserID) {
                console.log(users[i].UserID)
                console.log(usersSelected[j].UserID)
                users[i].Selected = true;

            }
        }

    }

    console.log(users)
    console.log(usersSelected);

答案 1 :(得分:1)

用javascript测试。

usersSelected =  $scope.usersSelected;

users = $scope.users;

for (var i=0;i<users.length;i++){

  if (usersSelected.some(function(e) e.UserID == users[i].UserID)) {
    users[i].Selected = true;
    console.dir(users[i]);
  }


}