1

I do have a javscript object containing a product name and the corresponding product code. The code is unique.

var products = {
"Pineapple":38,
"Apple":110,
"Pear":109
};

If i want to know the code for a product i can just do

var pineapplecode = products["Pineapple"];

How do I check if there is a product for a given number and if print it ? Or do I have to/should I change the way the data is saved ?

JHnet
  • 461
  • 6
  • 18
  • There is no key as `product` in given object.. – Rayon Jan 06 '16 at 17:31
  • 2
    You'd have to loop through all the properties looking for the code value. You could have *two* objects, one mapping from name to code and the other mapping from code to name. – Pointy Jan 06 '16 at 17:31

3 Answers3

1

I think the shortest way would be:

var prodCode = 38;
var result = Object.keys(products)
                   .filter(function(k) { return products[k] == prodCode; })[0];

// result == "Pineapple"

See MDN

haim770
  • 48,394
  • 7
  • 105
  • 133
  • product code should be unique, i assume. – Nina Scholz Jan 06 '16 at 17:40
  • @NinaScholz, I assume so as well. That's why I'm only returning `[0]`, assuming it's the only one that found. – haim770 Jan 06 '16 at 17:41
  • but then no need to iterate over all elements. – Nina Scholz Jan 06 '16 at 17:46
  • @NinaScholz, I guess you're right. That's the downside of using `filter()` (although negligible for most cases). There is the new (ES6) `find()` method that would do just that - find the first one that matches and return immediately (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find). – haim770 Jan 06 '16 at 17:50
  • @haim770 That answer is also in the thread I marked as a duplicate: https://stackoverflow.com/questions/9907419/javascript-object-get-key-by-value – Jon Koops Jan 06 '16 at 21:18
0

I guess you are basically asking if I can check the other way round, i.e. check for key by passing value.

Yes you can,

  • Either by iterating through all the key and values, check if the value is existing and finally return the key if the value exists

  • change the way you save the data, index it by product number rather than product name

here is the code to convert one form to another

var newProducts = {};
for( var prodName in products )
{
  newProducts[ products[ prodName ] ] = prodName;
}
console.log( newProducts );

And here is the code to check a specific value

var valueToCheck = "110";
var prodNameToFind = "";
for( var prodName in products )
{
  if ( products[ prodName ] == valueToCheck )
  {
      prodNameToFind = prodName;  
  }
}
console.log( prodNameToFind );
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
0

Just iterate over the keys and the properties values and break the iteration if found with Array.prototype.some()

The some() method tests whether some element in the array passes the test implemented by the provided function.

var products = {
        Pineapple: 38,
        Apple: 110,
        Pear: 109
    };

function getProduct(code) {
    var product;
    Object.keys(products).some(function (k) {
        if (products[k] === code) {
            product = k;
            return true;
        }          
    });
    return product;
}
document.write(getProduct(109));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392