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

NodeJS中如何过滤数组获取Type=A的DNS记录IP?

问题:查找Type为"A"的DNS记录IP地址

我有一个包含多个对象的数组(下方是var_dump输出结果),需要找到Type为"A"的数组元素(索引位置不固定),尝试用.includes和.filter方法都没成功,目标是获取这条Type="A"记录的IP地址,求NodeJS中的简便实现方法。

数据输出

object(2) {
    ["host"] => array(2) {
        [0] => object(1) {
            ["$"] => object(10) {
                ["HostId"] => string(9) "326540292"
                ["Name"] => string(1) "@"
                ["Type"] => string(1) "A"
                ["Address"] => string(14) "1.1.1.1"
                ["MXPref"] => string(2) "10"
                ["TTL"] => string(2) "60"
                ["AssociatedAppTitle"] => string(0) ""
                ["FriendlyName"] => string(0) ""
                ["IsActive"] => string(4) "true"
                ["IsDDNSEnabled"] => string(5) "false"
            }
        }
        [1] => object(1) {
            ["$"] => object(10) {
                ["HostId"] => string(9) "326540291"
                ["Name"] => string(3) "www"
                ["Type"] => string(5) "CNAME"
                ["Address"] => string(19) "domain.com."
                ["MXPref"] => string(2) "10"
                ["TTL"] => string(3) "300"
                ["AssociatedAppTitle"] => string(0) ""
                ["FriendlyName"] => string(0) ""
                ["IsActive"] => string(4) "true"
                ["IsDDNSEnabled"] => string(5) "false"
            }
        }
    }
}

当前代码

var namecheapApi = require('namecheap-api') 
const var_dump = require('var_dump')

....

namecheapApi.apiCall("namecheap.domains.dns.getHosts", {'SLD':sld,'TLD':tld}).then(function (data)
          {
         
           var test =  data['response'][0]["DomainDNSGetHostsResult"][0]["host"].filter(function(itemas) {
             return itemas.match(/\d+/).Type=="A";
           });

           var_dump (test);

  }
      );
  }
  
}

解决方案

核心问题

原代码错误在于:

  1. 没有正确访问数据结构:Type属性嵌套在每个host元素的$对象下,而非直接挂载在itemas上
  2. 错误地对对象调用了字符串的match方法,逻辑完全不符合数据结构

方法1:用find获取单个A记录(推荐)

如果只需要第一个匹配的A记录,用Array.find()效率更高,找到目标后立即停止遍历:

var namecheapApi = require('namecheap-api') 
const var_dump = require('var_dump')

// ...

namecheapApi.apiCall("namecheap.domains.dns.getHosts", {'SLD':sld,'TLD':tld}).then(function (data) {
  // 提取host数组
  const hostList = data['response'][0]["DomainDNSGetHostsResult"][0]["host"];
  // 查找Type为"A"的记录
  const aRecord = hostList.find(item => item["$"].Type === "A");
  
  if (aRecord) {
    const ip = aRecord["$"].Address;
    console.log("A记录IP:", ip);
    var_dump(ip);
  } else {
    console.log("未找到Type为A的记录");
  }
});

方法2:用filter获取所有A记录

如果存在多个A记录,用filter可以获取全部匹配项:

var namecheapApi = require('namecheap-api') 
const var_dump = require('var_dump')

// ...

namecheapApi.apiCall("namecheap.domains.dns.getHosts", {'SLD':sld,'TLD':tld}).then(function (data) {
  const hostList = data['response'][0]["DomainDNSGetHostsResult"][0]["host"];
  // 筛选所有Type为"A"的记录
  const aRecords = hostList.filter(item => item["$"].Type === "A");
  
  if (aRecords.length > 0) {
    // 提取第一个A记录的IP
    const firstIp = aRecords[0]["$"].Address;
    console.log("第一个A记录IP:", firstIp);
    // 提取所有A记录的IP
    const allIps = aRecords.map(record => record["$"].Address);
    console.log("所有A记录IP:", allIps);
    var_dump(allIps);
  } else {
    console.log("未找到Type为A的记录");
  }
});

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 05:07:52