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"

1

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

1 Comment

thank you for the elegant answer, no I just need to break it all down and understand. Much obliged
1

You could do that by using find on the products array

products.find(product => product.prodNum === prodNum).YN

Comments

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

3 Comments

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 Please don't mind. I said as it's looking easy question.
what's easy for you, isn't easy for everyone. Some of are still learning and arrays confuse me like nothing else
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.