多维数组与对象数组 [英] Multidimensional array vs array of objects

查看:156
本文介绍了多维数组与对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个运行基于代理的模型的房地产市场模拟,我想跟踪单个交易,以便我可以创建图表和图表以及数据统计。我想通过使用一种我将调用的对象来实现它交易

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
};

每次有交易时都会触发创建新的交易对象实例并被推送到 transArray

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));

这是为我陈述的目的积累和存储这些数据的最佳方式还是更好的方式使用二维数组,其中y将是日期,而x将是的房子,askPrice,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?

推荐答案

您可以使用一组对象,以便我可以使用它们来操作它们熟悉的动词功能;喜欢查找。可能有更多的纯粹Javascript方法可以做到这一点,但我还测试了查找的速度,这种格式也不错。

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

注意那个例如 hasDuplicates 函数 - 所以你可以很容易地在你的对象数组上支持它 - 如果你有多个对象数组,你可以重新使用每个函数。

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);
  }
};

这篇关于多维数组与对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆