亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用在函數內檢索和更新的數據更新數組

使用在函數內檢索和更新的數據更新數組

夢里花落0921 2021-10-14 13:21:56
我正在使用 .reduce 函數從數組中獲取數據,我不確定這是否是最好的方法,但是由于我在另一篇文章中收到的一些幫助,這是目前唯一有效的方法。我不知道如何使用函數中所做的更改來更新數組,返回數組會忽略所做的更改。數據由路由器提供并每秒更新一次。var data    =  [ {  macProtocol: 'ip',    ipProtocol: 'udp',    dstAddress: '1.1.1.1',    tx: '0',    rx: '384'}, {  macProtocol: 'ip',    ipProtocol: 'udp',    dstAddress: '8.8.8.8',    tx: '384',    rx: '384'}, {  macProtocol: 'ip',    ipProtocol: 'udp',    dstAddress: '1.1.1.1',    tx: '384',    rx: '384',},];// We resolve the DNS and output the final arraylet DNSvariable = result.reduce((arr, currentValue) => {    var setIP = currentValue.dstAddress;    var dns = require('dns');    dns.setServers([        '8.8.8.8',     ]);    // Test that IP's are being received correctly.     console.log(setIP);// Resolve the IP addressesdns.reverse(setIP, (err, address) => {     currentValue.dstAddress = address;    // If I log the currentValue inside this function everything looks correct, it has updated the values in the full array. I need to push this array, into the returned array. });return arr;}, []);// Logging the variable produces the array with the original IPconsole.log(DNSvariable);我想要 console.log(DNSvariable); 返回具有解析名稱的數組,此時它正在返回地址。我只是不知道如何更新數組。預期結果是:var data    =  [ {  macProtocol: 'ip',    ipProtocol: 'udp',    dstAddress: 'one.one.one.one',    tx: '0',    rx: '384'}, {  macProtocol: 'ip',    ipProtocol: 'udp',    dstAddress: 'dns.google',    tx: '384',    rx: '384'}, {  macProtocol: 'ip',    ipProtocol: 'udp',    dstAddress: 'one.one.one.one',    tx: '384',    rx: '384',},];如果有任何不正確或發布不正確,我深表歉意。編輯:我使用 reduce 函數的原因是因為訪問 dstAddress 的所有其他方法都沒有以與 dns 函數一起使用的方式返回數據。這絕對不是正確的方法。
查看完整描述

3 回答

?
烙印99

TA貢獻1829條經驗 獲得超13個贊

如果你想使用reduce,那么代替


return arr


嘗試


return [...arr, currentValue]


但是,我認為您最好提供地圖:


let DNSvariable = result.map((currentValue) => {


    var setIP = currentValue.dstAddress;


    var dns = require('dns');

    dns.setServers([

        '8.8.8.8', 

    ]);


    dns.reverse(setIP, (err, address) => {

        currentValue.dstAddress = address;

    });


    return currentValue;

});

我還建議將這些行移到您的函數之外,因為它們只需要調用一次。


var dns = require('dns');

dns.setServers([

    '8.8.8.8', 

]);


查看完整回答
反對 回復 2021-10-14
?
神不在的星期二

TA貢獻1963條經驗 獲得超6個贊

Reduce 看起來并不適合您的用例。我建議使用“地圖”方法。例如,


如果有一個數組,


var arr1=[{a:1},{a:3},{a:4}]

您需要另一個包含一些修改記錄的數組:


var arr2=arr1.map((elem)=>{ 

  const elem2={...elem}

  elem2.b=5;

  return elem2;

})



console.log('Array1:',arr1)

console.log('Array2:',arr2)

結果:


'Array1:' [ { a: 1 }, { a: 3 }, { a: 4 } ]

'Array2:' [ { a: 1, b: 5 }, { a: 3, b: 5 }, { a: 4, b: 5 } ]


查看完整回答
反對 回復 2021-10-14
?
翻閱古今

TA貢獻1780條經驗 獲得超5個贊

首先,使用新的 Promises api


const { Resolver } = require('dns').promises;

const resolver = new Resolver();

resolver.setServers(['8.8.8.8']);

接下來,像這樣解析所有地址:


async function main() {

    const promises = data.map(item => resolver.reverse(item.dstAddress));

    const hostNames = await Promise.all(promises);

    data.forEach((item, i) => {

        item.dstAddress = hostNames[i];

    });

    console.log(data);

}


main();

首先,我們構造一個包含所有解析 Promise 的數組。接下來我們等待所有 Promise 被解決。最后,我們.dstAddress用每個相應的結果覆蓋。


編輯:


由于Promise.all如果單個 Promise 失敗則失敗,這里有一個包含dns.reverse在自定義 Promise 中的替代版本:


const dns = require('dns');

dns.setServers(['8.8.8.8']);

const reverser = (ipAddress) => new Promise(resolve => {

  dns.reverse(ipAddress, (err, hostname) => {

    if (err) resolve(ipAddress);

    else resolve(hostname);

  });

});

如您所見,解析 IP 失敗只會返回 IP;這樣每個 Promise 都會解決,即使它dns.reverse自己失敗了。


像這樣使用:


async function main() {

  const promises = data.map(item => reverser(item.dstAddress));

  const hostnames = await Promise.all(promises);

  data.forEach((item, i) => {

    item.dstAddress = hostnames[i];

  });

  console.log(data);

}


main();


查看完整回答
反對 回復 2021-10-14
  • 3 回答
  • 0 關注
  • 208 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號