2017-08-15 27 views
0

我有名字的數組:試圖在基於對應數組的索引數組的數組查找字符串

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

回答

0
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」,...}

0

如果行情是要查找什麼,那麼也許它們存儲在嵌套數組使得這種不必要的複雜。 我會將報價存儲在一個平面數組中,並將它們映射到名稱。 也許是這樣的:

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

這可能會工作,但我必須保持陣列爲此目的嵌套。也許我可以在身體的某個地方連接數組? –

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

這也會起作用。謝謝。 –

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

謝謝你,安迪。我很感激你編輯這個問題,以使它更具可讀性。 –