我想搜索对象数组以查找特定 id 的索引。让我们看一下:
这是我的对象数组:
var sentences = [0,
{ id: "1-1", layer: ["A1", "A2", "A3", "A4", "A5"], difficulty: 4, track: "end", start: 4, end: 6, accuracy: undefined , accent: undefined, meaning_weight: undefined, shadow_weight: undefined, write_weight: undefined },
{ id: "1-2", layer: ["A1", "A2", "A3", "A4", "A5"], difficulty: 4, track: "end", start: 4, end: 6, accuracy: undefined , accent: undefined, meaning_weight: undefined, shadow_weight: undefined, write_weight: undefined },
{ id: "1-3", layer: ["A1", "A2", "A3", "A4", "A5"], difficulty: 4, track: "end", start: 4, end: 6, accuracy: undefined , accent: undefined, meaning_weight: undefined, shadow_weight: undefined, write_weight: undefined },
{ id: "2-1", layer: ["A1", "A2", "A3", "A4", "A5"], difficulty: 4, track: "end", start: 4, end: 6, accuracy: undefined , accent: undefined, meaning_weight: undefined, shadow_weight: undefined, write_weight: undefined },
{ id: "2-2", layer: ["A1", "A2", "A3", "A4", "A5"], difficulty: 4, track: "end", start: 4, end: 6, accuracy: undefined , accent: undefined, meaning_weight: undefined, shadow_weight: undefined, write_weight: undefined },
{ id: "2-3", layer: ["A1", "A2", "A3", "A4", "A5"], difficulty: 4, track: "end", start: 4, end: 6, accuracy: undefined , accent: undefined, meaning_weight: undefined, shadow_weight: undefined, write_weight: undefined },
];
例如我想查找id: "2-1"
的索引这是 4
。我怎样才能做到这一点?
我尝试过类似的方法但没有成功......
pos = sentences.map(function(e) { return e.sentences.id; }).indexOf('2-1');
console.log(pos)
最佳答案
这里最好的方法是使用findIndex()
var sentences = [0, { id: "1-1", layer: ["A1", "A2", "A3", "A4", "A5"], difficulty: 4, track: "end", start: 4, end: 6, accuracy: undefined , accent: undefined, meaning_weight: undefined, shadow_weight: undefined, write_weight: undefined }, { id: "1-2", layer: ["A1", "A2", "A3", "A4", "A5"], difficulty: 4, track: "end", start: 4, end: 6, accuracy: undefined , accent: undefined, meaning_weight: undefined, shadow_weight: undefined, write_weight: undefined }, { id: "1-3", layer: ["A1", "A2", "A3", "A4", "A5"], difficulty: 4, track: "end", start: 4, end: 6, accuracy: undefined , accent: undefined, meaning_weight: undefined, shadow_weight: undefined, write_weight: undefined }, { id: "2-1", layer: ["A1", "A2", "A3", "A4", "A5"], difficulty: 4, track: "end", start: 4, end: 6, accuracy: undefined , accent: undefined, meaning_weight: undefined, shadow_weight: undefined, write_weight: undefined }, { id: "2-2", layer: ["A1", "A2", "A3", "A4", "A5"], difficulty: 4, track: "end", start: 4, end: 6, accuracy: undefined , accent: undefined, meaning_weight: undefined, shadow_weight: undefined, write_weight: undefined }, { id: "2-3", layer: ["A1", "A2", "A3", "A4", "A5"], difficulty: 4, track: "end", start: 4, end: 6, accuracy: undefined , accent: undefined, meaning_weight: undefined, shadow_weight: undefined, write_weight: undefined }, ];
const res = sentences.findIndex(x => x.id === "2-1");
console.log(res)
您的方法中的问题是您从 map()
返回 e.sentences.id
,这将给出错误。您只需要返回e.id
pos = sentences.map(function(e) { return e.id; }).indexOf('2-1');
通过使用箭头函数
pos = sentences.map(e => e.id).indexOf('2-1');
关于javascript - 查找数组中对象的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57331945/