JavaScript compare arrays

后端 未结 2 1047
醉话见心
醉话见心 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/

提交回复
热议问题