6

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?

6
  • It's up to you. I would personally use an array of objects, but it's not necessarily wrong to use a 2d array. Commented Aug 3, 2016 at 23:32
  • @SpencerWieczorek What do you see as advantages and disadvantages for each method? Commented Aug 4, 2016 at 0:10
  • Whichever is more convenient to you as the developer. Commented Aug 4, 2016 at 0:12
  • @SpencerWieczorek indeed, while the practical usage is the same, the actual implementation behind both approaches varies. I think OP could potentially want to investigate the implications of memory usage, time complexity, and a whole variety of other concerns. Commented Aug 4, 2016 at 0:14
  • As it is, this will fail with gameDate not being defined - and I would not use a global in this case either. Commented Aug 4, 2016 at 1:12

1 Answer 1

4

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);
  }
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.