将Json对象数组转换为angularjs

时间:2016-06-30 13:50:18

标签: javascript arrays angularjs

我有这个对象列表 -

var o_list = [{id:1,name:jo},{id:2,name:mo}];

我想提取这样的东西 -

var list = [1,2]

是否已经构建了任何内容或者我们如何实现它。

6 个答案:

答案 0 :(得分:2)

你可以映射它

var o_list = [{id:1,name:"jo"},{id:2,name:"mo"}];

var list = o_list.map(x => x.id);

document.body.innerHTML = '<pre>' + JSON.stringify(list, 0, 4) + '</pre>';

答案 1 :(得分:2)

您可以使用过滤器执行此操作 https://docs.angularjs.org/api/ng/filter/filter

var myApp = angular.module('myApp', []);

myApp.controller("myCtrl", ['$scope', '$filter',
  function($scope, $filter) {
    $scope.values = [{
      id: 1,
      name: 'asdas'
    }, {
      id: 2,
      name: 'blabla'
    }];
    
    // this is how you use filters in your script
    console.log($filter('extract')($scope.values));
  }
]);

myApp.filter('extract', function() {
  return function(input) {
    return input.map(x => x.id);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
  <div ng-controller="myCtrl">
    <ol ng-repeat="id in values | extract">
      <li ng-bind="id"></li>
    </ol>
  </div>
</div>

答案 2 :(得分:0)

您只能使用javscript forEach数组方法来获得所需的结果

var o_list = [{id:1,name:'jo'},{id:2,name:'mo'}];

var list =[];
o_list.forEach(function(item){
list.push(item.id)

})

console.log(list)

JSFIDDLE

答案 3 :(得分:0)

from nltk import wordnet as wn
def split_word(self, word):
    result = list()
    while(len(word) > 2):
        i = 1
        found = True
        while(found):
            i = i + 1
            synsets = wn.synsets(word[:i])
            for s in synsets:
                if edit_distance(s.name().split('.')[0], word[:i]) == 0:
                    found = False
                    break;
        result.append(word[:i])
        word = word[i:]
   print(result)

答案 4 :(得分:0)

你可以使用这个简单的for循环

var o_list = [{id:1,name:'jo'},{id:2,name:'mo'}];
var s=[];
for (var i=0;i< o_list.length;i++){
 s.push(o_list[i].id);

}

答案 5 :(得分:0)

替代你可以使用underscore.js每种方法来获得结果

      var o_list = [{id:1,name:'jo'},{id:2,name:'mo'}];

        var list =[];
        _.each(o_list, function(d){
                         list.push(d.id)
                      });