我有一个对象数组.我需要获取最后一个对象的对象类型(在此示例中为"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 …Run Code Online (Sandbox Code Playgroud)