【问题标题】:Find In array TypeOrm在数组 TypeOrm 中查找
【发布时间】:2021-03-17 05:06:56
【问题描述】:

让我们看看你能不能帮助我,因为我已经这样好几个小时了,我不能。 我正在用 Typeorm 做一些查询,情况是这样的:

我有一个 ID 数组:["array1, array1, array3"]。也就是说,在数组中,一个 id 重复了 2 次。

问题是,如果我进行这样的查询:

  const found = await this.find ({
             where: {id: In (Ids)},
         })

发现我拥有的是:

[ 
  Catalog {
    id: 'array1',
    image: 'https://image.jpg',
    name: 'Article name',
    description: 'Description',
    price: 809,
    createdAt: 2020-12-04T17:34:16.869Z,
    updatedAt: 2020-12-04T17:34:16.869Z },
  Catalog {
    id: 'array3',
    image: 'https://image.jpg',
    name: 'Article name',
    description: 'Description',
    price: 809,
    createdAt: 2020-12-04T17:34:16.869Z,
    updatedAt: 2020-12-04T17:34:16.869Z }
]

而我需要的是:

[ 
  Catalog {
    id: 'array1',
    image: 'https://image.jpg',
    name: 'Article name',
    description: 'Description',
    price: 809,
    createdAt: 2020-12-04T17:34:16.869Z,
    updatedAt: 2020-12-04T17:34:16.869Z },
  Catalog {
    id: 'array1',
    image: 'https://image.jpg',
    name: 'Article name',
    description: 'Description',
    price: 809,
    createdAt: 2020-12-04T17:34:16.869Z,
    updatedAt: 2020-12-04T17:34:16.869Z },
  Catalog {
    id: 'array3',
    image: 'https://image.jpg',
    name: 'Article name',
    description: 'Description',
    price: 809,
    createdAt: 2020-12-04T17:34:16.869Z,
    updatedAt: 2020-12-04T17:34:16.869Z }
]

另一种选择是接下来执行一个方法,接收 ID 数组和对象的参数,迭代以获得我需要的对象数组......但我也没有找到密钥。

感谢您的帮助

【问题讨论】:

    标签: nestjs typeorm


    【解决方案1】:

    最后,我这样做了:

            const order: CatalogDto[] = []
            for (let i = 0; i < Ids.length; i++) {
                const found: CatalogDto = uniqueProduct.find(product=> product.id == Ids[i])
                order.push(found)
            }
    

    uniqueProduct 在哪里:

    [ 
      Catalog {
        id: 'array1',
        image: 'https://image.jpg',
        name: 'Article name',
        description: 'Description',
        price: 809,
        createdAt: 2020-12-04T17:34:16.869Z,
        updatedAt: 2020-12-04T17:34:16.869Z },
      Catalog {
        id: 'array3',
        image: 'https://image.jpg',
        name: 'Article name',
        description: 'Description',
        price: 809,
        createdAt: 2020-12-04T17:34:16.869Z,
        updatedAt: 2020-12-04T17:34:16.869Z }
    ]
    

    按顺序我有:

    [ 
      Catalog {
        id: 'array1',
        image: 'https://image.jpg',
        name: 'Article name',
        description: 'Description',
        price: 809,
        createdAt: 2020-12-04T17:34:16.869Z,
        updatedAt: 2020-12-04T17:34:16.869Z },
      Catalog {
        id: 'array1',
        image: 'https://image.jpg',
        name: 'Article name',
        description: 'Description',
        price: 809,
        createdAt: 2020-12-04T17:34:16.869Z,
        updatedAt: 2020-12-04T17:34:16.869Z },
      Catalog {
        id: 'array3',
        image: 'https://image.jpg',
        name: 'Article name',
        description: 'Description',
        price: 809,
        createdAt: 2020-12-04T17:34:16.869Z,
        updatedAt: 2020-12-04T17:34:16.869Z }
    ]
    

    【讨论】:

      猜你喜欢
      • 2021-07-20
      • 2021-04-26
      • 2021-01-20
      • 2020-02-16
      • 2021-01-29
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多