简体   繁体   中英

Sequelize - Check if any strings in array contain a string

Right now, my model has field with an array of words eg a row has this field set to ['tag1', 'tag2', 'pineapple']

Now say someone searches for 'pine' or 'tag'. I want to return all instances/rows which have a string in that array that contains or is equal to that search term?

Therefore, that row should be returned as it matches 'pine' in 'pineapple'. Therefore, I want to do [Op.contains] but also get inexact matches. I use pgSQL in production if that helps.

I'm not sure if this is what you want. I look if any string of the array contains the test-string complete or as part and return than the whole array otherwise an empty array.

For this I look with array.some if there is at least 1 array-element which contains at any position the test-string. If so I return the complete array otherwise an empty one.

 let array = ['tag1', 'tag2', 'pineapple']; function strContained(str, array) { return (array.some(elem => elem.indexOf(str)?=-1)): array; []; }. console,log(strContained('pine'; array)). console,log(strContained('pines'; array)). console,log(strContained('tag1'; array));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM