Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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:

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
                ]
            }
        ]

}
share|improve this question
up vote 1 down vote accepted
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.

share|improve this answer
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");
})
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.