0

I want to push a document with a name in a array

this is the expected output

[ light1 : {value: true } , light2 : { value : false}]

this is what I am trying

                let value1 = {value : false}
                LightName = Light1 ;

                this.lightArray.push(
                  LightName : value1
                     ); 

                let value1 = {value : false}
                LightName = Light2 ;

                this.lightArray.push(
                  LightName : value1
                     ); 

              console.log(lightArray);
ADyson
  • 57,178
  • 14
  • 51
  • 63
nitish
  • 111
  • 2
  • 13
  • 2
    In JavaScript, arrays don't have names, they only have numeric indexes. If you want to give names to things, use an object and set its properties. Aim for `{ light1 : {value: true } , light2 : { value : false}}`. You can still iterate the properties later, if you need to. Or if `value` is the only property of each `light`, you could simplify it to `{ light1 : true , light2 : false}`. I suggest you study the basics of arrays and objects in JavaScript if you're not aware of this distinction. – ADyson Sep 03 '18 at 14:11
  • 1
    Or push objects to an array like `{name: "Light1", data: {value: true, ...}}` – Stefan Blamberg Sep 03 '18 at 14:14
  • No I cannot do that I need to use value with that Light1 directly – nitish Sep 03 '18 at 14:16
  • Then do both. Use an array to save all the objects, then create an object from that array using the name properties as keys for direct reference to the object. Then you have the best of both worlds. Direct access through the object containing references to the array and easier collection management with the array itsself. ( or just use a Map ) – Shilly Sep 03 '18 at 14:19
  • There's no such thing as `[ light1 : ... ]` in Javascript. What you want is literally not possible. – deceze Sep 03 '18 at 15:26

0 Answers0