Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/478.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript/jQuery查找变量和返回值部分匹配的JSON键/值_Javascript_Jquery_Json - Fatal编程技术网

JavaScript/jQuery查找变量和返回值部分匹配的JSON键/值

JavaScript/jQuery查找变量和返回值部分匹配的JSON键/值,javascript,jquery,json,Javascript,Jquery,Json,我有一个模型列表,我想通过搜索和拉正确的一个网址。我不会总是拥有完整的密钥,也不会拥有完整的值,但至少会拥有它的唯一部分 现在代码正处于测试模式,设置了一个与键匹配的数字,打印成功或失败 控制台一直告诉我models[i].indexOf不是一个函数。我知道它是一个物体,但当我在它上做一个旋转时,我得到了“物体”。我不明白什么 我对一个解决方案很满意,它要么是普通的JavaScript,要么是使用jQuery 守则: if ($('.mobile_tutorial').length) {

我有一个模型列表,我想通过搜索和拉正确的一个网址。我不会总是拥有完整的密钥,也不会拥有完整的值,但至少会拥有它的唯一部分

现在代码正处于测试模式,设置了一个与键匹配的数字,打印成功或失败

控制台一直告诉我models[i].indexOf不是一个函数。我知道它是一个物体,但当我在它上做一个旋转时,我得到了“物体”。我不明白什么

我对一个解决方案很满意,它要么是普通的JavaScript,要么是使用jQuery

守则:

if ($('.mobile_tutorial').length) {
    var device = /*$device.model*/ "NTZEZ717VLU", model_code = device.substr(2).substr(0,device.length-3);
    $.ajax({
        url: "/scripts/phone_models.json",
        type: 'get',
        dataType: 'json',
        success: function (data) {
            var models = data.Manufacturer;
            for (var i = models.length - 1; i >= 0; i--) {
                if (models[i].indexOf(model_code) > -1) {
                    console.log(models[i])
                } else {
                    console.log('no match')
                }
            }
        }
    });
}
JSON(部分):


您需要获取键的值,将
models[i].indexOf(model\u code)
更改为
Object.keys(models[i])[0].indexOf(partial\u model\u code)
。以下是实际行动:

var部分模型代码='3VL'
函数ajax(数据){
var模型=数据。制造商;
对于(var i=models.length-1;i>=0;i--){
//抓住对象中的关键点
//因为只有一个对象,所以抓取第一个对象
//检查密钥是否部分匹配
if(Object.keys(models[i])[0].indexOf(partial\u model\u code)>-1){
console.log(型号[i])
}否则{
console.log('不匹配')
}
}
}
var data=JSON.parse(`{
“制造商”:[{
“ZEZ955L”:”http://x.com/mobile/home.seam?custId=ZEZ955L"
}, {
“ZEZ990G”:http://x.com/mobile/home.seam?custId=ZEZ990G"
}, {
“ZEZ828TL”:”http://x.com/mobile/home.seam?custId=ZEZ828TL"
}, {
“ZEZ716BL”:”http://x.com/mobile/home.seam?custId=ZEZ716BL"
}, {
“ZEZ717VL”:”http://x.com/mobile/home.seam?custId=ZEZ717VL"
}, {
“ZEZ962BL”:http://x.com/mobile/home.seam?custId=ZEZ962BL"
}, {
“ZEZ963VL”:http://x.com/mobile/home.seam?custId=ZEZ963VL"
}]
}`)

ajax(data)
您需要获取键的值,将
模型[i].indexOf(model\u code)
更改为
对象.keys(models[i])[0].indexOf(partial\u model\u code)
。以下是实际行动:

var部分模型代码='3VL'
函数ajax(数据){
var模型=数据。制造商;
对于(var i=models.length-1;i>=0;i--){
//抓住对象中的关键点
//因为只有一个对象,所以抓取第一个对象
//检查密钥是否部分匹配
if(Object.keys(models[i])[0].indexOf(partial\u model\u code)>-1){
console.log(型号[i])
}否则{
console.log('不匹配')
}
}
}
var data=JSON.parse(`{
“制造商”:[{
“ZEZ955L”:”http://x.com/mobile/home.seam?custId=ZEZ955L"
}, {
“ZEZ990G”:http://x.com/mobile/home.seam?custId=ZEZ990G"
}, {
“ZEZ828TL”:”http://x.com/mobile/home.seam?custId=ZEZ828TL"
}, {
“ZEZ716BL”:”http://x.com/mobile/home.seam?custId=ZEZ716BL"
}, {
“ZEZ717VL”:”http://x.com/mobile/home.seam?custId=ZEZ717VL"
}, {
“ZEZ962BL”:http://x.com/mobile/home.seam?custId=ZEZ962BL"
}, {
“ZEZ963VL”:http://x.com/mobile/home.seam?custId=ZEZ963VL"
}]
}`)

ajax(数据)
模型[i]不是字符串,所以您会遇到错误。如果要检查键,请在模型[i]上使用.each()函数。在这种情况下,每个循环使用indexOf函数比较键

if ($('.mobile_tutorial').length) {
var device = /*$device.model*/ "NTZEZ717VLU", model_code = device.substr(2).substr(0,device.length-3);
    $.ajax({
    url: "/scripts/phone_models.json",
    type: 'get',
    dataType: 'json',
    success: function (data) {
        var models = data.Manufacturer;
        for (var i = models.length - 1; i >= 0; i--) {


 $.each(models[i], function( key, value ) {
  if (key.indexOf(model_code) > -1)               {
                           console.log(models[i])
            } else {
                console.log('no match')
            }
        }
    }});               

});
}

模型[i]不是字符串,因此您会遇到错误。如果要检查键,请在模型[i]上使用.each()函数。在这种情况下,每个循环使用indexOf函数比较键

if ($('.mobile_tutorial').length) {
var device = /*$device.model*/ "NTZEZ717VLU", model_code = device.substr(2).substr(0,device.length-3);
    $.ajax({
    url: "/scripts/phone_models.json",
    type: 'get',
    dataType: 'json',
    success: function (data) {
        var models = data.Manufacturer;
        for (var i = models.length - 1; i >= 0; i--) {


 $.each(models[i], function( key, value ) {
  if (key.indexOf(model_code) > -1)               {
                           console.log(models[i])
            } else {
                console.log('no match')
            }
        }
    }});               

});
}

我原来误解了你的问题,请看我更新的答案!我原来误解了你的问题,请看我更新的答案!