나는 반응 겨에서 이동하는 과정에서 자신이 너무 여러 번 요청했습니다, 이것은 (키를 구문 분석하는 lodash를 사용하여 값 쌍)의 해결책이 최대 끝난 날 위해 최선의 노력 : 당신이 예멘 아랍 공화국 응용 프로그램 구조의 어딘가에 해당 파일이 있으면
import _ from 'lodash';
function escapeRegexCharacters(str) {
//make sure our term won't cause the regex to evaluate it partially
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
var filterHelper = {
filterObjectArrayByTerm: function(arr, term) {
//make sure the term doesn't make regex not do what we want
const escapedTerm = escapeRegexCharacters(term.trim());
//make the regex do what we want
const regEx = new RegExp (escapedTerm, 'ig');
//if the term is blank or has no value, return the object array unfiltered
if(escapedTerm === '' || escapedTerm === null || escapedTerm === undefined) {
return arr;
}
//iterate over each object in the array and create a map
return arr.map((obj) => {
//if any value in a key:value pair matches the term regex
if(Object.values(obj).filter((value) => regEx.test(value)).length > 0){
//return the object in the map
return obj;
} else {
//otherwise put the object in as null so it doesn't display
return null;
}
});
},
//most similar to ng-repeat:ng-filter in ng
filterObjectArrayByKeyThenTerm: function(arr, key, term) {
//make sure the terms don't make regex not do what we want
const escapedKey = escapeRegexCharacters(key.trim());
const escapedTerm = escapeRegexCharacters(term.trim());
//make the regex do what we want
const keyRegex = new RegExp (escapedKey, 'ig');
const termRegex = new RegExp (escapedTerm, 'ig');
//if the key or term value is blank or has no value, return array
if(escapedTerm === '' || escapedTerm === null || escapedTerm === undefined || escapedKey === '' || escapedKey === null || escapedKey === undefined) {
return arr;
}
//iterate over each object in the aray passed in and create a map
return arr.map((obj) => {
//mapped object hasn't matched the regex yet
let match = false;
//can't .map() over key:value pairs, so _.each
_.each(obj, (value, key) => {
//if the key and value match the regex
if(keyRegex.test(key) && termRegex.test(value)) {
//set match to true
match = true;
}
});
//if match was set to true
if(match){
//return the object in the map
console.log('success');
return obj;
} else {
//otherwise put the object in as null so it doesn't display
return null;
}
});
},
};
module.exports = filterHelper;
, 당신은 이러한 기능 중 하나를 사용할 수 있습니다
를 호출하여
import 'filterHelper' from '/path/to/filterHelper/filterhelper.js'
다른 구성 요소에서 ng-repeat : ng-filter in 을 사용하면 원하는 값 또는 키 : 값 쌍으로 개체 배열을 필터링 할 수 있습니다.
작업 예제 (용어, 키 및 상태 값, 기타 등으로 설정된 arr)가 마음에 드시면 알려 주시기 바랍니다.
무거운 각도의 배경에서 나온 ReactJS는 Angular와 같은 본격적인 프레임 워크가 아니라 확실히 라이브러리라는 것을 알게되었습니다. 당신이 당연한 일로 코드를 작성해야 할 때가있을 것입니다. 이것은 분명히 나쁜 것이 아닙니다. 그냥 관찰. 어쨌든, 나는 이것을 수행하기 위해 npm에 케이크가 있다고 확신합니다. 영감을 얻기 위해 https://react.rocks/tag/Filter를 확인하십시오. –
John F. React와 100 % 일치하는 것은 모 놀리 식 프레임 워크가 아닌 사용자 영역 라이브러리입니다. 환상적으로 한 가지를하고 부팅 할 수있는 놀라운 커뮤니티가 있습니다. – Urasquirrel