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){
}
.sortobviously. What if you have more items in your array? Are you gonna go and change the code and add moreifs every time that happens? "2 or more times? Do a loop."myArray.sort((a,b) => a.index - b.index)and loop through them for whatever you need them for.if/elseto sort?