【问题标题】:get all items that have match with other array获取与其他数组匹配的所有项目
【发布时间】:2021-02-09 01:02:02
【问题描述】:

我正在创建一个查询,但是当我不知道该怎么做时,我来到了一个部分。我有一个数组,例如有两个项目

//filter array
const filterArray=r.expr(['parking', 'pool'])

我还有一张表,上面有以下记录:

[
    {
        "properties": {
            "facilities": [
                "parking"
            ],
            "name": "Suba"
        }
    },
    {
        "properties": {
            "facilities": [
                "parking",
                "pool",
                "pet friendly"
            ],
            "name": "Kennedy",
        }
    },
    {
        "properties": {
            "facilities": [
                "parking",
                "pool"
            ],
            "name": "Soacha"
        }
    },
    {
        "properties": {
            "facilities": [
                "parking",
                "pet friendly",
                "GYM"
            ],
            "name": "Sta Librada"
        }
    },
]

我需要使用数组过滤记录,但我需要该记录具有数组过滤器的所有项目。如果记录有更多的数组过滤器项不是问题,我需要是否包含数组过滤器的所有项来获取该记录。在这种情况下,我需要所有具有“游泳池”和“停车场”设施的记录

当前查询

当前查询,但它也返回带有过滤器数组的一项或两项的记录

r.db('aucroom').table('hosts')
.filter(host=>
    host('properties')('facilities').contains(val=>{
        return filterArray.contains(val2=>val2.eq(val))

    })
)
.orderBy('properties')
.pluck(['properties'])

我希望等待的结果

如图片示例:

【问题讨论】:

    标签: javascript rethinkdb


    【解决方案1】:

    如果你想要两个数组的严格匹配(相同数量的元素,相同的顺序),那么使用.eq()

    array1.eq(array2)
    

    如果您希望第一个数组包含第二个数组的所有元素,则使用.setIntersection(),只需注意 array2 应该包含不同的元素(一组):

    array1.setIntersection(array2).eq(array2)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-12
      • 2018-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多