-1

I have problem with parsing my JSON files to JavaScript object.

The result of this code in select tag are 3 "undefined" options. Any help?

const webtask = `{
  "luxvehicles": [
    {
      "brand": "Audi",
      "Model": "A6",
      "Year": 2015,
      "FuelType": "diesel",
      "NumOfSeats": 5,
      "Picture": "no",
      "PricePerDay": 50,
      "Count": 5
    },
    {
      "brand": "Mercedes",
      "Model": "S class",
      "Year": 2017,
      "FuelType": "diesel",
      "NumOfSeats": 5,
      "Picture": "no",
      "PricePerDay": 70,
      "Count": 3
    },
    {
      "brand": "BMW",
      "Model": "Series 7",
      "Year": 2020,
      "FuelType": "diesel",
      "NumOfSeats": 5,
      "Picture": "no",
      "PricePerDay": 100,
      "Count": 1
    }
  ]
}`;


let v = document.getElementById("vehicletype");
let x = document.getElementById("selectvozilo");
let vozila = JSON.parse(webtask);

if (v.value == "LUX") {

  for (i = 0; i < vehicles.luxvehicles.length; i++) {
    var opt = document.createElement('option');
    opt.value = vozila.luxvehicles.forEach(element => element.brand + " " + element.Model + " " + element.Year +
      " " + element.FuelType + " " + element.NumOfSeats + " " + element.PricePerDay + " Available cars: " + element.Count);

    opt.innerHTML = vozila.luxvehicles.forEach(element => element.brand + " " + element.Model + " " + element.Year +
      " " + element.FuelType + " " + element.NumOfSeats + " " + element.PricePerDay + " Available cars: " + element.Count);

    x.appendChild(opt);
  }
}
<input type="text" id="vehicletype" value="LUX" />
<select id="selectvozilo"></select>

6
  • You need to show your webtask string or vozila JSON Commented Feb 27, 2021 at 18:46
  • try if (webtask === undefined) { instead of if (webtask==null) { Commented Feb 27, 2021 at 18:50
  • @MisterJojo: wont if (webtask) do? Commented Feb 27, 2021 at 18:53
  • @naveen it could become a bad habit of codding, in other cases the tested variable can have a value returning false, while it does exist fine and well Commented Feb 27, 2021 at 18:58
  • Please visit help center, take tour to see what and How to Ask. Do some research, search for related topics on SO; if you get stuck, post a minimal reproducible example of your attempt, noting input and expected output, preferably in a Stacksnippet Commented Feb 27, 2021 at 19:00

2 Answers 2

1

Try this. You mix vehicles and vozila

I would personally not have such a long VALUE in my select, but here you are

const webtask = `{ "luxvehicles": [ { "brand": "Audi", "Model": "A6", "Year": 2015, "FuelType": "diesel", "NumOfSeats": 5, "Picture": "no", "PricePerDay": 50, "Count": 5 }, { "brand": "Mercedes", "Model": "S class", "Year": 2017, "FuelType": "diesel", "NumOfSeats": 5, "Picture": "no", "PricePerDay": 70, "Count": 3 }, { "brand": "BMW", "Model": "Series 7", "Year": 2020, "FuelType": "diesel", "NumOfSeats": 5, "Picture": "no", "PricePerDay": 100, "Count": 1 } ] }`;


let v = document.getElementById("vehicletype");
let x = document.getElementById("selectvozilo");
let vehicles = JSON.parse(webtask);

if (v.value == "LUX") {
  x.innerHTML = vehicles.luxvehicles.map(element => {
    val = `${element.brand} ${element.Model} ${element.Year} ${element.FuelType} ${element.NumOfSeats} ${element.PricePerDay} Available cars: ${element.Count}`
    return `<option value="${val}">${val}</option>`;
  }).join("")
}
<input type="text" id="vehicletype" value="LUX" />
<select id="selectvozilo"></select>

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

Comments

1

There is no definition of vehicles as I can tell.

So instead

vehicles.luxvehicles

use

webtask.luxvehicles

or define let vehicles of for loop

2 Comments

webTask is localStorage which returns DOMString. So it must be parsed.
for(i=0; i<vehicles.luxvehicles.length;i++) Used vehicles here not webtask as a for loop element

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.