【问题标题】:Javascript Array containing multiple arrays which contain multiple objects包含多个数组的Javascript数组,其中包含多个对象
【发布时间】:2015-04-15 02:01:05
【问题描述】:

我正在使用来自 Edmunds 汽车 API 的 JSON 数据。这是返回数据的简化版本:

[[
 {drivenWheels: "front wheel drive", price: 32442},
 {drivenWheels: "front wheel drive", price: 42492},
 {drivenWheels: "front wheel drive", price: 38652},
 {drivenWheels: "front wheel drive", price: 52402}
 ],
 [{drivenWheels: "all wheel drive", price: 29902},
  {drivenWheels: "all wheel drive", price: 34566},
  {drivenWheels: "all wheel drive", price: 33451},
  {drivenWheels: "all wheel drive", price: 50876}
 ]
]

在此示例中,有 2 个内部数组表示该车型可用的不同传动系统选项(前轮和全轮)。

我正在尝试为每个相应的传动系统找到最低价格,并将对象推入一个新阵列。在我的示例中,我希望最终结果是..

var finalArr = [{drivenWheels: "front wheel drive", price: 32442},{drivenWheels: "all wheel drive", price: 29902}]

我一直在尝试解决这个问题一段时间,但无法弄清楚。这是我目前所拥有的。

function findLowestPriceDrivenWheels(arr){
    var wheelAndPriceArr = [];
    var shortest = Number.MAX_VALUE;
    for (var i = 0; i < arr.length; i++){
        //loops inner array
        for (var j = 0; j < arr[i].length; j++){
            if(arr[i][j].price < shortest){
                shortest = arr[i][j].price;
                wheelAndPriceArr.length = 0;
                wheelAndPriceArr.push(arr[i][j]);
            }
        }  
    }
    console.log(wheelAndPriceArr);
    return wheelAndPriceArr;
}

如果有 1 个内部数组。我可以让它工作。问题是当有 2,3 或 4 个内部阵列(代表传动系统)时。我想编写一个函数来处理 API 返回的任意数量的传动系统。 我实际上理解为什么我的解决方案不起作用。问题是我是新手,而且我在理解上遇到了障碍。解决方案有点超出我的掌握。

这里有 2 个类似的问题很有帮助,但它们涉及 1 个内部数组,我仍然无法弄清楚。任何帮助,将不胜感激! HereHere

【问题讨论】:

    标签: javascript jquery arrays object nested-loops


    【解决方案1】:

    假设 arr 是您的 JSON

    var results = [];
    arr.forEach(function(a,i){a.forEach(function(b){(!results[i]||b['price'] < results[i]['price'])&&(results[i]=b);});});
    

    我是单线的忠实粉丝

    结果 (从控制台复制粘贴)

    [
        {
            "drivenWheels":"front wheel drive",
            "price":32442
        },
        {
            "drivenWheels":"all wheel drive",
            "price":29902
        }
    ]
    

    更加直截了当

    var results = [],
        i;
    
    for (i = 0; i < arr.length; i += 1) {
        var a = arr[i],
            j;
    
        for (j = 0; j < a.length; j += 1) {
            var b = a[j];
            //  !results[i] will check if there is a car for the current drivenWheels. It will return true if there isn't
            if (!results[i] || b['price'] < results[i]['price']) {
                results[i] = b;
            }
        }
    }
    

    【讨论】:

    • 不管怎样,你可以简要地解释一下你在用这个函数做什么?我正试图绕过它。
    • @user3176148 所以基本上,我们循环遍历数组,然后遍历下一个数组,如果结果对象中没有对象,我们添加它,如果价格更大,我们也添加它.这很简单,如果有帮助的话,我可以写一个非一个班轮
    • 如果您愿意,那将非常有帮助!
    • @user3176148 添加:)
    【解决方案2】:

    使用 underscore.js (http://underscorejs.org/) 更容易

    arr = [
     [
      {drivenWheels: "front wheel drive", price: 32442},
      {drivenWheels: "front wheel drive", price: 42492},
      {drivenWheels: "front wheel drive", price: 38652},
      {drivenWheels: "front wheel drive", price: 52402}
     ],
     [
      {drivenWheels: "all wheel drive", price: 29902},
      {drivenWheels: "all wheel drive", price: 34566},
      {drivenWheels: "all wheel drive", price: 33451},
      {drivenWheels: "all wheel drive", price: 50876}
     ]
    ]
    
    _.map(arr, function(items){
     return _.chain(items)
      .sortBy(function(item){
        return item.price
      })
      .first()
      .value();
    })
    

    【讨论】:

      猜你喜欢
      • 2022-01-12
      • 1970-01-01
      • 2023-02-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多