简体   繁体   English

如何在Javascript Push中的索引内传递变量

[英]How to pass variable inside index in Javascript push

How to pass variable inside index in JavaScript push.I am using angular. 如何在JavaScript push中传递变量到索引内部。我正在使用angular。 This is what I did so far: 这是我到目前为止所做的:

angular.forEach(Val, function (Value,Key) {
    angular.forEach(Value, function (Value1,Key1) {
        saveDetailArr.push({ 'option_id':Val['option_id'],Key1:$scope[Key1] });
    });
});

Key1 contains "header,footer,text1,text2,left_contianer etc". Key1包含“页眉,页脚,text1,text2,left_contianer等”。 Basically,I want the value of Key1 instead of string "Key1" Currently,it look like: {'option_id':21,Key1:'abc'} But I want it like this: {'option_id':21,header:'abc'} How can I achieve this: 基本上,我想要的是Key1的值,而不是字符串“ Key1”。当前,它看起来像: {'option_id':21,Key1:'abc'}但我想要这样: {'option_id':21,header:'abc'}如何实现:

Use bracket notation for variable property names. 使用括号符号表示变量属性名称。 In your case obj[Key1] should work: 在您的情况下, obj[Key1]应该可以工作:

angular.forEach(Val, function (Value,Key) {
    angular.forEach(Value, function (Value1,Key1) {
        var obj = { 'option_id':Val['option_id'] };
        obj[Key1] = $scope[Key1];
        saveDetailArr.push(obj);
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM