Home > front end >  Get previous element of an unordered array of objects by ID
Get previous element of an unordered array of objects by ID

Time:10-15

how can I access the previous element of this array of objects by the attribute "id", when the id is not sequenced one after the other. Assuming im in the las item of this array (id 6) and want to access to the previous one (id 4), but the code need to be automatic, like dont the only that element, I think it need to ask if the previous id is "null" to substract n number from the id to reach that element and to work in every part of the array. Thanks

[
    {
     "id": 1,
     "nombre": "DITTO-X2 Looper",
     "precio": "10999",
     "img": "DITTO-X2.png",
     "categoria": "pedales",
     "sub_categoria": "loop_pedal",
     "descripcion": "looper",
     "marca": "ditto"
    },
    {
     "id": 2,
     "nombre": "Guitarra Taylor 562ce",
     "precio": "73999",
     "img": "guitarra562ce.png",
     "categoria": "cuerdas",
     "sub_categoria": "guitarra_acustica",
     "marca": "taylor",
     "descripcion": "Taylor’s Grand Concert 12-strings reaffirm Taylor’s heritage of easy-playing double course instruments thanks to a lap-friendly body size, a 12-fret neck, and a 24-7/8-inch scale length. The slinky handfeel makes fretting and bending strings easier, the neck and body are comfortably balanced, and the compact body produces a clear 12-string voice. The hardwood mahogany top adds just enough compression to the attack to smooth out the response, bringing an appealing consistency across the tonal spectrum, while still capturing the beautiful octave shimmer. V-Class bracing adds another dimension of musicality, improving volume and sustain so that notes and chords bloom and expand as they resonate. It makes a great 12-string choice for tracking in a studio, and behaves well with other instruments in a live setting. Refined aesthetic touches include a shaded edgeburst body and neck, faux tortoise shell binding, a rosette of faux tortoise shell and grained ivoroid, and a grained ivoroid Century fretboard inlay."
    },
    {
     "id": 4,
     "nombre": "BC-1x",
     "precio": "38000",
     "img": "1633645625329_img_.png",
     "categoria": "pedales",
     "sub_categoria": "bass_pedal",
     "descripcion": "Pedal",
     "marca": "boss"
    },
    {
     "id": 6,
     "nombre": "VE-500",
     "precio": "30000",
     "img": "1634188562527_img_.jpg",
     "sub_categoria": "vocal_pedal",
     "marca": "BOSS",
     "descripcion": "If you’re like most guitarists, you spend a lot of time choosing the right amps and pedals to craft your own personalized sound. But if you sing in your band as well, it hasn’t always been so easy to give your vocal sounds that same attention to detail—until now. The advanced VE-500 Vocal Performer provides everything you need to achieve impressive vocal harmonies and effects on stage, and all in streamlined stompbox that integrates seamlessly with the regular guitar effects on your pedalboard."
    }
] 

CodePudding user response:

Find index by Array.findIndex then get the result by Array[index-1].

const inputArray = [{
    "id": 1,
    "nombre": "DITTO-X2 Looper",
  },
  {
    "id": 2,
    "nombre": "Guitarra Taylor 562ce"
  },
  {
    "id": 4,
    "nombre": "BC-1x"
  },
  {
    "id": 6,
    "nombre": "VE-500"
  }
]

const inputId = 6;
const indexById = inputArray.findIndex(e => e.id == inputId);
console.log(inputArray[indexById-1]);

CodePudding user response:

Here is a step by step solution:

var currentId = 6;
  1. Use findIndex to find the index of the object with currentId (https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex)
var currentIndex = arr.findIndex(function (item) {
  return item.id === currentId;
});
  1. If the index is 0 there are no previous item, if it's -1 there are no item with id: currentId
if (currentIndex === 0 || currentIndex === -1) {
    // Invalid currentIndex
}
  1. Get the array item with index currentIndex - 1
var previousElement = arr[currentIndex - 1];
  • Related