Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multidimensional array vs array of objects

I'm building a real estate market simulation running an Agent Based Model and I want to keep track of individual transactions so that I can create graphs and charts and statistic with the data. I was thinking of doing it by using a type of object that I will call Transaction:

var Transaction = function(house, askingPrice, soldPrice, daysOnMarket) {
    this.date = gameDate
    this.house = house
    this.askingPrice = askingPrice
    this.soldPrice = soldPrice
    this.daysOnMarket = daysOnMarket
};

Every time there would be a transaction it would trigger the creation of a new Transaction object instance and be pushed to transArray:

transArray.push (new Transaction(house, askingPrice, soldPrice, daysOnMarket));

Is this the best way to accumulate and store this data for my stated purposes or would it be better to use a 2 dimensional array where y would be the date and x would be the house, askingPrice, soldPrice, daysOnMarket? What do you see as advantages and disadvantages for each method?

like image 524
snowfrogdev Avatar asked Aug 03 '16 23:08

snowfrogdev


People also ask

What is multidimensional array object?

A multidimensional array in MATLAB® is an array with more than two dimensions. In a matrix, the two dimensions are represented by rows and columns. Each element is defined by two subscripts, the row index and the column index.

Is nested array and multidimensional array same?

Multi dimensional arrays are just single dimension arrays with math done on the indexing. You can access any cell in O(1). Nested arrays have indirection. Accessing them is slower because of that indirection.

What is the difference between jagged array and multidimensional array?

In a multidimensional array, each element in each dimension has the same, fixed size as the other elements in that dimension. In a jagged array, which is an array of arrays, each inner array can be of a different size.


1 Answers

You might use an array of objects so I can manipulate them using familiar verb functions; like a lookup. There are likely more "pure" Javascript methods to do some of this but I also tested the lookupfor speed and it is decent in this form.

This is copied from another answer with a more complex example here: Binding an array of objects to their specific form fields for updating/deleting

Note on that one for instance the hasDuplicates function - so you could easily then support that upon your object array - and if you have more than one object array you can re-use the functions on each one.

var myApp = myApp || {};
myApp.arrayObj = {
  indexOf: function(myArray, searchTerm, property) {
    for (var i = 0; i < myArray.length; i++) {
      if (myArray[i][property] === searchTerm) return i;
    }
    return -1;
  },
  indexAllOf: function(myArray, searchTerm, property) {
    var ai = [];
    for (var i = 0; i < myArray.length; i++) {
      if (myArray[i][property] === searchTerm) ai.push(i);
    }
    return ai;
  },
  lookup: function(myArray, searchTerm, property, firstOnly) {
    var found = [];
    var i = myArray.length;
    while (i--) {
      if (myArray[i][property] === searchTerm) {
        found.push(myArray[i]);
        if (firstOnly) break; //if only the first 
      }
    }
    return found;
  },
  lookupAll: function(myArray, searchTerm, property) {
    return this.lookup(myArray, searchTerm, property, false);
  },
  remove: function(myArray, searchTerm, property, firstOnly) {
    for (var i = myArray.length - 1; i >= 0; i--) {
      if (myArray[i][property] === searchTerm) {
        myArray.splice(i, 1);
        if (firstOnly) break; //if only the first term has to be removed
      }
    }
  },
  removeByIndex: function(myArray, index) {
    myArray.splice(index, 1);
  }
};
like image 106
Mark Schultheiss Avatar answered Nov 09 '22 01:11

Mark Schultheiss