假設我有一個 javascript 物件陣列,如下所示:
[
{
"title": "The Great Peace",
"copyversion": 1
},
{
"title": "History of World War II",
"copyversion": 1
},
{
"title": "Crime and Punishment",
"copyversion": 2
},
{
"title": "War and Peace",
"copyversion": 2
}
]
現在,假設我有一個搜索字串,例如“War”或“and”。我想獲得一個物件陣列,其中“title”包含搜索字串(不區分大小寫),但我還想包含任何具有匹配“copyversion”值的兄弟值。
例如:
搜索字串“Great”應該會產生以下結果,因為即使“History of World War II”中沒有“Great”,它也會匹配具有“Great”的內容。
[
{
"title": "The Great Peace",
"copyversion": 1
},
{
"title": "History of World War II",
"copyversion": 1
}
]
另一個例子:
Search string of "Peace" would yield the original array. "History of World War II" is included because it has the same copyversion value as "The Great Peace", and "Crime and Punishment" is included because it has the same copyversion as "War and Peace"
[
{
"title": "The Great Peace",
"copyversion": 1
},
{
"title": "History of World War II",
"copyversion": 1
},
{
"title": "Crime and Punishment",
"copyversion": 2
},
{
"title": "War and Peace",
"copyversion": 2
}
]
If no matches are found, then an empty array would result.
I'm looking for a reasonable fast way to do this. I'm fine with pure javascript or a library like lodash.
uj5u.com熱心網友回復:
這是我簡單易讀的解決方案。希望它對你有用
const books = [
{
"title": "The Great Peace",
"copyversion": 1
},
{
"title": "History of World War II",
"copyversion": 1
},
{
"title": "Crime and Punishment",
"copyversion": 2
},
{
"title": "War and Peace",
"copyversion": 2
}
];
const findBooks = (titlePart) => {
const regexp = new RegExp(`${titlePart}`, 'i');
const resultSet = new Set();
const copyVersionsSet = new Set();
// Find all books which has titlePart in title
for (const book of books) {
if (regexp.test(book.title)) {
resultSet.add(book);
copyVersionsSet.add(book.copyversion);
}
}
// Find all books which has same copyversion as found books
for (const book of books) {
if (copyVersionsSet.has(book.copyversion)) {
resultSet.add(book);
}
}
return [...resultSet];
}
console.log(findBooks('hIsToRy'));
uj5u.com熱心網友回復:
解決方案包含兩部分:
- 找到所有匹配的物件并收集它們的 copyversion。不要存盤重復項。
- 回傳具有相應 copyversion 的所有物件。
第一部分可能會被優化——我們不必洗掉重復項。
const a = [
{
"title": "The Great Peace",
"copyversion": 1
},
{
"title": "History of World War II",
"copyversion": 1
},
{
"title": "Crime and Punishment",
"copyversion": 2
},
{
"title": "War and Peace",
"copyversion": 2
}
];
const copyFinder = (word, arr) => {
const rx = new RegExp(`${word}`, 'i');
const versions = arr.reduce((collector, value) => {
if(rx.test(value.title) && collector.indexOf(value.copyversion) === -1) {
collector.push(value.copyversion);
}
return collector;
}, []);
if(versions.length === 0) {
return [];
}
return arr.filter(x => versions.indexOf(x.copyversion) > -1);
}
console.log(copyFinder('History', a));
uj5u.com熱心網友回復:
這是一種方法:
const findMatches = (books = []) => (
search = "",
lc = search .toLowerCase (),
matches = new Set (books .filter (({title}) => title .toLowerCase () .includes (lc))),
versions = new Set ([...matches] .map (({copyversion}) => copyversion))
) => books .filter ((book) => matches .has (book) || versions .has (book .copyversion))
const books = [{"title": "The Great Peace", "copyversion": 1}, {"title": "History of World War II", "copyversion": 1}, {"title": "Crime and Punishment", "copyversion": 2}, {"title": "War and Peace", "copyversion": 2}]
console .log ('crime: ', findMatches (books) ('crime'))
console .log ('great: ', findMatches (books) ('great'))
console .log ('war: ', findMatches (books) ('war'))
.as-console-wrapper {max-height: 100% !important; top: 0}
我們采用搜索字串的小寫版本,使用它來過濾串列以匹配標題,將它們存盤為一個集合,然后將它們映射到它們的版本,再次存盤為一個集合,最后過濾原始串列書籍以選擇匹配集中的書籍或其版本在版本一中的書籍。
盡管我選擇以這種方式作業,在可行的情況下使用默認引數代替區域變數,但如果findMatches (books)以某些方式使用,例如傳遞給map. 如果這是一個問題,這個版本以稍微復雜的方式做同樣的事情,沒有那些潛在的問題,因為它知道的唯一引數是books和search:
const findMatches = (books = []) => (search = "") => ((
lc = search .toLowerCase (),
matches = new Set (books .filter (({title}) => title .toLowerCase () .includes (lc))),
versions = new Set ([...matches] .map (({copyversion}) => copyversion))
) => books .filter ((book) => matches .has (book) || versions .has (book .copyversion)))()
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/439307.html
標籤:javascript arrays json algorithm lodash
