javascript - 如何将数据存储到 AngularJS 数组中

标签 javascript jquery angularjs

$scope.arrSportData = data.sportdata;

        angular.forEach($scope.arrSportData, function(value, key){
             $scope.associatedSportplayer = value;
         console.log($scope.associatedSportplayer);

        //getting reponse

        /*
        Object { id: "1", user_id: "2", sport_id: "1", position_id: "1"}
        Object { id: "2", user_id: "2", sport_id: "2", position_id: "6"}
        Object { id: "3", user_id: "2", sport_id: "3", position_id: "12"}
        Object { id: "4", user_id: "2", sport_id: "5", position_id: "20"}
        */  

        });

我想将 sport_id 放入数组,即数组 [1,2,3,5]

请提前指导谢谢

最佳答案

var datas = [{
  id: "1",
  user_id: "2",
  sport_id: "1",
  position_id: "1"
}, {
  id: "2",
  user_id: "2",
  sport_id: "2",
  position_id: "6"
}, {
  id: "3",
  user_id: "2",
  sport_id: "3",
  position_id: "12"
}, {
  id: "4",
  user_id: "2",
  sport_id: "5",
  position_id: "20"
}];

var sport_ids = datas.map(function(data) {
  return Number(data.sport_id)
});

alert(sport_ids);

因此,您几乎可以利用 javascript 的 map 函数以一种格式通过管道传输数据数组,然后使用从原始数组中提取的数据将其转换为另一个数组。

Documentation

关于javascript - 如何将数据存储到 AngularJS 数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38501688/

相关文章:

javascript - Simplebar 滚动条插件 - 在功能中滚动到顶部/重置位置

javascript - 类型错误 : Cannot find function findIndex in object

javascript - 如果该索引位置存在元素,如何更新 javascript 数组?

javascript - 如何将 ERC20 代币发送到智能合约余额?

javascript - 限制允许选中的复选框数量

javascript - Android chrome pageY 值是否随着最新的 chrome 更新而改变?

javascript - 将 div 的宽度设置为没有 width 属性的 div 的 'display width'

javascript - 从循环中隐藏元素(AngularJS)

javascript - 在 Canvas 内绘制 div 元素

AngularJS加载webservice提供的远程JS Controller