Linked Questions

463 votes
2 answers
513k views

First off, I'm using Cheerio for some DOM access and parsing with Node.js. Here's the situation: I have a function that I need to create an object. That object uses variables for both its keys and ...
JDillon522's user avatar
  • 19.7k
123 votes
8 answers
62k views

Ok so I'm working away on a project in Nodes, and I've come across a small problem with the keys in object literals, I have the following set-up: var required = { directories : { this....
111 votes
4 answers
113k views

Is it at all possible to use variable names in object literal properties for object creation? Example function createJSON (propertyName){ return { propertyName : "Value"}; } var myObject = ...
69 votes
3 answers
83k views

Possible Duplicate: How do I add a property to a Javascript Object using a variable as the name? Use variable for property name in JavaScript literal? Is it possible to add a variable as the ...
jsgroove's user avatar
  • 1,507
28 votes
4 answers
29k views

I am creating a plugin for Jquery and need to have a variable as a key in an object. $(selector).animate({self.settings.direction: '+='+self.displacement+'px'}, "slow" , function () {}); this part ...
salmane's user avatar
  • 4,845
20 votes
1 answer
12k views

I would like to know if there is a clean way to set the value of a key from a string variable when using spread syntax in es6? Something like the following: let keyVar = 'newKey' let newObject = {...
21 votes
1 answer
21k views

I have an Enum: const ingredients = { BREAD_BOTTOM: 'BreadBottom', BREAD_TOP: 'BreadTop', MEAT: 'Meat', BACON: 'Bacon', CHEESE: 'Cheese', SALAD: 'Salad' }; ...
Icaro's user avatar
  • 14.9k
10 votes
7 answers
3k views

Consider the following var a = {foo: "bar"}; Equivalent to var a = {}; a.foo = "bar"; Equivalent to var a = {}; a['foo'] = "bar"; Equivalent to var a = {} var b = "foo"; a[b] = "bar"; Is it ...
Mulan's user avatar
  • 136k
8 votes
2 answers
8k views

I am attempting to add a variable key, with no luck. Here's what I got so far: mysql('translations',{ check: 'element_id', element_id: element_id, {'lang-'+lang_id}: value }); The ...
7 votes
2 answers
15k views

I am trying to set an array element as an object Property Simplified example: var array = ['a', 'b', 'c']; var obj = { array[1]: 'good' } Above causes an error. Update: In fact, I am passing the ...
erosman's user avatar
  • 7,831
6 votes
1 answer
9k views

I would like to define a set of constants, which I then use the values of in another javascript object. I have the below example. This however doesn't seem to work when I am trying to use 0 as a key ...
6 votes
2 answers
6k views

I'm looking for a simple syntax to allow me to create a new object with one property and set the value of that property. Here's an example. I'd love to be able to do something all on one line. let ...
ThomasReggi's user avatar
  • 60.4k
4 votes
5 answers
705 views

I observed a very weird phenomena in javascript and I can't figure out why. Consider this example below state = "West Bengal" city = "Kolkata" country = "India" some_json = {country: {"city": city, "...
1 vote
2 answers
3k views

Fiddle var json = {name: 'chan'}; var variable = 'age'; $.extend(json, {[variable]: 35}); $('#result').html(JSON.stringify(json)); This method works on most popular browsers except IE, I need to ...
Chan's user avatar
  • 1,969
3 votes
1 answer
5k views

From an input field I am sending the value as an argument to the function that sets state. I have multiple input fields so would like to use their name (which is equal to their state key) to then use ...
Middi's user avatar
  • 353

15 30 50 per page
1
2 3 4 5
...
22