D
D
Dmitry2016-06-04 16:47:41
API
Dmitry, 2016-06-04 16:47:41

How to search in Angular for related objects in the form of their url?

There are 2 models
Tickets

[
  {
    "id": 1,
    "client_url": "http://127.0.0.1:8000/api/v1/clients/1/",
    "url": "http://127.0.0.1:8000/api/v1/tickets/1/"
  },
  {
    "id": 2,
    "client_url": "http://127.0.0.1:8000/api/v1/clients/2/",
    "url": "http://127.0.0.1:8000/api/v1/tickets/2/"
  },
  {
    "id": 3,
    "client_url": "http://127.0.0.1:8000/api/v1/clients/2/",
    "url": "http://127.0.0.1:8000/api/v1/tickets/3/"
  }
]

Clients
[
  {
    "id": 1,
    "full_name": "Петров Иван Сергеевич",
    "birthdate": "1992-04-22",
    "url": "http://127.0.0.1:8000/api/v1/clients/1/"
  },
  {
    "id": 1,
    "full_name": "Иванов Иван Иванович",
    "birthdate": "1992-04-21",
    "url": "http://127.0.0.1:8000/api/v1/clients/2/"
  }
]

So we need to find all the tickets owned by customers who have "Petrov" in their full_name.
Can you share the best way to do this? Maybe there are already solutions.
Thanks to everyone who responded!!!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dasha Tsiklauri, 2016-06-04
@pyHammer

var clientUrls = _(clients).filter(function(i){
return i.full_name.indexOf('Петров')>=0;
})
.map(function(i){
return i.url;
}).value();
var myTickets = _.filter(tickets, function(t){
return _.some(clientUrls, function(i){return i===t.client_url;});
});

documentation on lodash , as an optimization, clientUrls can be stored not as an array, but as objects, checking by key will be faster.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question