0

This is my code:

var newArray = [];
newArray[3] = "Three"
newArray[1] = "One"
newArray[2] = "Two"

Result of this array is:

1: One
2: Two
3: Three

But I want to get result like this (sort by append):

3: Three
1: One
2: Two

Note: I don't want to lose the keys.

F Academy
  • 63
  • 2
  • 9
  • Show the JavaScript that you used as a [mcve]. The code you have so far means nothing because it doesn't even correctly represent correct syntax, so it's impossible to guess exactly how you got those pseudo-code results in the first place. – zer00ne Apr 07 '22 at 06:23
  • 1
    You can normally push the data sequentially into the array using push(), but if you want the data to be pushed sequentially and have the indexing different from the way you pushed it is not possible . – Gopi krishna Apr 07 '22 at 06:24

1 Answers1

1

you should push it sorted inside array, then it will print it in the order you added it.

newArray.push("Three")
newArray.push("One")
newArray.push("Two")

then you can do 1 thing key your assigning code, other then this array create one seprate array for only keys but that keys should be sorted in array like

keysArray.push(3)
keysArray.push(1)
keysArray.push(2)

keysArray.map(myKey=>{
  console.log(newArray[myKey])
})