Published
Edited
May 17, 2021
1 star
Insert cell
Insert cell
Insert cell
negativeArray = els =>
new Proxy(els, {
get: (target, propKey, receiver) =>
Reflect.get(
target,
+propKey < 0 ? String(target.length + +propKey) : propKey,
receiver
)
})
Insert cell
normal_arr = ["first", "middle1", "middle2", "last"]
Insert cell
// Negative indexing will not work with normal arrays
normal_arr[-1]
Insert cell
proxied_arr = negativeArray(normal_arr)
Insert cell
// But with our proxy we override the indexing to provide support for negative indices
proxied_arr[-1]
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more