Hi !
Given the following sample items :
ID | First name | Age |
---|---|---|
xvZwiCpi |
Naomi | 42 |
Nzd9UsGT |
Naomi | 24 |
QiDXP2wA |
Thea | 53 |
JpYeAY7H |
Jeremy | 35 |
I can store these in an array :
const data = [
{ id: 'xvZwiCpi', firstName: 'Frederic', age: 42 },
{ id: 'Nzd9UsGT', firstName: 'Naomi', age: 24 },
{ id: 'QiDXP2wA', firstName: 'Thea', age: 53 },
{ id: 'JpYeAY7H', firstName: 'Mathew', age: 35 }
];
Thus access them the same way by ID :
console.log(data.find(item => item.id === 'xvZwiCpi'));
And by properties :
console.log(data.find(item => item.firstName === 'Frederic').id);
Or I can store these in an object :
const data = {
'xvZwiCpi': { firstName: 'Frederic', age: 42 },
'Nzd9UsGT': { firstName: 'Naomi', age: 24 },
'QiDXP2wA': { firstName: 'Thea', age: 53 },
'JpYeAY7H': { firstName: 'Mathew', age: 35 }
};
Thus more easily access properties by ID :
console.log(data['xvZwiCpi'].firstName);
But more hardly access ID by properties :
console.log(Object.entries(data).find(([id, item]) => item.firstName = 'Frederic')[0]);
I could duplicate IDs :
const data = {
'xvZwiCpi': { id: 'xvZwiCpi', firstName: 'Frederic', age: 42 },
'Nzd9UsGT': { id: 'Nzd9UsGT', firstName: 'Naomi', age: 24 },
'QiDXP2wA': { id: 'QiDXP2wA', firstName: 'Thea', age: 53 },
'JpYeAY7H': { id: 'JpYeAY7H', firstName: 'Mathew', age: 35 }
};
To slightly simplify that previous line :
console.log(Object.values(data).find(item => item.firstName = 'Frederic').id);
But what if a single variable type could allow doing both operations easily ?
console.log(data['xvZwiCpi'].firstName);
console.log(data.find(item => item.firstName === 'Frederic').id);
Does that exist ?
If not, I’m thinking about implementing it that way :
const data = new Proxy([
{ id: 'xvZwiCpi', firstName: 'Frederic', age: 42 },
{ id: 'Nzd9UsGT', firstName: 'Naomi', age: 24 },
{ id: 'QiDXP2wA', firstName: 'Thea', age: 53 },
{ id: 'JpYeAY7H', firstName: 'Mathew', age: 35 }
], {
get: (array, property) =>
array[property]
||
array.find(item => item.id === property)
});
In which case I’d put it in a lib, but how would this be named ?
I’d also make a second implementation that would enforce ID uniqueness and use Map
to map IDs with indexes instead of running find
: while the first implementation would be fine for static data, the second one would be more suitable for dynamic data.
Would this make sense ?
Thanks
That’s a name, thanks !
Actually, my implementation idea relying on
Proxy
don’t have this issue.As for other implementations, they do have this issue, but as they say, hybrid array use cases don’t generally involve the need to stringify, and even when they do, using
Object.defineProperty
with{ enumerable: false }
(or replacing thetoJSON
method) would fix it.Actually, it would still be an
Array
, so no, there shouldn’t be any problems, and yes, those methods definitely work, which is precisely what said I want to achieve.If your IDs are integers then there is no need for an hybrid at all, precisely because all you have to do is put each item at the same position as their ID.
I’ll definitely run benchmarks so that users would be aware of performance losses, if any. But use cases of hybrid arrays are mostly small datasets so it usually shouldn’t be a concern indeed.
If I implement this, it will be as a documented and commented package, so it shouldn’t be that much of a problem.
I think I misunderstood your initial post (and definitely didn’t read it as carefully as I should have 😅).
Do I understand your correctly that your goal is a companion object for your arrays that simplifies access? Not a new data structure that you’d user instead of arrays? If so, most of my points are moot.
If you don’t treat IDs as opaque values, this is true.
I think my point is actually wrong (it was really late when I was writing my initial response). Performance would be O(n), since that’s the worst case scenario.
Anyways, I hope you could take something useful from my answer.
Happy hacking :D