1

I have array like this:

const arr2 = [
    one => {
      'Hello';
    },
    two => {
      'Answer';
    },
    three => {
      'LOREM IPSUM';
    },
  ];

If javascript have arrow operator inside array then how can i push value at one or at two.

Copy Paste
  • 51
  • 1
  • 8
  • 1
    This is an array of arrow functions. If you want something like PHP's arrays, that's not at all the same. – VLAZ Jun 28 '22 at 05:24
  • What exactly you mean by "how can i push value at one or at two"? Do you mean call the first or second arrow function inside the array while passing it argument(s)? – ZeroFlex Jun 28 '22 at 05:26
  • 1
    PHP's associative array is JavaScript object. – Han Moe Htet Jun 28 '22 at 05:27
  • if your goal is to create key-value pairs, you could use JavaScript [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) as well. Maps have more features than [JS Objects](https://stackoverflow.com/questions/18541940/map-vs-object-in-javascript) – ZeroFlex Jun 28 '22 at 05:33

1 Answers1

1
const arr2 = [
    one => {
      'Hello';
    },
    two => {
      'Answer';
    },
    three => {
      'LOREM IPSUM';
    },
  ];

The one above is basically an array of arrow functions.

so if you do arr2[0] it will print

one => {
      'Hello';
    }

If you wanna do normal push and pop, you can do the same as what you do with arrays in JS

But by your question of double arrow operator , im assuming you wanna have to change values at particular index.

You can always do by arr2[1] = someFunc you wanna assign

Update:

this is what i did and this is the console.log for the same:

const arr2 = [
    one => {
      'Hello';
    },
    two => {
      'Answer';
    },
    three => {
      'LOREM IPSUM';
    },
  ];
  
  arr2.push("hey")
  arr2[1] = "what is this even"
  
  console.log(arr2)

/** [one => {
    'Hello';
  }, "what is this even", three => {
    'LOREM IPSUM';
  }, "hey"] **/

Hope it clears :)

Do let me know in case if any concerns

Gaurav Roy
  • 11,175
  • 3
  • 24
  • 45