반응형
필터를 사용하여 배열에서 개체의 여러 키 값을 검색하는 방법
각 와인마다 데이터가 있는 물체가 들어 있는 와인이 여러 개 있다.
var wines = [
{ _id: '59a740b8aa06e549918b1fda',
wineryName: 'Some Winery',
wineName: 'Pinot Noir',
wineColor: 'Red',
imageLink: '/img/FortBerensPN.png' },
{ _id: '59a7410aaa06e549918b1fdb',
wineryName: 'Some Winery',
wineName: 'Pinot Gris',
wineColor: 'White',
imageLink: '/img/FortBerensPG.png' },
{ _id: '59a74125aa06e549918b1fdc',
wineryName: 'Some Winery',
wineName: 'Rose',
wineColor: 'Rose',
imageLink: '/img/FortBerensRose.png' },
{ _id: '59a74159aa06e549918b1fdd',
wineryName: 'Some other Winery',
wineName: 'Rose',
wineColor: 'Rose',
imageLink: '/img/FortBerensRose.png' },
{ _id: '59a7417aaa06e549918b1fde',
wineryName: 'Some other Winery',
wineName: 'Pinot Gris',
wineColor: 'White',
imageLink: '/img/FortBerensPG.png' },
{ _id: '59a8721f4fd43b676a1f5f0d',
wineryName: 'Some other Winery',
wineName: 'Pinot Gris',
wineColor: 'White',
imageLink: '/img/FortBerensPG.png' },
{ _id: '59a872244fd43b676a1f5f0e',
wineryName: 'Winery 3',
wineName: 'Pinot Noir',
wineColor: 'Red',
imageLink: '/img/FortBerensPN.png' } ]
와인 객체를 검색하는 방법(대소문자 구분 없음)은 다음과 같이 검색할 객체의 키를 지정하면서,
var search = 'Noir'
filteredWines = function () {
return wines.filter(function(wine){
return (wine.wineName.toLowerCase().indexOf(search.toLowerCase())>=0;
});
};
반환:
[ { _id: '59a740b8aa06e549918b1fda',
wineryName: 'Some Winery',
wineName: 'Pinot Noir',
wineColor: 'Red',
imageLink: '/img/FortBerensPN.png' },
{ _id: '59a872244fd43b676a1f5f0e',
wineryName: 'Winery 3',
wineName: 'Pinot Noir',
wineColor: 'Red',
imageLink: '/img/FortBerensPN.png' } ]
그러나, 만약var search = 'Winery 3'
또는var search = 'red'
그러면 그것은 분명히 어떤 결과도 돌려주지 않을 것이다, 왜냐하면 그것은 그것의 가치를 들여다보고 있기 때문이다.wineName
배열의 각 객체의.
그렇다면 필터(또는 다른 방법?)를 사용하여 모든 키 값 또는 더 나은 여러 개의 지정된 키 값을 검색하고 일치하는 개체의 배열을 반환하는 방법이 있는가?
다음과 같은 경우:
filteredWines = function () {
return wines.filter(function(wine){
return ((wine.wineName.toLowerCase() && wine.wineName.toLowerCase()
&& wine.wineName.toLowerCase()).indexOf(search.toLowerCase())>=0;
});
};
아니면 내가 완전히 잘못 짚고 있는 것일까?
PS. 나는 Vue.js 2를 사용하고 있어서 Vue 안에 더 좋은 방법이 있다면 나는 귀를 기울이고 있어!
문자열의 모든 속성을 스캔하는 보다 일반적인 함수를 가질 수 있다.로 모든 속성 값 반복Object.values()
사용하다some
성냥을 걸자마자 탈출하기 위해:
filteredWines = function (search) {
var lowSearch = search.toLowerCase();
return wines.filter(wine =>
Object.values(wine).some(val =>
String(val).toLowerCase().includes(lowSearch)
)
);
}
검색할 특정 키를 전달하려는 경우:
filteredWines = function (search, keys) {
var lowSearch = search.toLowerCase();
return wines.filter(wine =>
keys.some(key =>
String(wine[key]).toLowerCase().includes(lowSearch)
)
);
}
으로 부르다.
filteredWines('Winery 3', ['wineryName', 'wineName']);
다음과 같은 방법으로도 수행할 수 있다.
this.wines = this.wines.filter((item) => {
return (item.wineryName.toString().toLowerCase().indexOf(val.toLowerCase()) > -1 ||
item.wineName.toLowerCase().indexOf(val.toLowerCase()) > -1 ||
item.wineColor.toLowerCase().indexOf(val.toLowerCase()) > -1);
})
"트렌코트"의 용액을 사용하여 각도5를 적용하기 위해 다음과 같이 변경:
filter(search, list): Observable<IFilteredList> {
return list.filter(item => {
return Object.values(item).some(val =>
String(val).includes(search)
);
})
}
필터가 작동한다.아이고, 나는 그 질문을 조금 더 자세히 읽었다.필터는 여전히 작동하지만 값도 필터링해야 한다.
let wines = [
{
_id: '59a740b8aa06e549918b1fda',
wineryName: 'Some Winery',
wineName: 'Pinot Noir',
wineColor: 'Red',
imageLink: '/img/FortBerensPN.png'
},
{
_id: '59a7410aaa06e549918b1fdb',
wineryName: 'Some Winery',
wineName: 'Pinot Gris',
wineColor: 'White',
imageLink: '/img/FortBerensPG.png'
},
{
_id: '59a74125aa06e549918b1fdc',
wineryName: 'Some Winery',
wineName: 'Rose',
wineColor: 'Rose',
imageLink: '/img/FortBerensRose.png'
},
{
_id: '59a74159aa06e549918b1fdd',
wineryName: 'Some other Winery',
wineName: 'Rose',
wineColor: 'Rose',
imageLink: '/img/FortBerensRose.png'
},
{
_id: '59a7417aaa06e549918b1fde',
wineryName: 'Some other Winery',
wineName: 'Pinot Gris',
wineColor: 'White',
imageLink: '/img/FortBerensPG.png'
},
{
_id: '59a8721f4fd43b676a1f5f0d',
wineryName: 'Some other Winery',
wineName: 'Pinot Gris',
wineColor: 'White',
imageLink: '/img/FortBerensPG.png'
},
{
_id: '59a872244fd43b676a1f5f0e',
wineryName: 'Winery 3',
wineName: 'Pinot Noir',
wineColor: 'Red',
imageLink: '/img/FortBerensPN.png'
}
];
let search = (val) => wines.filter(w => Object.values(w).filter(v => v.toLowerCase().indexOf(val.toLowerCase()) !== -1).length > 0);
console.log(search('some'));
또한 좀 더 일반적인 접근법을 시도해 볼 것을 권하고 싶다.
function getMatchingWhine(keys, searchTerm, wines) {
function extractTextFromKeys(keys, object) {
let text = '';
keys.forEach(key => {
text += ' ' + object[key];
});
return text.toLowerCase();
}
return wines.filter(wine => {
const relevantText = extractTextFromKeys(keys, wine);
return relevantText.includes(searchTerm.toLowerCase());
});
}
반응형
'Programing' 카테고리의 다른 글
자바에서 더블틸드(~~)의 의미는 무엇인가? (0) | 2022.04.19 |
---|---|
VueJS 라이프사이클에서 소품 업데이트 시기 (0) | 2022.04.19 |
coudamalloc()의 사용.왜 이중 포인터가 되는 거지? (0) | 2022.04.19 |
Vue: 사용자 지정 확인란 구성 요소에서 v-model과의 바인딩이 작동하지 않음 (0) | 2022.04.19 |
Vuejs가 어레이에서 데이터를 가져올 수 없음 (0) | 2022.04.19 |