JavaScript compare arrays

后端 未结 2 1045
醉话见心
醉话见心 2021-01-27 15:41

I have an array in the following format:

var markers = [
    [\'Title\', 15.102253, 38.0505243, \'Description\', 1], 
    [\'Another Title\', 15.102253, 38.05052         


        
相关标签:
2条回答
  • 2021-01-27 15:48

    One option is to use nested loops:

    var markers = [
        ['Title', 15.102253, 38.0505243, 'Description', 1], 
        ['Another Title', 15.102253, 38.0505243, 'Another Description', 2],
        ['Title 3', 15.102253, 38.0505243, 'Description 3', 3], 
    ];
    var search = ['1', '2'];
    var result = [];
    
    for (var i = 0; i < search.length; i++)
        for (var j = 0; j < markers.length; j++)
            if (search[i] == markers[j][4]) {
                result.push(markers[j]);
                break;
            }
    
    console.log(result);
    

    DEMO: http://jsfiddle.net/3TErD/

    0 讨论(0)
  • 2021-01-27 16:06

    Couldn't you just use a nested loop here?

    var filteredMarkers = [];
    
    for(var i = 0; i < markers.length; i++) {
    
        for(var j = 0; j < queryStringArray.length; j++) {
    
            // this might need to be changed as your markers has index 4 as a number whereas the queryString array appears to be strings.
            if(markers[i][4] === queryStringArray[j]) {
    
                filteredMarkers.push(markers[i]);
                break;
    
            }
    
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题