I have a two dimensional array that I need to perform read and write action on from various angles. Sometimes I need to get/set a single row (easy of course) and sometimes I need to get/set a single column or even just a single cell but coming from a column perspective.
Let me try to explain what I want to achieve:
function myFunction() {
var myArray = [['a1','b1'],['a1','b1']];
var ds = new DataStructure(myArray);
console.log(ds) // -> [['a1','b1'],['a1','b1']]
console.log(da[0]) // -> ['a1','b1']
console.log(da[1]) // -> ['a2','b2']
console.log(da.cols[0]) // -> ['a1','a2']
console.log(da.cols[1]) // -> ['b1','b2']
da.cols[0] = ['A1','A2']
console.log(da.cols[0]) // -> ['A1','A2']
da.cols[1][1] = 'Q2'
console.log(da.cols[1]) // -> ['b1','Q2']
console.log(ds) // -> [['A1','b1'],['A1','Q1']]
console.log(myArray) // -> [['A1','b1'],['A1','Q1']]
}
So basically I want to have a wrapper to my original data structure and all get/set access to it should work directly on the original structure (by reference I reckon).
Any idea?
I have tried to use Proxy() and Object.defineProperty() to define custom getters/setters.
It kind of works but as soon as I access into a column i.e. ds.cols[0][0]
it doesn't work anymore.
da
is not even declared.I have tried
- show what you tried - perhaps it's an easy fixcols
as a property? Why not use methods,da.rows(1)
andda.cols(1)
? Then you don't need any fancy coding like getters or proxies.