0

I have a scenario in which I need to display the data in an increasing order of the index number.

myArray = [
{custom_carousel: false, default_label: "SmartCards", index: 3, visible: true}, 
{custom_carousel: false, default_label: "Pathways", index: 2, visible: false},
{custom_carousel: false, default_label: "Pathways", index: 1, visible: false},
{custom_carousel: false, default_label: "Pathways", index: 0, visible: false}
]

Should I first sort the array or add if else if condition ?

if(index === 0){

}else if (index === 1){

}else if (index === 2){

}else if (index === 3){

}

vikas95prasad
  • 1,234
  • 1
  • 12
  • 37
  • 4
    `.sort` obviously. What if you have more items in your array? Are you gonna go and change the code and add more `if`s every time that happens? *"2 or more times? Do a loop."* – nicholaswmin Jan 31 '19 at 12:41
  • 2
    Just use `myArray.sort((a,b) => a.index - b.index)` and loop through them for whatever you need them for. – adiga Jan 31 '19 at 12:42
  • 1
    Possible duplicate of [Sorting an array of JavaScript objects](https://stackoverflow.com/questions/979256/sorting-an-array-of-javascript-objects) and [Sort Array based on Object Attribute - Javascript](https://stackoverflow.com/questions/15593850) – adiga Jan 31 '19 at 12:47
  • I've commented above but reading this again I'm a bit confused. How exactly would you use the `if/else` to sort? – nicholaswmin Jan 31 '19 at 12:49
  • @adiga this is not a duplicate question. I need to know which one should be preferred. – vikas95prasad Jan 31 '19 at 12:50
  • @NikKyriakides While iterating over the loop i would compare index value in if else if whereas if i sort then i just need to iterate and show the values. – vikas95prasad Jan 31 '19 at 12:51
  • Read Nik's first comment. You won't know how many items are going to be in `myArray` when writing the code. Array size could vary. – adiga Jan 31 '19 at 13:09

4 Answers4

2

You can use the sort method based on index values:

myArray = [
{custom_carousel: false, default_label: "Pathways", index: 2, visible: false},
{custom_carousel: false, default_label: "SmartCards", index: 3, visible: true}, 
{custom_carousel: false, default_label: "Pathways", index: 1, visible: false},
{custom_carousel: false, default_label: "Pathways", index: 0, visible: false}
]
console.log(myArray.sort((a,b)=>a.index - b.index));
SLePort
  • 15,211
  • 3
  • 34
  • 44
2
myArray.sort(function(a, b){return a.index - b.index});

Will sort your array by index

Stiks
  • 221
  • 1
  • 9
1

I suggest to sort the array in advance and then access by the index of the array.

Reasons

  • easy to maintain with more elements of the array
  • easy to access in wanted order
  • code ie easy to understand, instead of having some code, which reason is not obvious and requieres explanation
  • access is fast, instead of iterating the array always from the beginning for getting the next item.

var myArray = [{ custom_carousel: false, default_label: "SmartCards", index: 3, visible: true }, { custom_carousel: false, default_label: "Pathways", index: 2, visible: false }, { custom_carousel: false, default_label: "Pathways", index: 1, visible: false }, { custom_carousel: false, default_label: "Pathways", index: 0, visible: false }]

myArray.sort(({ index: a }, { index: b }) => a - b);

console.log(myArray);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

Sorting will take O(n+log(n)) time and if-else-if-else will take O(n2) time. So sorting is better choice.

Alexey
  • 71
  • 7