Search Object Array value

Asked

Viewed 39 times

1

I have the following code:

jsonOptions = [{"description": "Carro 1", "product": "4"},
                {"description": "Carro 2", "product": "5"},
                {"description": "Carro 3", "product": "6"},
                {"description": "Carro 4", "product": "7"},
                {"description": "Carro 5", "product": "8"}

              ];

I use it to fill a <select> using the value of description to build the <option>

jsonOptions.forEach(function(item) {

    var option = document.createElement('option');
    option.text = item.description;
    dataList.appendChild(option);
});

What I’m not able to do is: When I select one of the options, it sends the value that was captured, so in this case, if I select the first value it returns the text "Car 1".. Show!

What I needed was type, if the selected value was Car 1, it would take the second value and launch into a variable. It would be like:

Send car 1 to a variable Then fetch the second value "4" and play to another variable

  • The tags I wrote did not come out. "Fill in a <select>" and "Build the <option>"

  • When you need me, edit the question Alexandre.

1 answer

1


Attribute the value of option:

jsonOptions.forEach(function(item) {

    var option = document.createElement('option');
    option.text = item.description;
    option.value = item.product;
    dataList.appendChild(option);
});
  • 1

    And then I look for the value like this: var str = e.options[e. selectedIndex]. value; Caraca, I don’t know if I’m happy or angry.. Thanks a lot man.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.