【问题标题】:Arrange Javascript Arrays排列 Javascript 数组
【发布时间】:2014-02-08 22:24:13
【问题描述】:

我有汽车数组和每辆车对应的价格数组。

我想实现函数highest3,它将按价格(从最高到最低)返回一个包含3辆汽车的数组。

但是,如果价格相同,则按字母顺序返回汽车。

在下面的示例中,“Hummer”将是第一个条目。

代码

var cars = ["Ferrari", "Lamborghini", "Jaguar", "Hummer", "Toyota"];
var price = [12, 34.5, 3.54, 45.9, 3.44];

result == ["Hummer", "Lamborghini", "Ferrari"];

function highest3 (cars, price) {

//Please help me here.

}

有人可以帮我实现highest3函数吗?我是新手。谢谢。

【问题讨论】:

标签: javascript


【解决方案1】:

这里有一个解决方案。 首先,它会创建一个 arrayobjects 代表汽车及其相关值 (joinCarPrices)。

然后我们执行排序,使用自定义排序函数priceSort 通过使用Array#sort 函数。 根据您要求的算法对汽车进行分类。 最后,我们使用Array#slice 来仅列出价格最高的 3 辆汽车。

var cars = ["Ferrari", "Lamborghini", "Jaguar", "Hummer", "Toyota"],
price = [12, 34.5, 3.54, 45.9, 3.44],
result,
joinCarPrices = function () {
    var index = 0,
        carPrices = [];

    for (index = 0; index < cars.length; index++) {
        carPrices[index] = {
            'car': cars[index],
            'price': price[index]
        };
    }

    return carPrices;
},
priceSort = function (a, b) {
    // If the first car is less than the second car
    if (a.price < b.price) {
        return 1;
    } else if (a.price > b.price) {
        // If the first car is more than the second car
        return -1
    } else {
        // Else sort by the car name
        return a.car < b.car ? -1 : 1;
    }
};

cars = joinCarPrices(); // Join the Cars/Prices together as Objects, into an array
result = cars.sort(priceSort); // Sort the Cars based on the Price Sort function
result = result.slice(0, 3); // Slice to only give us array items 0-3
console.log(result);

这里有一个JSFiddle 表明它工作正常!

【讨论】:

    【解决方案2】:

    这只是一种可能的解决方案:

    //I assume you get the arrays correctly linked i.e price[0] is the 
    //price of cars[0]
    var cars = ["Ferrari", "Lamborghini", "Jaguar", "Hummer", "Toyota"];
    var price = [12, 34.5, 3.54, 45.9, 3.44];
    
    result == ["Hummer", "Lamborghini", "Ferrari"];
    
    function highest3 (cars, price) {
    
    //First we unite the two arrays 
    carsAndPrice = [];
    for (var i = 0; i < cars.length; i = i +1)
        carsAndPrice[i] = {car:cars[i],price:price[i]};
    }
    //Now that the price and the cars are strongly linked we sort the new array
    //I'm using bubble sort to sort them descending this seems to be more of 
    //a beginner question
    var swapped;
        do {
            swapped = false;
            for (var j=0; j < carsAndPrice.length-1; i++) {
                if (carsAndPrice[i].price < carsAndPrice[i+1].price) {
                    var temp = carsAndPrice[i];
                    carsAndPrice[i] = a[i+1];
                    carsAndPrice[i+1] = temp;
                    swapped = true;
                }
            }
        } while (swapped);
      //return the name of the first 3 cars from the array
      var result = [];
      result[0] = carsAndPrice[0].price; //Most expensive
      result[1] = carsAndPrice[1].price;
      result[2] = carsAndPrice[2].price;
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-09-23
      • 2019-05-02
      • 1970-01-01
      • 2016-10-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-08
      相关资源
      最近更新 更多