0
votes

Let's see if you can help me, because I've been like this for a few hours and I can't. I'm doing some queries with Typeorm, and this is the situation:

I have an array of Ids: ["array1, array1, array3"]. that is, in the array an id is repeated 2 times.

The thing is that if I make a query such that:

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

In found what I have is:

[ 
  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 }
]

And that I need is:

[ 
  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 }
]

Another option would be to do next a method that receiving the parameters of the ID array and the object, iterate to obtain the array of objects that I need ... but I have not found the key either.

Thanks for you help

1

1 Answers

0
votes

Finally, i do this:

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

where uniqueProduct is :

[ 
  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 }
]

and in order I have:

[ 
  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 }
]