JavaScript Returning a new object, and modifying its property, in one expression

I found myself wanting to return a 180 degree hue-rotated color from a function. My project's using D3, so that's the library I'm using to do this. Unfortunately, D3 doesn't come with a function to rotate the hue.

This question is not specific to D3, I'm just using it as an example.

So I had to do something like this:

var c = d3.hsl(colorString);
c.h += 180;
return c;

Concise, but it's three expressions. Doesn't scratch my nerd itch. What I wish I could do is:

return d3.hsl(colorString).h += 180

but obviously that just returns the value of h, not the entire object.

Here is the ridiculous construction I had to resort to, using some Underscore.js:

return  _.create(Object.getPrototypeOf(d3.hsl()),
        _.mapObject(d3.hsl(colorString), function(val, key) {
            return key === "h" ? val + 180 : val;
        }));

See, I can't just return the result of mapObject() because it still needs to use the prototype of what d3.hsl() returns, which is why I needed to wrap that in a create().

So this works. It's just kind of ridiculous.

Is there any way that I could do something in one line, such as...

return d3.hsl(colorString).with180AddedToH();
Answer:1

You could make a more generic version of the other answer here like:

console.log(mutate(o, {h: o.h + 180, j: "abc"}));

function mutate(o, m){
    for(p in m){
        if (p in o){
            o[p] = m[p];
        }
    }
    return o;
}

but you still have to bind your variable on another line in order to reference it twice. You could alter this function to accept an object of mutator functions like:

console.log(mutate(d3.hsl(colorString), {h: function(x){return x + 180}}));

function mutate(o, m){
    for(p in m){
        if (p in o){
            o[p] = m[p](o[p]);
        }
    }
    return o;
}

Where it matches on property name and applies that function to the property and then sets it. It then returns the object so you could end up with

return mutate(d3.hsl(colorString), {h: function(x){return x + 180});

which still seems a little heavy for what you're trying to do, but in the right circumstances might be useful. Although in ES6 you could get away with:

return mutate(d3.hsl(colorString), {h: x => x + 180});
Answer:2

you can return a function so why not:

function with180AddedToH(obj) {
    obj.h += 180;
    return obj;
}

and then:

return with180AddedToH(d3.hsl(colorString))

or is this only going to occur once? and this is too much?

Answer:3

I'm looking to append elements conditionally to a container based on whether the tags data matches the key variable. The way tags' data is returned is like tag1 tag2 tag3 tag4 tag5, the key is ...

I'm looking to append elements conditionally to a container based on whether the tags data matches the key variable. The way tags' data is returned is like tag1 tag2 tag3 tag4 tag5, the key is ...

I'd like to track a form on my real-estate website. The difficulty is that I have 3 radio buttons (only one can be chosen) and I don't know how to link the button chosen to the event that will be send ...

I'd like to track a form on my real-estate website. The difficulty is that I have 3 radio buttons (only one can be chosen) and I don't know how to link the button chosen to the event that will be send ...

  1. google analytics event tracking
  2. google analytics event category
  3. google analytics event value
  4. google analytics event tracking examples
  5. google analytics event goals
  6. google analytics event funnel
  7. google analytics event flow
  8. google analytics event types
  9. google analytics event tag
  10. google analytics event timestamp
  11. google analytics event category examples
  12. google analytics event limits
  13. google analytics event setup
  14. google analytics event value string
  15. google analytics event custom dimension
  16. google analytics event label not set
  17. google analytics event count
  18. google analytics event label character limit
  19. google analytics event code generator
  20. google analytics event tracking gtag

I have several components that use basically the same table so I'm in the process of abstracting out that table. I have solved most of my dynamic table population needs but have yet to find a solution ...

I have several components that use basically the same table so I'm in the process of abstracting out that table. I have solved most of my dynamic table population needs but have yet to find a solution ...

  1. angular passing click event

I'm getting an unusual error: ctx_wrap.fillStyle is not a function on this line: ctx_wrap.fillStyle('#b8b8b8'); in here: ... this.paint = function() { self.path(ctx_wrap); if (!isOver) {...

I'm getting an unusual error: ctx_wrap.fillStyle is not a function on this line: ctx_wrap.fillStyle('#b8b8b8'); in here: ... this.paint = function() { self.path(ctx_wrap); if (!isOver) {...