Javascript 如何找到此数组值的索引号?
Javascript 如何找到此数组值的索引号?,javascript,arrays,object,indexing,Javascript,Arrays,Object,Indexing,const选项=[ {u id:“0175”,姓名:“Ahnaf”}, {u id:“8555”,名字:“Abir”}, {u id:“8795”,名字:“Book”}, ]您可以使用: const选项=[ {u id:“0175”,姓名:“Ahnaf”}, {u id:“8555”,名字:“Abir”}, {u id:“8795”,名字:“Book”}, ] const index=options.findIndex(e=>e._id==“8795”); 控制台日志(索引)您需要使用find
const选项=[
{u id:“0175”,姓名:“Ahnaf”},
{u id:“8555”,名字:“Abir”},
{u id:“8795”,名字:“Book”},
]
您可以使用:
const选项=[
{u id:“0175”,姓名:“Ahnaf”},
{u id:“8555”,名字:“Abir”},
{u id:“8795”,名字:“Book”},
]
const index=options.findIndex(e=>e._id==“8795”);
控制台日志(索引)代码>您需要使用find和findIndex的组合来查找您要查找的索引
const options = [
{_id: "0175", name: "Ahnaf"},
{_id: "8555", name: "Abir"},
{_id: "8795", name: "Book"},
]
const foundObj = options.find((option) => option._id === "8555")
const index = options.findIndex((option, index) => {
if(typeof foundObj !== "undefined" && option._id === foundObj._id) {
return index
}else
return null
} )
console.log('index', index);
在我的节点中不工作application@taslimkd问题是什么?我无法在这里解释我如何将图片发送到这里我已经包含了照片,您现在可以理解问题了。在比较时,您需要将ObjectId
转换为字符串,例如:e.\u id.toString()
。根据您使用的技术,您可以对此进行更多的研究