javascript - 查找数组中对象的索引

标签 javascript

我想搜索对象数组以查找特定 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/

相关文章:

javascript - 使用 Javascript 和 jQuery 调用 Web API

JavaScript 不安全访问?

javascript - 如何确保 jQuery 加载的可滚动模态内容始终滚动回顶部?

javascript - 在 Javascript 中捕获 DTE 事件

javascript - jquery 追加在 IE/Firefox 中不起作用

javascript - django 循环中的计数器

javascript - 如何让我的 Discord 机器人检查成员是否有角色?

javascript - 当所有数据项都相同时,使用 d3 绘制一条线是不可见的

JavaScript:需要帮助订购我的 JS "Uncaught TypeError: Cannot read property ' 值为 null"

javascript - Ajax 调用以 Base64 字符串形式返回 PDF 文件