Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/398.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 使用数组查询对象(无第三方脚本)_Javascript_Jquery_Arrays_Json - Fatal编程技术网

Javascript 使用数组查询对象(无第三方脚本)

Javascript 使用数组查询对象(无第三方脚本),javascript,jquery,arrays,json,Javascript,Jquery,Arrays,Json,如何使用数组查询对象 var myArray = ['A','C'] 使用myArray,我如何从以下内容(以前是JSON对象)中获得一个返回的带有['1','3']的数组(不使用第三方查询脚本——除非它是jQuery:) 您可以创建一个嵌套循环: var myArray = ['A','C']; var json = [{ 'property1': '1', 'property2': 'A' }, { 'property1': '2', 'proper

如何使用数组查询对象

var myArray = ['A','C']
使用myArray,我如何从以下内容(以前是JSON对象)中获得一个返回的带有['1','3']的数组(不使用第三方查询脚本——除非它是jQuery:)


您可以创建一个嵌套循环:

var myArray = ['A','C'];
var json = [{ 
    'property1': '1', 
    'property2': 'A'
}, { 
    'property1': '2', 
    'property2': 'B'
}, { 
    'property1': '3', 
    'property2': 'C'
}];

var result = new Array();
for (var i = 0; i < myArray.length; i++) {
    for (var j = 0; j < json.length; j++) {
        if (json[j].property2 === myArray[i]) {
            result.push(json[j].property1);
        }
    }
}

// at this point result will contain ['1', '3']
var myArray=['A','C'];
var json=[{
“property1”:“1”,
“property2”:“A”
}, { 
“property1”:“2”,
“property2”:“B”
}, { 
“property1”:“3”,
“property2”:“C”
}];
var result=新数组();
对于(var i=0;i
您可以创建一个嵌套循环:

var myArray = ['A','C'];
var json = [{ 
    'property1': '1', 
    'property2': 'A'
}, { 
    'property1': '2', 
    'property2': 'B'
}, { 
    'property1': '3', 
    'property2': 'C'
}];

var result = new Array();
for (var i = 0; i < myArray.length; i++) {
    for (var j = 0; j < json.length; j++) {
        if (json[j].property2 === myArray[i]) {
            result.push(json[j].property1);
        }
    }
}

// at this point result will contain ['1', '3']
var myArray=['A','C'];
var json=[{
“property1”:“1”,
“property2”:“A”
}, { 
“property1”:“2”,
“property2”:“B”
}, { 
“property1”:“3”,
“property2”:“C”
}];
var result=新数组();
对于(var i=0;i
您可以像这样漫游阵列:

var json = [ { "property1": "1", 
    "property2": "A"
   },
  { "property1": "2", 
    "property2": "B"
   },
  { "property1": "3", 
    "property2": "C"
   } ];

var myArray = ['A', 'C']; // Here is your request
var resultArray = []; // Here is the container for your result

// Walking the data with an old-fashioned for loop
// You can use some library forEach solution here.
for (var i = 0, len = json.length; i < len; i++) {
    var item = json[i]; // Caching the array element

    // Warning! Not all browsers implement indexOf, you may need to use a library
    // or find a way around
    if (myArray.indexOf(item.property2) !== -1) {
        resultArray.push(item.property1);
    }
}

console.log(resultArray);
var json=[{“property1”:“1”,
“物业2”:“A”
},
{“财产1”:“2”,
“物业2”:“B”
},
{“财产1”:“3”,
“财产2”:“C”
} ];
var myArray=['A','C'];//这是你的要求
var resultArray=[];//这是您的结果的容器
//使用老式的for循环遍历数据
//您可以在这里使用一些库forEach解决方案。
for(var i=0,len=json.length;i
您可以像这样漫游阵列:

var json = [ { "property1": "1", 
    "property2": "A"
   },
  { "property1": "2", 
    "property2": "B"
   },
  { "property1": "3", 
    "property2": "C"
   } ];

var myArray = ['A', 'C']; // Here is your request
var resultArray = []; // Here is the container for your result

// Walking the data with an old-fashioned for loop
// You can use some library forEach solution here.
for (var i = 0, len = json.length; i < len; i++) {
    var item = json[i]; // Caching the array element

    // Warning! Not all browsers implement indexOf, you may need to use a library
    // or find a way around
    if (myArray.indexOf(item.property2) !== -1) {
        resultArray.push(item.property1);
    }
}

console.log(resultArray);
var json=[{“property1”:“1”,
“物业2”:“A”
},
{“财产1”:“2”,
“物业2”:“B”
},
{“财产1”:“3”,
“财产2”:“C”
} ];
var myArray=['A','C'];//这是你的要求
var resultArray=[];//这是您的结果的容器
//使用老式的for循环遍历数据
//您可以在这里使用一些库forEach解决方案。
for(var i=0,len=json.length;i
Darin!!甜蜜的解决方案!谢谢你给我的“1”和“3”:达林!!甜蜜的解决方案!感谢我的“1”和“3”:D+1感谢Igor提供此解决方案!这似乎是一个很好的替代解决方案。它实际上不是一个替代方案:)除了
indexOf
部分之外,它是相同的。我真的很惊讶它离Emile的答案有多近,因为在回答这个问题之前我没有看到他的解决方案。+1感谢Igor的这个解决方案!这似乎是一个很好的替代解决方案。它实际上不是一个替代方案:)除了
indexOf
部分之外,它是相同的。我真的很惊讶这离埃米尔的答案有多近,因为在回答这个问题之前我没有看到他的答案。