在AngularJS中创建地图(Key,Value)结构并填充JSON中的数据

时间:2020-12-25 11:45:33

I have a list in AngularJS, $scope.list[];

我在AngularJS中有一个列表,$ scope.list [];

What I want to know is how can I fill that list to fit a key,value structure like this:

我想知道的是如何填写该列表以适合这样的键值结构:

$scope.list[{key,value},{key,value}];

and I want to fill that "map" with data coming in json format:

我想用json格式的数据填充“map”:

 {
  exportData {
    id: 1,
    name : "Peter",
    lastname : "Smith"
    age : 36
  }
}

Where the Id is going to be the KEY and the rest of the strucure is going to be de VALUE

其中Id将成为KEY,其余的结构将是de VALUE

For example in a structure like this:

例如,在这样的结构中:

[
      1: {
        name : "Peter",
        lastname : "Smith"
        age : 36
      },
    2: {
        name : "John",
        lastname : "Carlos"
        age : 40
      },
    ]

2 个解决方案

#1


3  

I written the below code as per your need, hope it will help you:

我根据您的需要编写了以下代码,希望它能帮助您:

var data = [
                  {
                    id: 1,
                    name : "Peter",
                    lastname : "Smith",
                    age : 36
                  }, {
                    id: 2,
                    name : "Peter",
                    lastname : "Smith",
                    age : 36
                  }
                ];

                $scope.itemList = [];
                angular.forEach(data, function(item){
                    var obj = {};
                    var valObj = {};

                    valObj.name = item.name;
                    valObj.lastname = item.lastname;
                    valObj.age = item.age;

                    obj[item.id] = valObj;
                    $scope.itemList.push(obj);
                });

#2


1  

Hope this function will help you

希望这个功能能帮到你

$scope.transform = function(exportData){ var _value = {}; _value.name = exportData.name; _value.lastname = exportData.lastname; _value.age = exportData.age; var item = [exportData.id, _value]; $scope.list.push(item); }

$ scope.transform = function(exportData){var _value = {}; _value.name = exportData.name; _value.lastname = exportData.lastname; _value.age = exportData.age; var item = [exportData.id,_value]; $ scope.list.push(项目); }

#1


3  

I written the below code as per your need, hope it will help you:

我根据您的需要编写了以下代码,希望它能帮助您:

var data = [
                  {
                    id: 1,
                    name : "Peter",
                    lastname : "Smith",
                    age : 36
                  }, {
                    id: 2,
                    name : "Peter",
                    lastname : "Smith",
                    age : 36
                  }
                ];

                $scope.itemList = [];
                angular.forEach(data, function(item){
                    var obj = {};
                    var valObj = {};

                    valObj.name = item.name;
                    valObj.lastname = item.lastname;
                    valObj.age = item.age;

                    obj[item.id] = valObj;
                    $scope.itemList.push(obj);
                });

#2


1  

Hope this function will help you

希望这个功能能帮到你

$scope.transform = function(exportData){ var _value = {}; _value.name = exportData.name; _value.lastname = exportData.lastname; _value.age = exportData.age; var item = [exportData.id, _value]; $scope.list.push(item); }

$ scope.transform = function(exportData){var _value = {}; _value.name = exportData.name; _value.lastname = exportData.lastname; _value.age = exportData.age; var item = [exportData.id,_value]; $ scope.list.push(项目); }