當 id 沒有一個接一個排序時,如何通過屬性“id”訪問這個物件陣列的前一個元素。假設 im 在這個陣列的 las 項中(id 6)并且想要訪問前一個(id 4),但是代碼需要是自動的,比如不是唯一的那個元素,我認為它需要詢問前一個id 為“null”以從 id 中減去 n 個數字以到達該元素并在陣列的每個部分中作業。謝謝
[
{
"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."
}
]
uj5u.com熱心網友回復:
查找索引,Array.findIndex然后得到結果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]);
uj5u.com熱心網友回復:
這是一個分步解決方案:
var currentId = 6;
- 使用
findIndex找物件的索引與currentId(https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex)
var currentIndex = arr.findIndex(function (item) {
return item.id === currentId;
});
- 如果索引是
0沒有前一項,如果-1是沒有專案id: currentId
if (currentIndex === 0 || currentIndex === -1) {
// Invalid currentIndex
}
- 獲取帶索引的陣列項
currentIndex - 1
var previousElement = arr[currentIndex - 1];
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/315772.html
標籤:javascript 数组 目的 属性
上一篇:如何從.fields獲取元素
