0

I have an array where every entry is a car name and I have a json object/string that contains those same car names followed by colour properties.

How can I check for each car in the array whether it is contained in the JSON string and return the colours associated with it one after another? Thanks very much for any help!

My code is like this:

var allCars;
var inputCount = 1;

$(document).ready(function() {
    $.getJSON('cars.json', function(data) {
    console.log (data);
    allCars = data;

    createInputField();

    $('form').submit(function(){
        getCarColours()
    });

    })
}
);

function createInputField()
{           
$('.button').before('<select id="cars'+ inputCount +'" name="cars'+ inputCount +'" class="cars"></select> - <select id="percentage'+ inputCount +'" name="percentage'+ inputCount +'" class="percentage"></select><br />');             

$('.cars').append('<option value="'+ 0 +'">Please select a country </option>');
for (var i = 0; i < allcars.cars.length; i++) {
    $('.cars').append('<option value="'+ allcars.cars[i].name +'">'+ allcars.cars[i].name +'</option>');            
}

inputCount += 1
}


function getCarColours(){

var carsSelected = [];

for( var y = 1; y < inputCount; y++){
    carsSelected.push($('select[name="cars'+ y +'"]').val());
}

for (var i = 0; i < allcars.cars.length; i++) {
        if allcars.cars.has(carsSelected[i]{
            console.log(carsSelected[i]);
        }
}   
}

My JSON document looks like this:

{
"cars" : 
    [
        {
            "name": "Merc",
            "colour" : [
                {"name":"green", "percent":35, "rgb":"rgb(0,153,0)"}, 
                {"name":"red", "percent":31, "rgb":"rgb(191,0,0)"}, 
                {"name":"black", "percent":32, "rgb":"rgb(0,0,0)"},
                {"name":"white", "percent":2, "rgb":"rgb(255,255,255)"}
            ]
        },
        {
            "name": "BMW",
            "colour" : [
                {"name":"red", "percent":90, "rgb":"rgb(216,17,38)"}, 
                {"name":"black", "percent":10, "rgb":"rgb(0,0,0)"}
            ]
        },

2 Answers 2

3

See this FIDDLE for working example

Some missing parenthesis in getCarColours() see fix below:

function getCarColours(){
    var carsSelected = [];
    for( var y = 1; y < inputCount; y++){
        carsSelected.push($('select[name="cars'+ y +'"]').val());
    }

    for (var i = 0; i < cars.length; i++) {
            if (allCars.cars.has(carsSelected[i])){
                console.log(carsSelected[i]);
            }
    }
}

also your createInputField() is referring to the allcars variable instead of the global alLCars.

function createInputField()
{           
    $('.button').before('<select id="cars'+ inputCount +'" name="cars'+ inputCount +'" class="cars"></select> - <select id="percentage'+ inputCount +'" name="percentage'+ inputCount +'" class="percentage"></select><br />');             

    $('.cars').append('<option value="'+ 0 +'">Please select a country </option>');
    for (var i = 0; i < allCars.cars.length; i++) {
        $('.cars').append('<option value="'+ allCars.cars[i].name +'">'+ allCars.cars[i].name +'</option>');            
    }

    inputCount += 1
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks - hadn't noticed the missing parenthesis - the allCars variable is global so I don't think that's stopping anything working - any suggestion on how I can check if each car is in allCars.cars? Apparently the 'has' method doesn't exist for my object.
@db579 Ah yes I see. Do notice though that in your createInputField() you are referring to allcars with a lowercase 'c' instead of the global variable allCars. The same is true for getCarColours. Javascript is case sensitive.
I have updated the fiddle with some changes. Do you also want that percentage select menu populated from the json?
Thanks - I think I've actually got user814628's answer to work - just need some regex for the strings I think
1

Something like so

$.each(carsSelected, function(index,carName){
   var availableColors = [];
   for(var i = 0; i < cars.length; ++i){ 
      if(cars[i].name == carName){
         availableColors = cars[i].colour;
         break;
      }
   }
  //at this point availableColors will either be empty if no match is found or have the associated color
});

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.