When working with objects in javascript and needing to maintain a specific sort order, key/value pairs cannot be used. Instead, utilizing an array with features like ES6 map
is more suitable.
An example structure that could be used is shown below. The id
is specified and all the items
are enclosed within an array. This concept applies to the col
blocks as well.
[
{
id: 21,
items: [
{
col: 'name',
data: {
data1: 'hello',
data2: 'world'
}
},
{
col: 'slug',
data: {
data1: 'hello',
data2: 'world'
}
}
]
},
{
id: 44,
items: [
{
col: 'name',
data: {
data1: 'hello',
data2: 'world'
}
},
{
col: 'slug',
data: {
data1: 'hello',
data2: 'world'
}
}
]
},
]
The Challenge
An issue arises with this method when it becomes necessary to search for specific identifiers such as finding id
44
. In real scenarios, there could be numerous groups instead of just two, adding complexity. Similarly, there may be several columns within each group of items, requiring extensive searching.
Desired Code Structure
let result = get(44, 'slug', 'data2');
Regarding Performance
- Does the provided structure ensure good performance?
- How can efficiency be maintained without compromising performance?