4

When iterating with a for loop it's more convenient to use this:

for (var key in obj){ key }

than these:

for (var i in array){ var key = array[i]; key }
for (var i = 0; i< array.length;i++){ var key = array[i]; key }

But, obviously, declaring arrays is easier:

var array = ['key1', 'key2', 'key3']

var obj =   { key1:'',key2:'',key3:'' }

Assuming I only care about keys and don't care about order, is there a way of declaring objects with just keys and no values (more easily than above)? I just don't like using

:'',    

It looks like a weird smiley face.

Edit: for-of would definitely be the way to go, just not yet.

11
  • 2
    You might just be looking at an array then right ? If the values are empty why do you need it to be stored as an object anyways. Commented Feb 26, 2016 at 19:49
  • 1
    Is there some reason an array won't work for you? Commented Feb 26, 2016 at 19:50
  • 2
    You shouldn't use for...in loops to iterate over arrays fwiw. Commented Feb 26, 2016 at 19:51
  • @forgivenson because constant time lookups. Commented Feb 26, 2016 at 19:54
  • Objects are JS structures for key/value pairs. If you don't want key/value pairs use another structure like an array. For arrays you can use the even more convenient arr.forEach. Commented Feb 26, 2016 at 19:58

2 Answers 2

1

use lodash

var thing = ['a','b','c']
_.mapKeys(thing)
    Object {a: "a", b: "b", c: "c"}
_(thing).mapKeys().mapValues(_.constant(true)).value()
    Object {a: true, b: true, c: true}
Sign up to request clarification or add additional context in comments.

4 Comments

for ( var key in _.mapKeys(thing) ) ?
If you just want to loop through the values, you can do: _.each(thing, function(n){// do something with n})
trying to stick with a for loop: python > ruby
I feel like typing them out is just better in every imaginable way
1

ES5's official answer to this annoyance is the forEach method, which is built into the Array type. If you are using an Array and not an object, it accomplishes the same purpose, although it looks a bit different.

this.balls.forEach(function(ball) {
  this.space(ball);
}, this);

(The ,this ensures the right context of the this keyword)

In ES6 with the arrow function, this is just:

this.balls.forEach(ball => {
  this.space(ball);
});

All versions of JavaScript define for k in o as a way of iterating keys, not array indexes or values, so it's something you'll want to avoid unless you're iterating through the keys of a dictionary.

2 Comments

Sadly the OP added this edit to the question "Edit: forEach is not the Answer, for-of would def be the way to go, just not yet."
@Andy Eugh...I had already seen that edit, but if your opposition to a particular approach is the wording used for it, you should write your own language that translates your own preference to another. ayCaramba! --> .substring(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.