Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating object with dynamic keys [duplicate]

First off, I'm using Cheerio for some DOM access and parsing with Node.js. Good times.

Heres the situation:

I have a function that I need to create an object. That object uses variables for both its keys and values, and then return that single object. Example:

stuff = function (thing, callback) {   var inputs  = $('div.quantity > input').map(function(){     var key   = this.attr('name')      ,  value = this.attr('value');       return { key : value }   })     callback(null, inputs); } 

It outputs this:

[ { key: '1' }, { key: '1' } ] 

(.map() returns an array of objects fyi)

I need key to actually be the string from this.attr('name').

Whats the best way to assign a string as a key in Javascript, considering what I'm trying to do?

like image 708
JDillon522 Avatar asked Nov 07 '13 14:11

JDillon522


People also ask

Can object have duplicate keys?

No, JavaScript objects cannot have duplicate keys. The keys must all be unique.

How do you add a key value pair in an object dynamically?

To add dynamic key-value pairs to a JavaScript array or hash table, we can use computed key names. const obj = {}; obj[name] = val; to add a property with the value of name string as the property name. We assign val to as the property's value.

What is dynamic key in object JavaScript?

Dynamic Object Keys. A way to handle dynamic keys in objects prior to ES6 is to do something like: let newObject = { name: 'Jane', age: 24 }; const newData = "location"; newObject[newData] = "Nicaragua" // Output {age: "24", location: "Nicaragua", name: "Jane"}

How to create dynamic keys in JavaScript Object?

Lets discuss , how we can create dynamic keys in javascript object. With ES6 objects can be created with dynamic keys in the object declaration. Add another property using Object.defineProperty () static method

How to create an object with dynamic keys in ES6?

With ES6 objects can be created with dynamic keys in the object declaration. Add another property using Object.defineProperty () static method

How to create an object with a computed key in JavaScript?

In the new ES2015 standard for JavaScript (formerly called ES6), objects can be created with computed keys: Object Initializer spec. stuff = function (thing, callback) { var inputs = $ ('div.quantity > input').map (function () { return { [this.attr ('name')]: this.attr ('value'), }; }) callback (null, inputs); }

How do I define an object literal with a dynamic key?

You can't define an object literal with a dynamic key. Do this : There's no shortcut (edit: there's one now, with ES6, see the other answer). I think in es6 you can now do this: var propname = 'thing' { [propname]:5} -> {thing:5} See this for more : developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…


1 Answers

In the new ES2015 standard for JavaScript (formerly called ES6), objects can be created with computed keys: Object Initializer spec.

The syntax is:

var obj = {   [myKey]: value, } 

If applied to the OP's scenario, it would turn into:

stuff = function (thing, callback) {   var inputs  = $('div.quantity > input').map(function(){     return {       [this.attr('name')]: this.attr('value'),     };   })     callback(null, inputs); } 

Note: A transpiler is still required for browser compatiblity.

Using Babel or Google's traceur, it is possible to use this syntax today.


In earlier JavaScript specifications (ES5 and below), the key in an object literal is always interpreted literally, as a string.

To use a "dynamic" key, you have to use bracket notation:

var obj = {}; obj[myKey] = value; 

In your case:

stuff = function (thing, callback) {   var inputs  = $('div.quantity > input').map(function(){     var key   = this.attr('name')      ,  value = this.attr('value')      ,  ret   = {};       ret[key] = value;      return ret;   })     callback(null, inputs); } 
like image 53
Renato Zannon Avatar answered Sep 19 '22 13:09

Renato Zannon