【问题标题】:Trying to Find String in Array of Arrays Based on Index of Corresponding Array尝试根据对应数组的索引在数组数组中查找字符串
【发布时间】:2018-01-23 19:03:03
【问题描述】:

我有一个名字数组:

names = [name1, name2, name3...]

对应于一个数组数组,其中包含对应于名称数组的引号。

quotes = [[q#1a, q#2b..], [q#1c, q#2d..], [q#1e, q#2f]]

我正在尝试创建一个函数,您可以在其中输入特定的引用(例如q#1c),console.log 将返回说它的人的姓名。

我被卡住了,一直不确定。有什么建议?以下是我到目前为止所拥有的。

function findQuote(quote) {
  return quote == `"q#1c"`;
}

let whoSaidIt = (c) => {
  let quote = quotes.index;
  return `${name[c]} said ${quotes[c]}.`;
};

console.log(whoSaidIt(quotes.findIndex(findQuote)));

【问题讨论】:

    标签: javascript arrays string return


    【解决方案1】:

    如果引号是您要查找的内容,那么可能将它们存储在嵌套数组中会使这变得不必要地复杂。 我会将引号存储在一个平面数组中并将它们映射到名称。 也许是这样的:

    var names = ["Einstein","Trump","Pikachu"];
    var quotes = ["E=mcc","Sad.","Pika!","Two things are stupid."];
    var quotees = [0,1,2,0];
    
    function whoSaidIt(q) {
      return names[quotees[quotes.indexOf(q)]];
    }
    
    document.getElementById("out").innerHTML=whoSaidIt("E=mcc");
    <div id="out"></div>

    【讨论】:

    • 这可能有效,但我必须为此目的保持数组嵌套。也许我可以在身体的某个地方连接数组?
    【解决方案2】:
    let names = ['name1', 'name2', 'name3'];
    let quotes = [['q#1a', 'q#2b'], ['q#1c', 'q#2d'], ['q#1e', 'q#2f']];
    let whoSaidIt = (c) => quotes.reduce((a, v, i) => quotes[i].includes(c) ? `${names[i]} said ${c}.` : a, '') || 'No-one said that!';
    
    whoSaidIt('q#1c'); //Returns 'name2'
    

    假设 names.length === quotes.length 和 names[I] 表示引号 [I],您可以使用 reduce 来得到答案。 a 默认为 '',由第二个属性决定。

    我们只需在找到报价时设置返回消息。 i 可以用在名字上来得到说这句话的人。如果找不到匹配项,我们可以使用 || 留下默认消息。

    您可能需要考虑使用一个对象来存储名称和引号,因为它可能更容易引用。 { 'q#1a': 'name1', ... }

    【讨论】:

      【解决方案3】:

      您的findQuote 函数仍然需要查看一个数组(因为quotes 是一个数组数组),您可以使用includes 来完成。此外,如果您将要搜索的引用传递给它而不是在函数中对其进行硬编码,这将更加实用。然后你可以在findIndex中使用它时将该参数绑定到findQuote

      var names = ['John', 'Helen', 'Anna'];
      var quotes = [['q#1a', 'q#2b'], ['q#1c', 'q#2d'], ['q#1e', 'q#2f']];
      
      function findQuote(quote, quotes) {
        return quotes.includes(quote);
      }
      
      let whoSaidIt = (c) => {
        return names[c];
      };
      
      let quote = "q#1c";
      console.log(`${whoSaidIt(quotes.findIndex(findQuote.bind(null,quote)))} said ${quote}`);

      简而言之:

      const names = ['John', 'Helen', 'Anna'],
            quotes = [['q#1a', 'q#2b'], ['q#1c', 'q#2d'], ['q#1e', 'q#2f']],
            findQuote = (quote, quotes) => quotes.includes(quote),
            whoSaidIt = c => names[c],
            quote = "q#1c";
      
      console.log(`${whoSaidIt(quotes.findIndex(findQuote.bind(null,quote)))} said ${quote}`);

      如果你可以改变你的数据结构,那么最好把属于一起的东西放在一起:

      const quotes = [
               { name: 'John', quotes: ['q#1a', 'q#2b'] },
               { name: 'Helen', quotes: ['q#1c', 'q#2d'] },
               { name: 'Anna', quotes: ['q#1e', 'q#2f'] }
            ],
            findQuote = (quote, quotes) => quotes.quotes.includes(quote),
            quote = "q#1c";
      
      console.log(`${quotes.find(findQuote.bind(null,quote)).name} said ${quote}`);

      【讨论】:

      • 您为较短方式提供的示例在大多数情况下都有效,但它会发回该人所说的所有报价,而不仅仅是我正在搜索的报价。你知道为什么会这样吗?
      • 从您的问题代码中,我认为这就是您要寻找的。当然,这段代码只是从数据结构中获取数据,其中包含人员的报价列表。如果您不希望这样,那么就不要让函数返回它,因为您已经知道要查找的报价,并且可以在名称后自己打印。
      【解决方案4】:

      理想情况下,您的 findQuote 函数应该返回另一个函数。您将引用传递给findQuotethat 返回 findIndex 对数组的每个元素调用的函数:

      function findQuote(quote) {
        return function (el) {
          return el === quote;
        }
      }
      

      然后你就可以完成代码了:

      let names = ['Bob', 'Dave', 'Mavis'];
      let quotes = ['q#1c','q#2c','q#3c'];
      
      let whoSaidIt = (names, quotes, quote) => {
        const index = quotes.findIndex(findQuote(quote));
        return `${names[index]} said ${quote}`;
      };
      
      const quote = 'q#1c';
      const result = whoSaidIt(names, quotes, quote);
      

      DEMO

      【讨论】:

      • 谢谢你,安迪。感谢您编辑问题以使其更具可读性。
      猜你喜欢
      • 1970-01-01
      • 2018-10-14
      • 2020-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-25
      • 2021-04-16
      相关资源
      最近更新 更多