Javascript / Jquery:在嵌套数组中使用键值对查找第一个和最后一个数组的索引的最有效方法

时间:2016-01-05 11:24:04

标签: javascript jquery arrays

我有一个看起来像这样的嵌套数组:

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做到这一点?

5 个答案:

答案 0 :(得分:1)

使用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>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您可以使用totalCH = credithour1 + credithour2...

&#13;
&#13;
Array.forEach
&#13;
&#13;
&#13;

答案 2 :(得分:1)

一种选择是使用 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>

答案 3 :(得分:1)

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

demo

答案 4 :(得分:0)

试试这个

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]);