如何从angularjs中的现有对象中选择一些数据来创建一个新对象?

时间:2016-03-22 06:41:04

标签: javascript angularjs ionic-framework

我有一个json对象如下。

[{
 "id": "1",
   "username": "vishnu",
 "FromDate": "Thu Apr 21 2016 10:56:45 GMT+0530 (India Standard Time)",
  "selectedProject": "createwhimsy",
  "task": "fixing bugs",
  "time": "1"
  }, {

    "id": "2",
   "username": "vishnu",
  "FromDate": "Wed Mar 02 2016 10:56:45 GMT+0530 (India Standard Time)",
   "selectedProject": "createwhimsy",
   "task": "working on ui of creatwhimsy home page",
    "time": "2"
       }, {
         "id": "3",
     "username": "vishnu",
   "FromDate": "Wed Mar 02 2016 10:56:45 GMT+0530 (India Standard Time)",
      "selectedProject": "bigiron",
      "task": "working on api",
     "time": "5" 
       }, {
     "id": "4",
   "username": "vishnu",
     "FromDate": "Sat Jul 30 2016 12:03:20 GMT+0530 (India Standard Time",
     "selectedProject": "timetracker",
     "task": "working on ui of creatwhimsy home page and admin side home      page",
     "time": "1"
  }, {
       "id": "5",
      "username": "vishnu",
       "FromDate": "Wed Jan 02 2016 10:56:45 GMT+0530 (India Standard Time)",
       "selectedProject": "createwhimsy",
   "task": "fixing bugs",
    "time": "1"
   }]

让对象名称为Data。我想为Data创建一个新对象,但它应该只包含“selectedProject”和“time”。 如何在angularjs中做到这一点?

2 个答案:

答案 0 :(得分:1)

您可以使用欲望数据迭代并创建一个新对象。为此,您可以使用javascript push()功能。 你需要做的是

$scope.log = [];
angular.forEach($scope.names, function(value, key){  
   $scope.log.push({"selectedProject":value.selectedProject,"time":value.time});
});

这是JS小提琴:http://jsfiddle.net/kyaqzgo6/4/

您的示例代码:

<script>
            var app = angular.module('myApp', []);
            app.controller('myCtrl', function($scope) {
                $scope.names = [{
                        "id": "1",
                        "username": "vishnu",
                        "FromDate": "Thu Apr 21 2016 10:56:45 GMT+0530 (India Standard Time)",
                        "selectedProject": "createwhimsy",
                        "task": "fixing bugs",
                        "time": "1"
                    }, {
                        "id": "2",
                        "username": "vishnu",
                        "FromDate": "Wed Mar 02 2016 10:56:45 GMT+0530 (India Standard Time)",
                        "selectedProject": "createwhimsy",
                        "task": "working on ui of creatwhimsy home page",
                        "time": "2"
                    }, {
                        "id": "3",
                        "username": "vishnu",
                        "FromDate": "Wed Mar 02 2016 10:56:45 GMT+0530 (India Standard Time)",
                        "selectedProject": "bigiron",
                        "task": "working on api",
                        "time": "5"
                    }, {
                        "id": "4",
                        "username": "vishnu",
                        "FromDate": "Sat Jul 30 2016 12:03:20 GMT+0530 (India Standard Time",
                        "selectedProject": "timetracker",
                        "task": "working on ui of creatwhimsy home page and admin side home      page",
                        "time": "1"
                    }, {
                        "id": "5",
                        "username": "vishnu",
                        "FromDate": "Wed Jan 02 2016 10:56:45 GMT+0530 (India Standard Time)",
                        "selectedProject": "createwhimsy",
                        "task": "fixing bugs",
                        "time": "1"
                    }];
                $scope.log = [];
                angular.forEach($scope.names, function(value, key){
                    $scope.log.push({"selectedProject":value.selectedProject,"time":value.time});
                });
                console.log($scope.log);

            });
        </script>

答案 1 :(得分:0)

尝试这样的事情:

function SomeFunction() {

 function getdata(callback, url) {

    var obj= {};
    $http.get(url).success(function(response) {

        if (response.jsonname[0].length > 0) {
            for (var i = 0; i < response.jsonname[0].length; i++) {   
                obj= {
                    selectedProject:response.jsonname[0][i].selectedProject,  
                    time:response.jsonname[0][i].time

                }

            }
        } else {

        }
    }).error(function() {
        //something        
    }).then(function() {
        callback(obj);
    });;
}
return {
    getdata: getdata
}

}