2017-09-22 5 views
1

두 개의 목록이 있습니다. 첫 x 째는 값 목록이고 other는 임의의. 자 열에이 값이 들어갈 수있는. 자열 목록입니다. 그래서 wordList를 반복하고 내부 루프에서 값을 반복 할 때 문자열에 값이 있으면 값을 반환해야합니다.lodash 모듈을 사용하여 Node.js에서 중첩 루프를 반복하고 일치하는 값을 반환하지 않습니다.

const _ = require('lodash'); 
const valueList = ['abb','bcd','ghi']; 

const wordList = ['ab','a','abc','abcde','bcef','aghif']; 

const selectedValue = _.filter(wordList, (word) => { 
    return _.filter(valueList, (value) => { 
    return _.includes(word,value); 
    }); 
}); 

console.log(`Printing matched value ${selectedValue}`); 
// Output should be bcd as 'bcd' as wordList contains this value and also it is first match. 

답변

1

아마도 로다시가 필요하지 않습니다. 일반 자바 스크립트도 할 수 있습니다

const valueList = ['abb','bcd','ghi']; 
 

 
const wordList = ['ab','a','abc','abcde','bcef','aghif']; 
 

 
const selectedValue = valueList.find(val => wordList.some(word=>word.includes(val))); 
 

 
console.log(`Printing matched value ${selectedValue}`); 
 
// Output should be bcd as 'bcd' as wordList contains this value and also it is first match.

0

작동 코드가 lodash 사용.

const _ = require('lodash'); 
 

 
const valueList = ['abb','bcd','ghi']; 
 

 
const wordList = ['ab','a','abc','abcde','bcef','aghif']; 
 

 
const selectedValue = _.filter(valueList, (value) => { 
 
    return _.some(wordList, (word) => { 
 
    return _.includes(word,value); 
 
    }); 
 
}); 
 

 
console.log(`Printing matched value ${selectedValue}`);

+0

당신의 조각은 BCD, GHI를 반환 –

+0

작동하지 않습니다 – jyoti