0

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

}

2 Answers 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.

Sign up to request clarification or add additional context in comments.

Comments

1
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");
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.