Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/436.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何找到此数组值的索引号?_Javascript_Arrays_Object_Indexing - Fatal编程技术网

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()
。根据您使用的技术,您可以对此进行更多的研究