그래서 두 개의 다른 배열을 포함하는 하나의 배열이 있습니다.
var _staticRoutingTable = [];
function StaticRoute(directory, extentions) {
this.dir = directory;
this.extentions = extentions;
}
_staticRoutingTable.push(new StaticRoute(["htmlfolder"], ["./html"]));
_staticRoutingTable.push(new StaticRoute(["somefolder"], ["./html","./txt","./css"]));
폴더 이름이 "somefolder"인 "dir"배열 만 가져오고 싶다고 가정 해 보겠습니다.
그래서 나는 이와 같은 smth를 원하지 않습니다.
return _staticRoutingTable.forEach(function callb(route) {
return route.dir.filter(function callb(directory) {directory=="somefolder" })
});
.... dir + 확장 배열을 얻습니다. 어떻게 하나의 배열 만 필터링 할 수 있습니까 (이 경우 "dir").
귀하의 질문을 올바르게 이해했는지 확실하지 않지만 같은 배열을 얻으려면 ['something']
find를 사용할 수 있습니다.
var _staticRoutingTable = [];
function StaticRoute(directory, extentions) {
this.dir = directory;
this.extentions = extentions;
}
_staticRoutingTable.push(new StaticRoute(["htmlfolder"], ["./html"]));
_staticRoutingTable.push(new StaticRoute(["somefolder"], ["./html","./txt","./css"]));
let foo = _staticRoutingTable.find(function (a) {
return a.dir.indexOf("somefolder") > -1;
});
if (foo) {
console.log(foo.dir);
}
이것은 첫 번째 일치 만 반환합니다. 관심있는 일치 항목이 여러 개있는 경우 필터를 전환하여 결과 배열을 찾아 작업 할 수 있습니다.
그러나 'somefolder'를 검색하고와 같은 배열을 반환 ['somefolder']
하려면 그냥하는 것이 더 쉬울 것입니다.
console.log(['somefolder']);
...
이것은 여러 일치에 대해 작동합니다.
var _staticRoutingTable = [];
function StaticRoute(directory, extentions) {
this.dir = directory;
this.extentions = extentions;
}
_staticRoutingTable.push(new StaticRoute(["htmlfolder"], ["./html"]));
_staticRoutingTable.push(new StaticRoute(["somefolder"], ["./html","./txt","./css"]));
let foo = _staticRoutingTable.filter(function (a) {
return a.dir.indexOf("somefolder") > -1;
});
foo.forEach(function (v) { console.log(v.dir); });
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다