[ { "uid": "d259832e1fc86bc3f81dc2df4ac95946", "id": "481859bbd4d42871599ca5c8780b5319", "status": "done", "url": "https://file-server.yunhaochefu.com/image/d259832e1fc86bc3f81dc2df4ac95946", "isHas": true }, { "uid": "ad8b5dbdda15294631377d9ebe0700c1", "id": "09366a13ef31f460d769843c6bb5ad34", "status": "done", "url": "https://file-server.yunhaochefu.com/image/ad8b5dbdda15294631377d9ebe0700c1", "isHas": true } ] { "uid": "d259832e1fc86bc3f81dc2df4ac95946", "id": "481859bbd4d42871599ca5c8780b5319", "status": "done", "url": "https://file-server.yunhaochefu.com/image/d259832e1fc86bc3f81dc2df4ac95946", "isHas": true } 将对象中的id 与数组中的id匹配,如果相等,那么返回数组中的与之匹配id的索引值js es6
时间: 2023-08-09 14:10:55 浏览: 290
copy_A2C4D76B-A0AF-4BC3-9693-A5307FA7D470.GIF
你可以使用 `findIndex` 方法来查找数组中与指定 id 相匹配的对象,并返回其索引值。以下是一个示例代码:
```javascript
const arr = [
{
"uid": "d259832e1fc86bc3f81dc2df4ac95946",
"id": "481859bbd4d42871599ca5c8780b5319",
"status": "done",
"url": "https://file-server.yunhaochefu.com/image/d259832e1fc86bc3f81dc2df4ac95946",
"isHas": true
},
{
"uid": "ad8b5dbdda15294631377d9ebe0700c1",
"id": "09366a13ef31f460d769843c6bb5ad34",
"status": "done",
"url": "https://file-server.yunhaochefu.com/image/ad8b5dbdda15294631377d9ebe0700c1",
"isHas": true
}
];
const targetId = "481859bbd4d42871599ca5c8780b5319";
const index = arr.findIndex((item) => item.id === targetId);
console.log(index); // 输出:0
```
在上面的代码中,我们使用 `findIndex` 方法来遍历数组 `arr`,并在回调函数中判断每个对象的 `id` 是否与目标 id 相等。如果相等,则返回匹配项的索引值。如果没有找到匹配的项,则返回 -1。在这个例子中,目标 id 为 "481859bbd4d42871599ca5c8780b5319",与数组中的第一个对象的 `id` 属性匹配,所以返回索引值 0。
阅读全文