0

I am using JSON data from Edmunds car API. Here is a simplified version of the returned data:

[[
 {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}
 ]
]

In this example there are 2 inner arrays that represent the different options of drive train available for this car model (front and all wheel).

I am trying to find the lowest price for each respective drive train and push the object into a new array. In my example I would like the final result to be..

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

I have been trying to solve this for a while now and can't figure it out. Here is what I have so far.

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

If there is 1 inner array. I can get it to work. The problem is when there are 2,3 or 4 inner arrays (representing drive trains). I would like to write a function that can handle any number of drive trains that the API returns. I actually understand why my solution doesn't work. The problem is that I'm new and I've hit a brick wall of understanding. The solution is slightly out of my grasp.

Here are 2 similiar questions that were helpful but they deal with 1 inner array and I still can't figure it out. Any help would be appreciated! Here and Here

Community
  • 1
  • 1
swollavg
  • 911
  • 1
  • 6
  • 11

2 Answers2

1

It's easier using 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();
})
Furqan Zafar
  • 1,471
  • 1
  • 13
  • 17
1

Assumes arr is your JSON

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

I'm a huge fan of one-liners

Result (Copy pasted from console)

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

More strait-forward

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;
        }
    }
}
Community
  • 1
  • 1
Downgoat
  • 13,771
  • 5
  • 46
  • 69
  • Anyway you could briefly explain in general terms what you are doing with this function? I'm trying to wrap my head around it. – swollavg Apr 16 '15 at 01:57
  • 1
    @user3176148 So basically, we loop through the array, then we go through the next array, if there is no object in the result object, we add it, we also add it if the price is greater. It's pretty simple, I can write a non-one liner if that would help – Downgoat Apr 16 '15 at 02:32
  • If you would like to that would be very helpful! – swollavg Apr 16 '15 at 03:41