Gra*_*eck 15 javascript arrays
我有一个对象数组.我需要获取最后一个对象的对象类型(在此示例中为"shape"),将其删除,然后在数组中找到具有相同类型的前一个对象的索引,例如"shape".
var fruits = [
{
shape: round,
name: orange
},
{
shape: round,
name: apple
},
{
shape: oblong,
name: zucchini
},
{
shape: oblong,
name: banana
},
{
shape: round,
name: grapefruit
}
]
// What's the shape of the last fruit
var currentShape = fruits[fruits.length-1].shape;
// Remove last fruit
fruits.pop(); // grapefruit removed
// Find the index of the last round fruit
var previousInShapeType = fruits.lastIndexOf(currentShape);
// should find apple, index = 1
Run Code Online (Sandbox Code Playgroud)
所以,显然这个例子中的类型将是"圆形".但我不是在寻找"圆"的数组值.我正在寻找fruits.shape = round的地方.
var previousInShapeType = fruits.lastIndexOf(fruits.shape = currentShape);
Run Code Online (Sandbox Code Playgroud)
但只是使用它不起作用.我确定我错过了一些简单的事情.如何在数组中找到对象形状=圆形的最后一项?
Luk*_*Liu 26
var fruit = fruits.slice().reverse().find(fruit => fruit.shape === currentShape);
Run Code Online (Sandbox Code Playgroud)
Nen*_*vic 18
提案现已Array.prototype.findLast进入第3Array.prototype.findLastIndex阶段4!
以下是如何使用它们:
\nconst fruits = [\n { shape: \'round\', name: \'orange\' },\n { shape: \'round\', name: \'apple\' },\n { shape: \'oblong\', name: \'zucchini\' },\n { shape: \'oblong\', name: \'banana\' },\n { shape: \'round\', name: \'grapefruit\' }\n]\n\nlet last_element = fruits.findLast((item) => item.shape === \'oblong\');\n// \xe2\x86\x92 { shape: oblong, name: banana }\n\nlet last_element_index = fruits.findLastIndex((item) => item.shape === \'oblong\');\n// \xe2\x86\x92 3\nRun Code Online (Sandbox Code Playgroud)\n您可以在这篇 V8 博客文章中阅读更多内容。
\n您可以在“Chrome 新增功能”系列中找到更多内容。
\nDen*_*Den 17
您可以将数组转换为数组boolean类型并获取最后一个true索引。
const lastIndex = fruits.map(fruit =>
fruit.shape === currentShape).lastIndexOf(true);
Run Code Online (Sandbox Code Playgroud)
Anu*_*rya 11
使用Lodash 库,您可以找到最后一个逻辑元素。
_.findLast([1,2,3,5,4], n => n % 2 == 1); // Find last odd element
// expected output: 5
Run Code Online (Sandbox Code Playgroud)
这是一个不依赖于 的解决方案,reverse因此不需要“克隆”原始集合。
const lastShapeIndex = fruits.reduce((acc, fruit, index) => (
fruit.shape === currentShape ? index : acc
), -1);
Run Code Online (Sandbox Code Playgroud)
var previousInShapeType, index = fruits.length - 1;
for ( ; index >= 0; index--) {
if (fruits[index].shape == currentShape) {
previousInShapeType = fruits[index];
break;
}
}
Run Code Online (Sandbox Code Playgroud)
您还可以向后遍历数组。
小提琴:http : //jsfiddle.net/vonn9xhm/
一种更简单且相对有效的解决方案。过滤和流行!
过滤所有与当前形状匹配的水果,然后弹出以获取最后一个。
fruits.filter(({shape}) => shape === currentShape).pop()
var fruits = [{
shape: 'round',
name: 'orange'
}, {
shape: 'round',
name: 'apple'
}, {
shape: 'oblong',
name: 'zucchini'
}, {
shape: 'oblong',
name: 'banana'
}, {
shape: 'round',
name: 'grapefruit'
}];
// What's the shape of the last fruit
var currentShape = fruits[fruits.length - 1].shape;
// Remove last fruit
fruits.pop(); // grapefruit removed
alert(fruits.filter(({shape}) => shape === currentShape).pop().name);Run Code Online (Sandbox Code Playgroud)