OneCompiler

[JavaScript] How to print an array along with indexes?

I want to print all elements with their indexes on to console and I want to print in the format ['i1:v1','i2:v2',...]., how can I do that in JavaScript?

1 Answer

4 years ago by

The below code serves your purpose to print array elements along with their indices in the format ['i1:v1','i2:v2',...]

  let givenArray = ['zero','one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
  let indexArray= givenArray.map((val)=> {
      return (givenArray.indexOf(val)+ ":" + val);
  })

Try checking the result here: https://onecompiler.com/javascript/3xmt7mf5d

4 years ago by Meera