繁体   English   中英

ReactJS - JavaScript:过滤器功能无法正常工作

[英]ReactJS - JavaScript: Filter function does not work properly

我正在使用AISHub API构建一个船可视化器。 在查询 API 后,我能够获得包含数千艘船只的 json 文件,但我只过滤了我感兴趣的船只,并将它们注入网页上的表格中。 API 提供以下字段: [NAME, MMSI, LONGITUDE, LATITUDE, others...] 我用于过滤的最重要参数是: NAMEMMSI (虽然可能有多艘船只具有相同的NAME ,但不能有两艘船只具有相同的MMSI编号,因为它是唯一的)。

的问题filter功能似乎没有正确的行为。 事实上,它不会针对特定的NAME和/或MMSI唯一过滤,我最终会拥有多个具有相同NAME和不同MMSI 还有应该出现的船只,它没有出现,尽管我为该特定船只硬编码了NAMEMMSI 这是无法解释的,因为我对这些数字进行了硬编码以进行专门过滤。

在我用于过滤搜索的代码下方:

var express = require('express');
var router = express.Router();
var axios = require('axios');
const NodeCache = require('node-cache');
const myCache = new NodeCache();

let hitCount = 0;

/* GET home page. */
router.get('/', function(req, res, next) {
    res.render('index', { title: 'Express' });
});

const mmsiOfInterest = [
    '367029520', // ok -> MICHIGAN
    '366909730', // ok -> JP BOISSEAU
    '367128570', // ok -> DELAWARE BAY
    '366744010', // ok -> ATLANTIC SALVOR
    // Other MMSI numbers .....
];


const shipNamesOfInterest = [
    'MICHIGAN',
    'JP BOISSEAU',
    'DELAWARE BAY',
    'ATLANTIC SALVOR',
    // Other NAMES....
]

router.get('/hello', async function(req, res, next) {
    const allData = myCache.get('allData');

    if (!allData) {
        hitCount++;
        console.log(`hit ${hitCount} number of times`);
        const { data } = await axios.get(
            'http://data.aishub.net/ws.php?username=MY_KEY&format=1&output=json&compress=0&latmin=11.42&latmax=58.20&lonmin=-134.09&lonmax=-52.62'
        );

        const [ metaData, ships ] = data;
        console.log(data);

        const shipsOfInterest = ships.filter(
            (ship) => mmsiOfInterest.includes(ship.MMSI) || shipNamesOfInterest.includes(ship.NAME)
        );
        myCache.set('allData', shipsOfInterest, 70);
        res.send(data);
        return;
    }

    console.log('this is the data:', allData);
    res.send(allData);
});

module.exports = router;

同样在来自 API 的典型JSON响应下方:

{"MMSI":225342000,"TIME":"2020-01-26 01:45:48 GMT","LONGITUDE":1.43912,"LATITUDE":38.91523,"COG":339.9,"SOG":0,"HEADING":297,"ROT":0,"NAVSTAT":0,"IMO":9822449,"NAME":"ILLETAS JET","CALLSIGN":"EAVX","TYPE":40,"A":4,"B":25,"C":4,"D":4,"DRAUGHT":0,"DEST":"IBIZA","ETA":"00-00 00:00"}

到目前为止我做了什么:

1)我尝试了与filter功能的不同组合,并尝试通过MMSI进行过滤,这对于每个船只来说应该是唯一的,但最终我仍然得到具有相同NAME和不同MMSI (尽管我对MMSI硬编码。 ..我不明白):

const shipsOfInterest = ships.filter(
                (ship) => mmsiOfInterest.includes(ship.MMSI)
            );

在我尝试按NAME过滤后,但这也不起作用:

const shipsOfInterest = ships.filter(
                (ship) => shipNamesOfInterest.includes(ship.NAME)
            );

编辑 2

router.get('/hello', async function(req, res, next) {
    //
    const allData = myCache.get('allData');

    if (!allData) {
        hitCount++;
        console.log(`hit ${hitCount} number of times`);
        const { data } = await axios.get(
            'http://data.aishub.net/ws.php?username=KEY&format=1&output=json&compress=0&latmin=11.42&latmax=58.20&lonmin=-134.09&lonmax=-52.62'
        );

        // console.log(data);
        const { metaData, ships } = data;
        const set = new Set();
        const shipsOfInterest = ships.filter((ship) => {
            if (set.has(ship.MMSI)) return false;
            set.add(ship.MMSI);
            return true;
        });
        myCache.set('allData', shipsOfInterest, 70);
        res.send(data);
        return;
    }

    console.log('this is the data:', allData);
    res.send(allData);
});

module.exports = router;

在错误下方:

错误

我不知道如果以不同的方式组织filter功能是否可以获得更好的结果。 我认为这可能是一种很好的组织搜索方式,但不明白它不起作用的地方。 感谢您指出解决此问题的正确方向。

我猜您在使用速记分配时犯了错误。 使用 {} 而不是 []。

代替:

  const [ metaData, ships ] = data;

尝试:

  const { metaData, ships } = data;

if语句的末尾执行res.send(data) ,这基本上是您从 API 收到的数据。 相反,您需要res.send(shipsOfInterest) 此外,将mmsiOfInterest列表的格式从字符串更改为数字,因为您从 API 接收数字。

因此,据我了解,过滤列表包含具有唯一MMSI值的记录。 所以,你需要做的是:

const uniqueMMSI = new Set();
const shipsOfInterest = ships.filter(
  (ship) => {
    if (set.has(ship.MMSI)) return false;
    set.add(ship.MMSI);
    return true;
  }
);

我希望我理解你的问题:)

看起来 MMSI 以数值形式出现,而您的比较数组则包含字符串。 如何使用整数数组进行比较?

const mmsiOfInterest = [
    367029520,
    366909730, 
    367128570, 
    366744010, 
    // Other MMSI numbers .....
];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM