You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js中如何根据JSON数组的name字段查找对应id值

根据name字段获取对应id的Node.js实现方案

你可以通过数组的find()方法,在GET请求返回的JSON数组中快速匹配目标name值,进而获取对应的id。以下是修改后的完整代码:

const axios = require('axios');
let token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MTc0MzUxNCwidXNlcm5hbWUiOiJzbmFwa2l0cmljaEBnbWFpbC5jb20iLCJyb2xlIjoiYWRtaW4iLCJ0ZWFtSWQiOjE2OTMxMzEsInRv';

axios.get('https://dolphin-anty-api.com/browser_profiles', {
  headers: {
    Authorization: `Bearer ${token}`
  }
})
.then(function (response) {
  // 从返回结果中取出浏览器配置数组
  const profiles = response.data.data;
  // 指定要查找的name值
  const targetName = 'test3';
  // 查找匹配的项
  const targetProfile = profiles.find(item => item.name === targetName);
  
  if (targetProfile) {
    console.log(`匹配到的id值:${targetProfile.id}`);
  } else {
    console.log(`未找到name为${targetName}的项`);
  }
})
.catch(function (error) {
  console.log(error);
});

代码说明:

  • response.data.data:对应接口返回的配置项数组(若你的接口直接返回数组,可改为response.data)
  • Array.find():遍历数组,返回第一个满足item.name === targetName条件的元素
  • 增加容错判断:如果没有匹配到目标name,会给出明确提示

内容的提问来源于stack exchange,提问作者Bella Smith

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 08:15:44