我需要撰寫一個演算法,讓用戶選擇一個選項來過濾特定資料,每次點擊它都會發出一個 api 請求,回傳過濾后的資料。用戶點擊它需要繼續過濾掉資料的次數。我的問題是,演算法如何保存最近的結果并對最近的結果運行 for/filter 回圈?我應該將最近的結果存盤在 localStorage 中以便進一步過濾結果嗎?當用戶決定取消選擇他們想要過濾的資料時,它應該向用戶顯示他們之前的結果。用戶應該能夠繼續過濾,直到獲得所需的資料。請看下面的例子。
***DATA***
let data = [
{"id":0, "name":"John1", "age":29, "city":"seattle", "score": 95},
{"id":1, "name":"John2", "age":28, "city":"seattle", "score": 95},
{"id":2, "name":"John3", "age":29, "city":"seattle", "score": 85},
{"id":3, "name":"John4", "age":30, "city":"austin", "score": 75},
{"id":4, "name":"John5", "age":24, "city":"austin", "score": 85},
{"id":5, "name":"John6", "age":30, "city":"aspen", "score": 84},
{"id":6, "name":"John7", "age":31, "city":"aspen", "score": 100},
{"id":7, "name":"John8", "age":31, "city":"aspen", "score": 93},
{"id":8, "name":"John9", "age":35, "city":"denver", "score": 93},
{"id":9, "name":"John10", "age":29, "city":"denver", "score": 75},
{"id":10, "name":"John11", "age":28, "city":"denver", "score": 85},
{"id":11, "name":"John12", "age":28, "city":"denver", "score": 85},
]
***FIRST USER SELECTED FILTER***
let firstFilter = [{"score":85}]
***FIRST FILTER RESULTS***
let firstFilterdResults = [
{"id":2, "name":"John3", "age":29, "city":"seattle", "score": 85},
{"id":4, "name":"John5", "age":24, "city":"austin", "score": 85},
{"id":10, "name":"John11", "age":28, "city":"denver", "score": 85},
{"id":11, "name":"John12", "age":28, "city":"denver", "score": 85},
]
***SECOND USER SELECTED FILTER***
let secondFilter = [{"age":28}]
***SECOND FILTER RESULTS***
let secondFilterdResults = [
{"id":10, "name":"John11", "age":28, "city":"denver", "score": 85},
{"id":11, "name":"John12", "age":28, "city":"denver", "score": 85},
]
***CURRENT ALGORITHM***
function filterDataList(data, firstFilter) {
let firstFilteredResults = [];
firstFilteredResults = data.filter((dataItem) => {
var throwVar = 0
for (let item of firstFilter) {
for (let key in item) {
if (dataItem[key] === undefined || dataItem[key] !== item[key]) {
throwVar = 0
} else {
return true
}
}
}
if (throwVar == 0) {
return false
}
})
return firstFilteredResults
}
uj5u.com熱心網友回復:
連續的過濾器選擇實際上指定了鍵和值的結合......
age == x AND score == y AND ....
就像一個物體!
由于用戶可以大概作出這些選擇不止一次,他們會產生這樣一個錯誤的過濾器:age == x AND age == y。
我們真的希望查詢是唯一鍵與值的結合。
就像一個物體!
// this expresses a conjunction of unique keys - values
let query = {}
let data = [...]
let filteredData = []
// add a key-value, pair and update the filtered data
function addFilter(key, value) {
query[key] = value;
const keys = Object.keys(query);
filteredData = data.filter(datum => {
return keys.every(key => datum[key] === query[key])
});
}
function reset() {
query = {}
}
uj5u.com熱心網友回復:
我認為您應該將過濾器作為陣列而不是物件傳遞:
function filter(data, filters){
let tmpData = data;
let result;
for(let filter of filters){
result = [];
for(let row of tmpData){
switch(filter[0]){
case 'id':
if(row.id === filter[1]) result.push(row);
break;
case 'name':
if(row.name === filter[1]) result.push(row);
break;
case 'age':
if(row.age === filter[1]) result.push(row);
break;
case 'city':
if(row.city === filter[1]) result.push(row);
break;
case 'score':
if(row.score=== filter[1]) result.push(row);
break;
}
}
tmpData = result;
}
return result;
}
//test:
let data = [
{"id":0, "name":"John1", "age":29, "city":"seattle", "score": 95},
{"id":1, "name":"John2", "age":25, "city":"seattle", "score": 95},
{"id":2, "name":"John3", "age":29, "city":"seattle", "score": 85},
{"id":3, "name":"John4", "age":30, "city":"austin", "score": 75},
{"id":4, "name":"John5", "age":24, "city":"austin", "score": 85},
{"id":5, "name":"John6", "age":30, "city":"aspen", "score": 84},
{"id":6, "name":"John7", "age":31, "city":"aspen", "score": 100},
{"id":7, "name":"John8", "age":31, "city":"aspen", "score": 93},
{"id":8, "name":"John9", "age":35, "city":"denver", "score": 93},
{"id":9, "name":"John10", "age":29, "city":"denver", "score": 75},
{"id":10, "name":"John11", "age":28, "city":"denver", "score": 85},
{"id":11, "name":"John12", "age":28, "city":"denver", "score": 85}
];
let result = filter(data, [['age', 29], ['city', 'seattle']]);
console.log(result);
uj5u.com熱心網友回復:
您可以迭代陣列并檢查每個物件是否滿足傳遞的過濾器。這適用于具有多個元素且每個元素具有多個屬性的過濾器。
let data = [
{ id: 0, name: 'John1', age: 29, city: 'seattle', score: 95 },
{ id: 1, name: 'John2', age: 25, city: 'seattle', score: 95 },
{ id: 2, name: 'John3', age: 29, city: 'seattle', score: 85 },
{ id: 3, name: 'John4', age: 30, city: 'austin', score: 75 },
{ id: 4, name: 'John5', age: 24, city: 'austin', score: 85 },
{ id: 5, name: 'John6', age: 30, city: 'aspen', score: 84 },
{ id: 6, name: 'John7', age: 31, city: 'aspen', score: 100 },
{ id: 7, name: 'John8', age: 31, city: 'aspen', score: 93 },
{ id: 8, name: 'John9', age: 35, city: 'denver', score: 93 },
{ id: 9, name: 'John10', age: 29, city: 'denver', score: 75 },
{ id: 10, name: 'John11', age: 28, city: 'denver', score: 85 },
{ id: 11, name: 'John12', age: 28, city: 'denver', score: 85 },
];
let filters = [{ score: 85 }, { age: 28, city: 'denver' }];
const filterArr = (array, filters) =>
array.filter((o) =>
filters.every((f) => Object.entries(f).every(([k, v]) => o[k] === v))
);
console.log(filterArr(data, filters));
您也可以filterArr根據需要多次呼叫。
let data = [
{ id: 0, name: 'John1', age: 29, city: 'seattle', score: 95 },
{ id: 1, name: 'John2', age: 25, city: 'seattle', score: 95 },
{ id: 2, name: 'John3', age: 29, city: 'seattle', score: 85 },
{ id: 3, name: 'John4', age: 30, city: 'austin', score: 75 },
{ id: 4, name: 'John5', age: 24, city: 'austin', score: 85 },
{ id: 5, name: 'John6', age: 30, city: 'aspen', score: 84 },
{ id: 6, name: 'John7', age: 31, city: 'aspen', score: 100 },
{ id: 7, name: 'John8', age: 31, city: 'aspen', score: 93 },
{ id: 8, name: 'John9', age: 35, city: 'denver', score: 93 },
{ id: 9, name: 'John10', age: 29, city: 'denver', score: 75 },
{ id: 10, name: 'John11', age: 28, city: 'denver', score: 85 },
{ id: 11, name: 'John12', age: 28, city: 'denver', score: 85 },
];
const filterArr = (array, filters) =>
array.filter((o) =>
filters.every((f) => Object.entries(f).every(([k, v]) => o[k] === v))
);
let firstFilter = [{"score":85}]
let filteredArr = filterArr(data, firstFilter);
let secondFilter = [{"age":28}]
filteredArr = filterArr(filteredArr, secondFilter)
console.log(filteredArr)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/400701.html
標籤:javascript 算法
上一篇:整數陣列的基于廣度級別的磁區方法
下一篇:最大公分母演算法背后的邏輯
