0

I have an array and two inputs. I'd like to take the value of the first input and place that in variable. I have that part down. I'd then like to use that variable to find the corresponding array entry and return another object from that array.

The input is var prodNum=$('input#prodNum').val();

The array is:

var products = [
{
 "brand": "brand1",
 "prodNum": "01-005",
 "YN": "Yes",
 "Stock": "Order"
 },
{
"brand": "brand2",
"prodNum": "02-005",
"YN": "Yes",
"Stock": "Ship"
},
{
"brand": "brand1",
"prodNum": "01-008",
"YN": "No",
"Stock": "Order"
}
]

I'd like to return the "YN" value to var YNvalue, given a certain prodNum

For example if prodNum = 01-008 , YNvalue would be "No"

mcadamsjustin
  • 297
  • 4
  • 11
  • 23
  • What have you tried? This is almost the same as [Find a value in an array of objects in Javascript](http://stackoverflow.com/q/12462318/4642212). The next step is trivial. – Sebastian Simon Mar 23 '17 at 03:02

4 Answers4

2

You will have to loop over the objects in the product array until you find the one that matches:

function getProductYN(productList, productNumber) {
  for (var prodIdx = 0, prodCnt = productList.length; prodIdx < prodCnt; prodIdx++) {
    if (productList[prodIdx].prodNum === productNumber)
      return productList[prodIdx].YN;
  }
}

var products = [{
    "brand": "brand1",
    "prodNum": "01-005",
    "YN": "Yes",
    "Stock": "Order"
  },
  {
    "brand": "brand2",
    "prodNum": "02-005",
    "YN": "Yes",
    "Stock": "Ship"
  },
  {
    "brand": "brand1",
    "prodNum": "01-008",
    "YN": "No",
    "Stock": "Order"
  }
];

var prodNum = "01-008"; // $('input#prodNum').val();
var productYN = getProductYN(products, prodNum);

console.log(productYN);
Cᴏʀʏ
  • 105,112
  • 20
  • 162
  • 194
1

You could do that by using find on the products array

products.find(product => product.prodNum === prodNum).YN
Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
gabesoft
  • 1,228
  • 9
  • 6
1

You can use below code. But next time before asking give a try.

  var YNvalue;
    for (var index = 0; index < products.length; index++) {
        if (products[index].prodNum === prodNum ) {
            YNvalue = products[index].YN;
            break;
        }
    }
Sumit Gulati
  • 665
  • 4
  • 14
  • I did try, I've been trying for two days. Posting a question on here is always my last resort because I try to learn/figure it out on my own. I didn't want to put up the 30 or so combinations I've tried and didn't get the results I wanted. I appreciate your help, but don't assume people asking the question didn't try. – mcadamsjustin Mar 23 '17 at 03:06
  • @mcadamsjustin Please don't mind. I said as it's looking easy question. – Sumit Gulati Mar 23 '17 at 03:08
  • what's easy for you, isn't easy for everyone. Some of are still learning and arrays confuse me like nothing else – mcadamsjustin Mar 23 '17 at 03:09
0

If you want to find the YN value of a specific object using their YN number, in this example you could simply loop through each object until you find one that has a matching product number. once we find that object we should return its YN value

var YNvalue = "";
var products = [
{
 "brand": "brand1",
 "prodNum": "01-005",
 "YN": "Yes",
 "Stock": "Order"
 },
{
"brand": "brand2",
"prodNum": "02-005",
"YN": "Yes",
"Stock": "Ship"
},
{
"brand": "brand1",
"prodNum": "01-008",
"YN": "No",
"Stock": "Order"
}
]
function main() {
  var option = window.prompt("Product Number?", "01-008")
  YNvalue = getYNByProdNum(option)
  window.alert("Your YN value is simply " + YNvalue)
  console.log(YNvalue);
}

function getYNByProdNum(prodNum) {
  //loop through each product until we find on number that matches
  for(i in products) {
    //if product's number matches we return the YN value
    if(products[i].prodNum == prodNum) {
      return products[i].YN
    }
  }
}
<html>
<head>
</head>
<body onload="main()">
<div id="results"></div>
</body>
</html>

whoopsie, a little slow I am

CrazyInfin8
  • 113
  • 1
  • 9