javascript - 检查 JSON 字符串是否包含值并返回关联属性

标签 javascript arrays json

我有一个数组,其中每个条目都是一个汽车名称,并且我有一个 json 对象/字符串,其中包含相同的汽车名称,后跟颜色属性。

如何检查数组中的每辆车是否包含在 JSON 字符串中并依次返回与其关联的颜色?非常感谢您的帮助!

我的代码是这样的:

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

我的 JSON 文档如下所示:

{
"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)"}
            ]
        },

最佳答案

查看此FIDDLE对于工作示例

getCarColours() 中缺少一些括号,请参阅下面的修复:

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

此外,您的 createInputField() 引用的是 allcars 变量,而不是全局 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
}

关于javascript - 检查 JSON 字符串是否包含值并返回关联属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16696202/

相关文章:

javascript - Highcharts 不适用于 Angular 4

python - 从字符串中提取 float 以创建多个/多维数组来操作数据

python - 用其他数组给定的索引屏蔽一个数组

javascript - ajax 无效 json,为什么这是无效的 JSON?

javascript - "family"在 Assets 管道的上下文中意味着什么?

php - 如何从表格构建图表?

arrays - 如何从一维数组中创建一个二维未装箱数组?

javascript - 发布和从服务器获取

json - 使用不可打印的 ASCII 字符解码 JSON

c# - 为什么 WinRT 需要 DefaultOverloadAttribute?