javascript使用对象键和值的变量

ste*_*715 1 javascript jquery

我有以下代码:

 pub.call_response = function() {
            return {
                units: [
                    {
                        test_attributes: {

                        }

                    }
                ]
            };
        };
Run Code Online (Sandbox Code Playgroud)

我有一组变量需要用于键/值,test_attributes如下所示:

var key1 = something;
var key2 = something;
var value1 = something;
var value2 = something;

pub.call_response = function() {
                return {
                    units: [
                        {
                            test_attributes: {
                               key1: value1,
                               key2: value2
                            }

                        }
                    ]
                };
            };
Run Code Online (Sandbox Code Playgroud)

显然,这不起作用.有没有人对如何做这样的事情有任何想法?使用jQuery是可以接受的.

谢谢!

Nic*_*tti 6

你应该做

var key1 = something;
var key2 = something;
var value1 = something;
var value2 = something;

var attributes = {};

attributes[key1] = value1;
Run Code Online (Sandbox Code Playgroud)