I want to filter an array of objects by another object, and show them if they have as a property either one of the second object's entries.
My array of objects to filter:
accounts = [
0: {id: 24, name: 'name24', company: company1, location:location1, ...}
1: {id: 25, name: 'name25', company: company2, location:location1, ...}
2: {id: 26, name: 'name26', company: company1, location:location2, ...}
3: {id: 27, name: 'name27', company: company3, location:location1, ...}
4: {id: 28, name: 'name28', company: company2, location:location3, ...}
..................................................
]
My filtering object:
companies = {
company1: 1,
company2: 0,
company3: 1,
............
}
In my companies object, I have all the companies with a value of 1 for selected and 0 for not selected.
I already have a filter for the location, but data.locations contains only one entry.
<div class="description">
<div ng-repeat="accounts in data.accounts | filter:{location_id: data.location.id">....</div>
</div>
More clearly, I want to filter and get the accounts that belong to companies which have the value of 1 in my companies object if it's possible with a method like the one I use for filtering the location, without creating a custom filter.
Related
Going to the point, I'm creating a table component that receives an Object containing all information that the dynamic table needs, including head information and so on. My mission is to make it possible for every row (data), my table doesn't need to know the property name, only list its value straight away.
I did some console tests and it seems to work around those lines:
const dataList = [
{ name: 'Eugene', age: 20, alive: true },
{ name: 'Game Master', age: 40, alive: false },
{ name: 'Camel', age: 22, alive: true }
];
for(let data of dataList) {
console.log("All data info");
console.log(data);
for(let propertyValue of Object.values(data)) {
console.log(propertyValue);
}
}
The Results:
VM1123:2 All data info VM1123:3 {name: 'Eugene', age: 20, alive:
true} VM1123:6 Eugene VM1123:6 20 VM1123:6 true
VM1123:2 All data info VM1123:3 {name: 'Game Master', age: 40,
alive: false} VM1123:6 Game Master VM1123:6 40 VM1123:6
false VM1123:2 All data info VM1123:3 {name: 'Camel', age:
22, alive: true} VM1123:6 Camel VM1123:6 22 VM1123:6
true
I'm trying to achieve the same result, but this time iterating between ngFor directive, like below:
<tr *ngFor="let data of tableConfig.data; let i = index">
<td *ngFor="let propValue of Object.values(data)"> {{ propValue }}</td>
But, the problem is that I can't access the 'Object' class inside the component HTML.
Property 'Object' does not exist on type 'PaginationTableComponent'.
Add method to get your object values like
getValues(data): any{
return Object.values(data);
}
In template :
<td *ngFor="let propValue of getValues(data)"> {{ propValue }}</td>
Demo
I'm building a fake API with json-server and I came across a problem when passing a query. I can show the parameters of a relationship when I use _expand, but when it is a relationship within a child it does not work.
For example, I have this json that I access by passinghttp://localhost:3000/schedules:
{
id: 1,
date: "2020-04-25T14:20:00-03:00",
status: "Novo serviço",
usersId: 5,
providersId: 1,
servicesId: 1,
},
Now to show the relationship with the user I pass the following query:
http://localhost:3000/schedules?_expand=users
It returns as follows:
{
id: 1,
date: "2020-04-25T14:20:00-03:00",
status: "Novo serviço",
usersId: 5,
providersId: 1,
servicesId: 1,
users: {
id: 5,
name: "Carla Pedroso",
photo: "https://cdn.pixabay.com/photo/2016/01/19/17/48/woman-1149911_960_720.jpg",
rating: 5,
addressesId: 1,
},
},
Well, my question is how I can show the JSON of the addressesId, because I already tried using_expand but without success.
change the schedule property "usersId" to "userId",
should work with
http://localhost:3000/schedules?_expand=user
and the response should be something like that:
{
id: 1,
date: "2020-04-25T14:20:00-03:00",
status: "Novo serviço",
providersId: 1,
servicesId: 1,
userId: 5,
user: {
id: 5,
name: "Carla Pedroso",
photo: "https://cdn.pixabay.com/photo/2016/01/19/17/48/woman-1149911_960_720.jpg",
rating: 5,
addressesId: 1
}
}
With json-server you can add the parent and children of an element to the result, but it will only bring one level, so it will not bring grandparents or grandchildren's.
In your case instead of looking at the schedules you can look at the users and ask to expand the addresses and embed the schedules
http://localhost:3000/users?_expand=addresses&_embed=schedules
I have a model City:
class City
belongs_to :country
end
And a model Street:
//street has an attribute `Type` which can be 1, 2 or 3
class Street
belongs_to City
end
I want all cities in Croatia including streets that are of type 2
So something like this:
cities = City.find_by_country("Croatie").include_streets_where(type: 2)
so I get something like this:
[
{
name: "Zagreb",
country: "Croatia",
streets: [{name: "street1", type: 2},{name: "street2", type: 2}]
},
{
name: "Split",
country: "Croatia",
streets: [{name: "street3", type: 2},{name: "street4", type: 2}]
}
]
My solution is to first get the cities by country name and loop through each city to query it's streets. But I'm guessing there is a better way.
I'm assuming your City has_many :streets, and your Country class has an attribute name.
A 2-layered loop is not as efficient as an INNER JOIN, which you can assemble with this: (You can look at the SQL it generates by appending .to_sql to the end of it.)
cities = City.where(country: Country.find_by_name("Croatie"))
.joins(:streets).where(streets: { type: 2 })
This will return a list of city objects matching your criteria. Now to get it to the format you specified, you have to do some formatting on the Ruby side since the default returned is not an Array type. This is assuming you want an array of hashes.
formatted_list = cities.map do |city|
{ name: city.name,
country: city.country.name,
streets: list_of_streets_with_type(city.streets) }
end
def list_of_streets_with_type(streets)
streets.map do |street|
{ name: street.name,
type: street.type }
end
end
In the end, formatted_list would be returning what you wanted.
(Disclaimer: I have not checked syntax, but the general idea is there. Give it a try, it should work)
I have the following JSON Object, which is the result of a loopback model (Classifications), with a relationship with another model (Labels).
My call to get the classifications is:
modClassification.findOne({
where: { id: classificationid },
include: 'labels' },
function( err, classification ){ ...
And this returns classification with something like
{ id: 'b01',
title: 'population',
country_id: 1,
labels:
[ { column_id: 'b1',
classification_id: 'b01',
classification_title: 'population',
dsporder: 1,
label: 'Total_Persons_Males',
country_id: 1,
id: 1 },
{ column_id: 'b2',
classification_id: 'b01',
classification_title: 'population',
dsporder: 2,
label: 'Total_Persons_Females',
country_id: 1,
id: 2 } ] }
which is what I would expect.
I now need to loop over the labels and access it's properties, but this is where I am stuck.
classification.labels[0] = undefined..
I have tried looping, each and whatever I can find online, but can't seem to get to each labels properties.
Can someone tell me what I am doing wrong/need to do?
Thanks
When you are including related models inside a findOne call, you need to JSONify the result before accessing the related records:
classification = classification.toJSON()
Then you should be able to access the included label items as you expect.
See https://docs.strongloop.com/display/public/LB/Include+filter, specifically the "Access included objects" section.
Note this does not work the same when you retrieve more than one result in an array. In that case you'll need to perform toJSON() on each item in the array.
Collection: years
Entry 1
{
_id: ObjectId("4f2fd2ee4516ed6ceebbe17c"),
shows: [
{
day: 1,
hasVideos: false,
month: 1,
showid: 1252187000,
venue: "Someplace"
},
{
day: 2,
hasVideos: false,
month: 2,
showid: 1252188010,
venue: "Philly"
}
],
year: 1988
}
Entry 2
{
_id: ObjectId("4f2fd2ee4516ed6ceebbe17c"),
shows: [
{
day: 19,
hasVideos: false,
month: 1,
showid: 1252187999,
venue: "Hunt's"
},
{
day: 21,
hasVideos: false,
month: 1,
showid: 1252188030,
venue: "Hunt's"
}
],
year: 1987
}
Now, I created a set called years in my redis store. This is populated with strings in the form of year:1988. I created a hash called year:1988. But this is wrong. I want to nest an array of these show hashes.
How can nest a hash two sets deep? Should I just continue the pattern and create a set in the form of 1988:01:01 and 1988:02:02 to store the first entry?
Thanks
If you only want to be able to query by date (or year) you should use a Sorted Set as lookup.
Sorted Sets on redis.io
What you want to do is use the show's Id or the serialized show as the member and the date the show aired, or took place as the score.
Note that if you only store the shows Id you need to perform another lookup to get the shows' details.
Now you can perform range queries which seems to be what you want:
ZRANGEBYSCORE
ZREVRANGEBYSCORE