获取对象对象返回angularjs

时间:2017-05-25 14:12:55

标签: javascript angularjs arrays json

如何在以下代码中访问locfrnd 我创建了一个包含2个元素的数组PlaceCollection

  1. place
  2. locfrnd这是一个数组
  3. 我尝试访问place并且能够访问它,但locfrnd我收到以下错误

    var ctry = $scope.country;
    var frnds = [];
    
    angular.forEach($scope.Friend, function (friend) {
        var eachFriend = {
            name: friend
        }
        frnds.push(eachFriend);
    });
    
    var record = {
        place: ctry,
        locFrnd: frnds
    }
    
    $scope.placeCollection.push(record);
    
    //I am getting object object from the following code
    for (var j = 0; j < $scope.placeCollection.length; j++) {
        alert($scope.placeCollection[j].locFrnd);
    }
    

2 个答案:

答案 0 :(得分:0)

您可以尝试JSON.stringify,如下所示:

var arr = [{place: 'ABC', locFriend: 'XYZ'}, {place: 'abc', locFriend: 'xyz'}];
alert(JSON.stringify(arr));

你的locFrnd是一个数组,当你在数组上发出警报时,你会得到[objcet Object]。

答案 1 :(得分:0)

这也应该有用

   for (var j = 0; j < $scope.placeCollection.length; j++) {
        var locFrndArray = $scope.placeCollection[j].locFrnd;
            locFrndArray.map(function(item){
                                            alert(item.name })
                                         }
}