Answer the question
In order to leave comments, you need to log in
How to sort an array of objects for binary search?
There is an array of objects (there are a lot of objects), you need to sort them so that binary search can be applied.
obj = [
12234 :{
"ID" : "CV122H32",
"name" : "Дмитрий",
"position" : "Слесарь"
},
334534:{
"ID" : "SM12345",
"name" : "Антон",
"position" : "Електрик"
},
]
Answer the question
In order to leave comments, you need to log in
Any. The main thing is that the array should be sorted in ascending order according to the parameter by which the search will be carried out.
But, if there are a lot of objects (millions) - then this approach is not correct - it's better to have a database then.
The code you provided is not valid, you are trying to add a key to the array, there are no associative arrays in JS, but you can use objects as a hash table:
const data = {
12234: { ... },
334534: { ... }
};
const data = [
{ ... },
{ ... }
];
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question