使用angular.foreach获取数组中的值以循环对象值

时间:2023-02-05 02:32:50

I am looping an object to get values for a particular set (here it is _source). Here in the example I would like to loop the object and get the _source values from the object and push in array.

I am using angular.forEach for this. However when I use this function I am not getting an array. I am looking for an output like:

我循环一个对象来获取特定集合的值(这里是_source)。在这个示例中,我想循环对象并从对象获取_source值并推入数组。我正在使用angular.forEach。但是当我使用这个函数时,我没有得到一个数组。我正在寻找一个输出:

myarr[0] = [value1:0, value2: "2", value3:"MON"]
myarr[1] = [value1:1, value2:"3", value3: "MON1"]

My code:

Mysvc.getSourceValues()
            .success(function (data) {

                srcobj= data.hits.kal;
                angular.forEach(srcobj, function(value, key) {

                    var sourcevalues= value._source;
                    //this.push(key + ': ' + value);
                });



            })
            .error(function (error) {
                $log.info("Unable to load values");
            })

My srcobjdata:

{

        "kal": [
            {
                "_index": "log2015.18",
                "_type": "MT_DETAIL",
                "_sc": null,
                "_source": {
                    "value1": 0,
                    "value2": "2",
                    "value3": "MON"
                },
                "sort": [
                    1
                ]
            },
            {
                "_index": "logw-2015.18",
                "_type": "MT_DETAIL",
                "_sc": null,
                "_source": {
                    "value1": 1,
                    "value2": "3",
                    "value3": "MON1"
                },
                "sort": [
                    2
                ]
            },
            {
                "_index": "log-2015.18",
                "_type": "MT_DETAIL",
                "_sc": null,
                "_source": {
                   "value1": 3,
                    "value2": "265",
                    "value3": "MON2"
                },
                "sort": [
                    3
                ]
            },
            {
                "_index": "log2015.18",
                "_type": "MT_DETAIL",
                "_sc": null,
                "_source": {
                   "value1": 4,
                    "value2": "5",
                    "value3": "MON5"
                },
                "sort": [
                    1
                ]
            },
            {
                "_index": "log2015.18",
                "_type": "MT_DETAIL",
                "_sc": null,
                "_source": {
                    "value1": 7,
                    "value2": "3",
                    "value3": "MON8"
                },
                "sort": [
                    1
                ]
            }
        ]

}

2 个解决方案

#1


angular.forEach(srcObj, function(value, key) {                    
    angular.forEach(value, function(valueI) {
        console.log(valueI._source);
    })
});

Try this. You value gets an array of values, and then you would have to loop through each of the array elements, which will contain the source property.

试试这个。您获取一个值数组,然后您必须遍历每个数组元素,这些数组元素将包含source属性。

#2


Mysvc.getSourceValues()
.success(function(data) {
     var myarray = [];

    srcobj = data.hits.kal;

    angular.forEach(data.kal, function(value, key){
        myarray.push(value._source);
    });
})
.error(function(error) {
    $log.info("Unable to load values");
})

#1


angular.forEach(srcObj, function(value, key) {                    
    angular.forEach(value, function(valueI) {
        console.log(valueI._source);
    })
});

Try this. You value gets an array of values, and then you would have to loop through each of the array elements, which will contain the source property.

试试这个。您获取一个值数组,然后您必须遍历每个数组元素,这些数组元素将包含source属性。

#2


Mysvc.getSourceValues()
.success(function(data) {
     var myarray = [];

    srcobj = data.hits.kal;

    angular.forEach(data.kal, function(value, key){
        myarray.push(value._source);
    });
})
.error(function(error) {
    $log.info("Unable to load values");
})