Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript using variables for object keys and values

I have the following code:

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

                        }

                    }
                ]
            };
        };

I have a set of variables that I need to use for the keys/values in test_attributes as follows:

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

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

                        }
                    ]
                };
            };

Obviously, this does not work. Does anyone have any ideas on how to do something like this? Using jQuery is acceptable.

Thanks!

like image 292
stewart715 Avatar asked Apr 24 '26 03:04

stewart715


1 Answers

You should do

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

var attributes = {};

attributes[key1] = value1;
like image 183
Nicola Peluchetti Avatar answered Apr 26 '26 16:04

Nicola Peluchetti