简体   繁体   English

如何动态地将函数数组添加到 javascript 函数?

[英]How dynamic add an array of functions to an javascript function?

I do have this structure:我确实有这样的结构:

db.collection('users')

and want to make this:并希望做到这一点:

db.collection('users')
   .where('works', 'array-contains', 'CASE_A')
   .where('works', 'array-contains', 'CASE_B')
   .where('works', 'array-contains', 'CASE_C')
   ...

In this case, I will make an array of在这种情况下,我将制作一个数组
(( .where('works', 'array-contains', ${variable} )) (( .where('works', 'array-contains', ${variable} ))

and i will add this in the beginning db.collection('users'), how can i make this?我将在 db.collection('users') 的开头添加这个,我该怎么做?

Just make an inline array:只需创建一个内联数组:

db.collection('users')
   .where('works', 'array-contains', ['CASE_A', 'CASE_B', 'CASE_C'])

Or use a variable if you want:或者,如果需要,可以使用变量:

const array = ['CASE_A', 'CASE_B', 'CASE_C']
db.collection('users')
   .where('works', 'array-contains', array)

If you actually want to find documents where all cases are present (not where any of the cases are present), you won't be able to use an array-contains type query.如果您确实想查找所有案例都存在的文档(而不是任何案例都存在的地方),您将无法使用数组包含类型查询。 You will have to convert your array into an object and query it as shown in this question: Firestore: Multiple 'array-contains'您必须将数组转换为对象并按此问题所示进行查询: Firestore: Multiple 'array-contains'

Please try this code.请试试这个代码。

 const makeItem = value => `.where('works', 'array-contains',${value}` const arrFuncs = [ makeItem('CASE_A'), makeItem('CASE_B'), makeItem('CASE_C'), ] const initialCmd = `db.collection('users')` eval(initialCmd + arrFuncs.join(''))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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