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
    if ($scope.data) {
        $formData = [];
        angular.forEach($scope.data, function(value, key){

            console.log(key);  //  o/p: 'fruitname'
            var k = key;
            var value = value || 'Not Available';
            console.log(value); // o/p: 'apple'

            var parts = {k : value};
            console.log(parts);  // o/p: Object {k: "apple"}
            $formData.push(parts);
        });
    }

Why i cannot able to populate key, while creating parts object. or how can i do the same.

share|improve this question

What you need to do is parts[k] = value; Actually when you assign parts = { k : value }; , This goes something like this :

parts["k"] = value;

So you see instead of taking the value of k, it takes k as string and assign a value to this string as key field.

share|improve this answer
    
yeah... i got it.. i change my code as parts[key] = value; and it is working now. Thank you – Jyotirmay Jul 11 '15 at 13:43
    
well.. anytime.. – binariedMe Jul 11 '15 at 14:01
up vote 1 down vote accepted

it works when i try to do like this.

if ($scope.data) {
    $formData = [];
    angular.forEach($scope.data, function(value, key){

        console.log(key);  //  o/p: 'fruitname'
        var value = value || 'Not Available';
        console.log(value); // o/p: 'apple'

        var parts = {};
        parts[key] = value;
        console.log(parts);  // o/p: Object {"fruitname": "apple"}
        $formData.push(parts);
    });
}
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.