繁体   English   中英

包含多个包含多个对象的数组的Javascript数组

[英]Javascript Array containing multiple arrays which contain multiple objects

我正在使用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个内部数组,但我仍然无法弄清楚。 任何帮助,将不胜感激! 这里这里

使用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();
})

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM