简体   繁体   English

JavaScript数组搜索返回2元素搜索的索引位置

[英]JavaScript array search to return index position for 2 element search

I need to search a JSON set for 2 elements of an array. 我需要在JSON集中搜索数组的2个元素。 If a match is found for these 2 elements, I need the index of this position. 如果找到这两个元素的匹配项,则需要该位置的索引。 And once I have this index, I can use this to access a different property in this same array position. 一旦有了该索引,就可以使用它在同一数组位置访问另一个属性。

Below is sample of what I have. 以下是我所拥有的示例。 If you look at line 22, I am hard-coding the array position to 1. This is the value I dynamically need depending on what I pass into my getModelID() function. 如果您看一下第22行,则我将数组位置硬编码为1。根据我传递给getModelID()函数的内容,这是我动态需要的值。

http://codepen.io/bdang/pen/pJvmox/?editors=101 http://codepen.io/bdang/pen/pJvmox/?editors=101

function getModelID(extClr, intClr) {

    var colorLockDatesAr = lockDates.Models[0].Colors,
        colorLockDatesCount = colorLockDatesAr.length;

    for(var i = 0; i < colorLockDatesCount; i++) {      

        if(colorLockDatesAr[i].ExtColorCd == extClr && colorLockDatesAr[i].IntColorCd == intClr) {          

            $('#modelID').html(colorLockDatesAr[1].ModelID);

        }   
    }   
}

getModelID('BK', 'BK'); // should return Model 1
getModelID('BK', 'WH'); // should return Model 2
getModelID('WH', 'BK'); // should return Model 3

I have looked through various forums and many issues I've found are related issues but none specific to what I need. 我浏览了各种论坛,发现许多问题都是相关问题,但并不是我所需要的。 Any help with this would be greatly appreciated. 任何帮助,将不胜感激。

Just pass i 只要通过i

$('#modelID').html(colorLockDatesAr[i].ModelID);

http://codepen.io/miguelmota/pen/xGbNVG http://codepen.io/miguelmota/pen/xGbNVG

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM