1

I have an object like so:

var products = {"products":[
    {"productID":"32652", "name":"Playstation 4", "price":"109.99"},
    {"productID":"24164", "name":"Xbox", "price":"129.99"}
]};

I need to search with the variable productID and find the associated name and price

My searches have just led me off track with things like .closest which is for the DOM not for an object.

How can I search the object for the productID var and find the associated data?

user1486133
  • 1,309
  • 3
  • 19
  • 37
  • 1
    Possible duplicate of [Find an object by property value in an array of JavaScript objects](http://stackoverflow.com/questions/7364150/find-an-object-by-property-value-in-an-array-of-javascript-objects) – Heretic Monkey May 16 '16 at 22:32

4 Answers4

1

You can use array.find

var foundProduct = products.products.find(x => x.productId === "12345")
if (foundProduct) { // foundProduct can be undefined. 
    var foundName = foundProduct.name
    var foundPrice = foundProduct.price
} else { /* handle not found */ }

If you are in a context that doesn't support Array.find yet, you can use .filter instead which is more commonly implemented.

var foundProduct = products.products.filter(x => x.productId === "12345")[0]
if (foundProduct) { // foundProduct can be undefined. 
    var foundName = foundProduct.name
    var foundPrice = foundProduct.price
} else { /* handle not found */ }
Paarth
  • 9,687
  • 4
  • 27
  • 36
1

You can use Array.prototype.filter()

var result = products.products.filter(function(obj) {
  return obj.name == "Xbox";
})[0];

Note that .filter will return an array. If you just need the first match then this will work for you. In case you need all the results then just remove the [0]

Sarantis Tofas
  • 5,097
  • 1
  • 23
  • 36
  • Hey, I'm a bit confused by this answer because you have hardcoded "Xbox" in place. I am trying to find out that data, so how can I have it hardcoded into the function? – user1486133 May 17 '16 at 09:44
  • @user1486133 the hardcoded "Xbox" can be any variable you want. It can be anything this is just an example of how it is done – Sarantis Tofas May 17 '16 at 12:27
0
var products = {"products":[
   {"productID":"32652", "name":"Playstation 4", "price":"109.99"},
   {"productID":"24164", "name":"Xbox", "price":"129.99"}
]};

function findProduct(product) { 
   return product.productID === '24164';
}

console.log(products.products.find(findProduct)); //{productID: "24164", name: "Xbox", price: "129.99"}
Samer Ayoub
  • 981
  • 9
  • 10
  • The find method executes the callback function (findProduct) once for each element present in the array until it finds one where callback returns a true value. If such an element is found, find immediately returns the value of that element. Otherwise, find returns undefined. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find – Samer Ayoub May 16 '16 at 23:22
0
console.log(products.products.find(function(p) {return p.productID === '24164'}));
Samer Ayoub
  • 981
  • 9
  • 10