试图在基于对应数组的索引数组的数组查找字符串

问题描述:

我有名字的数组:试图在基于对应数组的索引数组的数组查找字符串

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))); 

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和名称[I]说行情[I],你可以使用减少,让您的回答。 a默认为“',由第二个属性决定。

我们只需在找到报价时设置返回消息。我可以在名字上使用,以获得说出该报价的人。如果找不到匹配,我们可以使用||保留默认消息。

您可能想考虑使用一个对象来存储名称和引号,因为它可能更容易引用。 {“Q#1A”:“名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>

+0

这可能会工作,但我必须保持阵列为此目的嵌套。也许我可以在身体的某个地方连接数组? –

findQuote功能仍然需要寻找到一个数组(因为quotes是阵列的数组),您可以用includes做。此外,如果您将引号传递给搜索引用而不是在函数中对其进行硬编码,它将会更加实用。然后,你可以使用它时该参数绑定到findQuotefindIndex

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}`);

如果你可以改变你的数据结构,那么最好是pu事情都到一起,属于一:

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}`);

+0

您为较短的方式提供的示例大多数都适用,但它会发回所有由该人说的引用,而不是我正在搜索的引号。你知道这可能是为什么吗? –

+0

从你的问题的代码,我认为这是你在找什么。当然,这段代码只是从数据结构中获取数据,其中包含该人员的引用列表。如果你不想这样做,那么只是不要让函数返回它,因为你已经知道你正在寻找的引用,并且可以在名称后自行打印。 – trincot

+0

这也会起作用。谢谢。 –

理想的情况下你findQuote函数返回另一个函数。您在报价传递给findQuote,并返回功能,该阵列中的每个元素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

+1

谢谢你,安迪。我很感激你编辑这个问题,以使它更具可读性。 –