簡體   English   中英

Javascript / Jquery:查找嵌套數組中具有鍵值對的第一個和最后一個數組的索引的最有效方法

[英]Javascript/Jquery: Most efficient way to find index of first and last array with key-value pair in nested array

我有一個看起來像這樣的嵌套數組:

route = [{
  instruction: "Walk to the New York Train Station",
  type: "WALK"
}, {
  instruction: "Take the train to Chicago Train Station",
  type: "TRANSIT"
}, {
  instruction: "Take the metro to Randolph Street",
  type: "TRANSIT"
}, {
  instruction: "Walk to the restaurant",
  type: "WALK"
}, ];

我想在此嵌套數組中找到第一個和最后一個數組的索引,其type: "WALK" 如何使用Jquery和/或JavaScript做到這一點?

使用Array.filter

 var route = [ { instruction: "Walk to the New York Train Station", type: "WALK" }, { instruction: "Take the train to Chicago Train Station", type: "TRANSIT" }, { instruction: "Take the metro to Randolph Street", type: "TRANSIT" }, { instruction: "Walk to the restaurant", type: "WALK" }, ]; var routeFiltered = route.filter( function (v, i) { v.index = i; return v.type === 'WALK' } // ^ add index value ); document.querySelector('#result').textContent = JSON.stringify(routeFiltered, null, ' '); // if it's just the indexes you need - use Array.map: document.querySelector('#result').textContent += '\\n\\nindexes of route-elements of type \\'WALK\\': '+ routeFiltered.map( function (v) { return v.index } ); 
 <pre id="result"></pre> 

您可以使用Array.forEach

 var route = [{ instruction: "Walk to the New York Train Station", type: "WALK" }, { instruction: "Take the train to Chicago Train Station", type: "TRANSIT" }, { instruction: "Take the metro to Randolph Street", type: "TRANSIT" }, { instruction: "Walk to the restaurant", type: "WALK" }, ]; var result = []; route.forEach(function(item){ if(item.type === "WALK"){ result.push(item); } }) console.log(result) 

一種選擇是使用UnderscoreJs

 var route = [ { instruction: "Walk to the New York Train Station", type: "WALK" }, { instruction: "Take the train to Chicago Train Station", type: "TRANSIT" }, { instruction: "Foo Bar", type: "WALK" }, { instruction: "Take the metro to Randolph Street", type: "TRANSIT" }, { instruction: "Walk to the restaurant", type: "WALK" }, ]; var firstIndex = _.findIndex(route, { type: "WALK" }); console.log(route[firstIndex]); var lastIndex = _.findLastIndex(route, { type: "WALK" }); console.log(route[lastIndex]); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script> 

var indexes = [];
function findIndexOf(value,i) {
  if(value.type == "WALK"){
        indexes.push(i);
        return true;
    }
}
var filtered = route.filter(findIndexOf);
console.log(indexes);

演示

嘗試這個

route = [
{ instruction: "Walk to the New York Train Station",
    type:        "WALK"
},
{ instruction: "Take the train to Chicago Train Station",
    type:        "TRANSIT"
},
{ instruction: "Walk to the restaurant",
    type:        "WALK"
},
{ instruction: "Walk to the restaurant",
    type:        "WALK"
},
{ instruction: "Take the metro to Randolph Street",
    type:        "TRANSIT"
},
{ instruction: "Walk to the restaurant",
    type:        "WALK"
},
];

var indices = [];
for(var i = 0; i < route.length; i++) {
    if(route[i].type === 'WALK') {
        indices.push(i);
    }
}   
console.log("First :"+indices[0]+" Last : "+indices[indices.length-1]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM