【问题标题】:How to get the matched values from an array to another array via (NODE.JS);如何通过(NODE.JS)从一个数组中获取匹配的值到另一个数组;
【发布时间】:2019-09-13 06:44:17
【问题描述】:

我是 JSCRIPT 的新手,我想尝试一下 twitter 情绪分析。我有一个名为“disectToWords”的数组1,情绪所在的位置。然后,我有另一个名为“getScoredWords”的数组,其中“AFINN.txt”或“单词分数”所在的位置。我想匹配两个数组,以获得匹配的值。我正在尝试使用 filter(),但是,我总是得到一个空/空数组,这意味着,所有的都是假的。

var getScoredWords = ['abandon',
  'abandoned',
  'abandons',
  'abducted',
  'abduction',
  'abductions',
  'abhor',
  'abhorred',
  'abhorrent',
  'abhors',
  'abilities',
  'ability',
  'aboard',
  'absentee',]

var disectToWords = ['Barry',
    'Manilow',
    'Singin',
    'With',
    'The',
    'Big',
    'Bands',
    'abducted',
    'aboard',]

var result = getScoredWords.filter(words => disectToWords.includes(words));

但它应该返回单词“abducted”和“aboard”。 请帮忙

【问题讨论】:

  • 去掉变量名两边的引号,除了你的代码没问题,working demo

标签: javascript twitter sentiment-analysis


【解决方案1】:

您好,我正在尝试,它看起来不错,所以您可以尝试

    <script type="text/javascript">
        var getScoredWords = ['abandon',
      'abandoned',
      'abandons',
      'abducted',
      'abduction',
      'abductions',
      'abhor',
      'abhorred',
      'abhorrent',
      'abhors',
      'abilities',
      'ability',
      'aboard',
      'absentee',]

    var disectToWords = ['Barry',
        'Manilow',
        'Singin',
        'With',
        'The',
        'Big',
        'Bands',
        'abducted',
        'aboard',]

    var result = getScoredWords.filter(words => disectToWords.includes(words));
    console.log(result);
    </script>

【讨论】:

  • 顺便说一句,我通过 node.js 在终端(MAC)或 Cprompt(Win)中执行此操作。我认为 alert() 不会在终端上运行。但是,谢谢!
猜你喜欢
  • 1970-01-01
  • 2022-07-05
  • 1970-01-01
  • 2016-03-22
  • 1970-01-01
  • 2020-05-29
  • 1970-01-01
  • 1970-01-01
  • 2023-01-20
相关资源
最近更新 更多