Can one set multiple properties inside an object literal to the same value?

35,770

Solution 1

You could set a line of equality between various properties:

var foo = {};
foo.a = foo.b = foo.c = "Hello";

Or you could just create a method that does the mass-assignment for you:

var foo = {
    setValue: function( props, value ) {
        while ( props.length ) this[ props.pop() ] = value;
    }
}

foo.setValue( [ "a", "b", "c" ] , "Foo" );

Solution 2

An update to this (in terms of the latest JavaScript abilities) avoiding unwanted defined vars:

{
  let v;
  var obj = {
     "a": (v = 'some value'),
     "b": v,
     "c": v
  };
}

This will mean v won't be defined outside the block, but obj will be.

Original answer

Another way of doing the same thing is:

var v;
var obj = {
     "a": (v = 'some value'),
     "b": v,
     "c": v
};

Solution 3

You could try this. It's not the syntactic sugar you're looking for (eg. {a,b,c:1, d:2}) but it's another way to do it, although all of these answers are pretty much fine.

(object,fields,value)=>Object.assign(object||{}, ...fields.map(f=>({[f]:value}) ))

Explanation:

(object,fields,value)=>

Takes an object (or falsey value if you want a new object, feel free to rearrange the argument order)

Object.assign(object||{},

Will return an object based on object and it will mutate the object. To disable this, simply add a first argument object literal like this Object.assign({}, object || {}, ...

...fields.map(f=>({[f]:value}) )

Will spread the array of fields mapped to objects as a list of extra arguments to Object.assign. ['a','b'].map(f=>({[f]:value}) ) will give [{a:value}, {b:value}] and f(...[{a:1},{b:1}]) is like f({a:1},{b:1}). Object.assign does the rest :)

Solution 4

There's yet another approach: using a mapping function...

// This will be standard! 
if (!Object.fromEntries)
  Object.fromEntries = entries => entries.reduce ((o, [key, value]) => ({
     ...o,
     [key]: value
  }), {})


const setSameValue = (source, props, value) => ({
  ...source,
  ...Object.fromEntries (
     props.map (prop => [prop, value])
  )
})

// The important part: do what you want with ease!
const output = setSameValue ({}, ['1', '01'], 'string 1')

const obj = { x: 1, y: 'hello' }

const output2 = setSameValue (obj, ['1', '01'], 'string1')

console.log ('output1:', output)
console.log ('output2:', output2)

Solution 5

You could wrap in a closure too, if you didn't want multiple local vars. This syntax seems to be popular (but ugly):

var obj = (function() { var v='some value'; return { a:v, b:v, c:v }; })();
Share:
35,770
PitaJ
Author by

PitaJ

I'm a web developer with a focus on Node.js backend and frontend web technologies.

Updated on November 11, 2020

Comments

  • PitaJ
    PitaJ over 3 years

    For example, can I do this?:

    { 
       a: b: c: d: 1,
       e: 2,
       geh: function() { alert("Hi!") }
    }
    

    EDIT: Is there some way I can avoid doing this?:

    { 
       a: 1,
       b: 1,
       c: 1,
       d: 1,
       e: 2,
       geh: function() { alert("Hi!") }
    }