cal
cal

Reputation: 47

Loop through JS object to return matching result of value

I have an object, full of salmon. I want to loop through and check whether salmon is fresh or frozen. If frozen, put in no pile, if fresh, put in yes pile. If ounces is less than 2 and more than 6, no pile, and if it matches 2-6, yes pile.

Then, with the yes pile, I want to loop through those keys and values to find the cheapest one, and print that out.

Here is what I have so far, but I seem to be stuck on looping through the big object in order to run the checks.

How do I do this?

function findSalmon(salmonInStore){
  var noPile, 
      yesPile; 
      var salmon = Object.keys(salmonInStore));
  for(var key in salmonInStore){

    if(salmonInStore.key.freshness !== 'fresh'){
      noPile.push(salmonInStore.key);
    } else {
      yesPile.push(salmonInStore.key);
    }
    if(salmonInStore.key.ounces < 2 && salmonInStore.key.ounces > 6){
      noPile.push(key);
    }else {
      yesPile.push(salmonInStore.key);
    }
  }
  return yesPile;
}

var salmonInStore = {
  salmon1 : {
    ounces: 1,
    freshness: 'frozen',
    price: 16
  },

 salmon2 : {
    ounces: 6,
    freshness: 'fresh',
    price: 10
  },

  salmon3 : {
    ounces: 2,
    freshness: 'frozen',
    price: 20
  },

  salmon4 : {
    ounces: 5,
    freshness: 'fresh',
    price: 1
  },

  salmon5 : {
    ounces: 3,
    freshness: 'frozen',
    price: 12
  }
}

findSalmon(salmonInStore);

Upvotes: 1

Views: 2722

Answers (3)

nicholaswmin
nicholaswmin

Reputation: 22949

I seem to be stuck on looping through the big object in order to run the checks.

Objects are also known as key/value pairs.

You can use Object.keys to get the keys of an object. From then on you need nested loops and dynamic value access using bracket notation to get the value of a k/v pair based on the key you got when you used Object.keys.

Here's an example:

var salmonInStore = {
  salmon1 : {
    ounces: 1,
    freshness: 'frozen',
    price: 16
  },

 salmon2 : {
    ounces: 6,
    freshness: 'fresh',
    price: 10
  }
}

Object.keys(salmonInStore).forEach(storeKey => {
  var salmon = salmonInStore[storeKey]
  // put per salmon logic here...

  Object.keys(salmon).forEach(salmonKey => {
    var value = salmon[salmonKey] // value for each key  
    console.log(value)
    // put per salmon value logic here...
  })
})

However if you plan on having arbitrary nesting levels - not just 1 level, you would need a recursive function to walk your salmons.

The above can be made more 'functional' by using reduce instead of forEach but that's another story.

The gist of the above is that you need Object.keys and someObj[key]. I've substituted your for.. loops with forEach just for brevity.

Upvotes: 1

justin
justin

Reputation: 439

First we can sort the salmon list by price.

const salmonList = Object.keys(salmonInStore)
  .map(key => salmonInStore[key])
  .sort((a,b) => {
    if (a.price < b.price)
      return -1;
    if (a.price > b.price)
      return 1;

    return 0;
});

Then we can filter out the yes fish.

const yesPile = salmonList.filter((salmon) => {
 return salmon.ounces >= 2 && salmon.ounces <= 6 && salmon.freshness === 'fresh';
});

yesPile[0] // cheapest fish

Check out the fiddle for a working example.

https://jsfiddle.net/y1jg01wy/

Upvotes: 2

Isaac
Isaac

Reputation: 11805

function findSalmon(salmonInStore){
var yesPile = [];
var noPile = [];
Object.keys(salmonInStore).forEach( (key)=> {
  var salmon = salmonInStore[key]

  if (salmon.freshness !== 'fresh' || salmon.ounces < 2 && salmon.ounces > 6){
    noPile.push(key);
  } else {
    yesPile.push(key);
  }
})
return yesPile;
}

Upvotes: 2

Related Questions