admin管理员组文章数量:1335380
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.
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 asked Jul 11, 2015 at 13:32 JyotirmayJyotirmay 1,8553 gold badges25 silver badges43 bronze badges2 Answers
Reset to default 4What 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.
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);
});
}
本文标签: javascriptDynamically create angular object with keyvalue from angularforEach()Stack Overflow
版权声明:本文标题:javascript - Dynamically create angular object with key, value from angular.forEach() - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742264800a2443186.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论