{
{
id: 125,
apartments: [
{
floor: 28,
},
{
floor: 29,
}
],
},
{
id: 126,
apartments: [
{
floor: 25,
},
{
floor: 29,
}
],
},
....
}
It is sample collection data.
Now I want to filter this collection by floor attribute. For example, I want to get only floor == 29
I want to get all the data that including floor == 29.
How can I filter it?
Related
Having an observable emitting a list of users with the next content:
[
{
"id": 1,
"name": "John",
"status": "Active"
},
{
"id": 2,
"name": "Mary",
"status": "Inactive"
},
{
"id": 3,
"name": "Peter",
"status": "Inactive"
},
{
"id": 4,
"name": "Susan",
"status": "Active"
}
]
And I have another observable returning the extended user data:
{
"id": 1,
"authorizations: 20
}
I use the detail of each user in an specific details page, but I would like to combine part of the detail in the users list and obtain the next result and only filter by the status Active:
[
{
"id": 1,
"name": "John",
"status": "Active",
"authorizations": 20
},
{
"id": 4,
"name": "Susan",
"status": "Active",
"authorizations": 10
}
]
It is possible to use some filtering operator and combine those results without use two subscriptions?
Tried the following code but, would be a better or simplified way to do it?
import { of, Observable, combineLatest } from 'rxjs';
import { filter, map, mergeAll, mergeMap } from 'rxjs/operators';
type State = 'Active' | 'Inactive';
type User = { id: number; name: string; status: State };
type UserDetail = { id: number; authorizations: number };
type UserWithAuthorizations = User & UserDetail
const users: User[] = [
{
"id": 1,
"name": "John",
"status": "Active"
},
{
"id": 2,
"name": "Mary",
"status": "Inactive"
},
{
"id": 3,
"name": "Peter",
"status": "Inactive"
},
{
"id": 4,
"name": "Susan",
"status": "Active"
}
]
const authorizations: UserDetail[] = [
{ id: 1, authorizations: 20 },
{ id: 2, authorizations: 5 },
{ id: 3, authorizations: 30 },
{ id: 4, authorizations: 10 },
];
const getAuthorizationsByUser= (userId: number): Observable<Partial<UserWithAuthorizations>> => {
const users$ = of(users)
const authorizations$ = of(authorizations)
return combineLatest([users$, authorizations$]).pipe(
map(res => {
const user = res[0].find(u => u.id === userId)
const { authorizations } = res[1].find(a => a.id === userId)
return {
...user,
authorizations
}
}))
};
const fetchUsersWithAuthorizations = () => of(users);
fetchUsersWithAuthorizations()
.pipe(
mergeAll<User>(),
filter((user) => user.status === "Active"),
mergeMap((user) => getAuthorizationsByUser(user.id))
)
.subscribe(console.log);
Why not do it all in a single combine latest?
const { of, map, combineLatest } = rxjs;
const users = [
{
"id": 1,
"name": "John",
"status": "Active"
},
{
"id": 2,
"name": "Mary",
"status": "Inactive"
},
{
"id": 3,
"name": "Peter",
"status": "Inactive"
},
{
"id": 4,
"name": "Susan",
"status": "Active"
}
]
const authorizations = [
{ id: 1, authorizations: 20 },
{ id: 2, authorizations: 5 },
{ id: 3, authorizations: 30 },
{ id: 4, authorizations: 10 },
];
const users$ = of(users)
const authorizations$ = of(authorizations)
const activeUsersWithAuthorizations$ = combineLatest([users$, authorizations$]).pipe(
map(([users, authorizations]) =>
users
.filter((user) => user.status === 'Active')
.map((user) => ({
...user,
authorizations: authorizations.find((a) => a.id === user.id)?.authorizations,
}))
)
);
activeUsersWithAuthorizations$.subscribe(activeUsersWithAuthorizations => {
console.log(activeUsersWithAuthorizations);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/7.8.0/rxjs.umd.min.js" integrity="sha512-v0/YVjBcbjLN6scjmmJN+h86koeB7JhY4/2YeyA5l+rTdtKLv0VbDBNJ32rxJpsaW1QGMd1Z16lsLOSGI38Rbg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
My goal is to normalize this object:
{
talks: [
{
id: 1755,
speakers: [
{
id: 1487,
name: 'John Doe',
},
],
related_talks: [{
id: 14,
speakers: [{
id: 125,
name: 'Jane Doe',
}],
event: {
id: 181,
name: 'First Annual',
},
}],
event: {
id: 180,
name: 'July Party',
},
},
],
};
into this result:
{
entities: {
events: {
181: {
id: 181,
name: 'First Annual'
},
180: {
id: 180,
name: 'July Party'
}
},
speakers: {
125: {
id: 125,
name: 'Jane Doe'
},
1487: {
id: 1487,
name: 'John Doe'
}
},
talks: {
1755: {
id: 1755,
event: 181,
speakers: [ 1487 ],
related_talks: [ 14 ],
},
14: {
id: 14,
speakers: [ 125 ],
event: 180,
}
},
},
result: {
talks: [ 1755, 14 ],
},
}
If you'll notice, the items in related_talks are treated the same as a talk.
My schemas follow the examples and are set up like this:
const speaker = new schema.Entity('speakers');
const event = new schema.Entity('events');
export const talk = new schema.Entity('talks', {
speakers: [speaker],
event,
});
talk.define({ related_talks: [talk] });
No matter what I try, I can't get the items in related_talks to be added to the result.talks array. It is, however, in the entities object.
What is my schema configuration missing in order to accommodate this?
Unfortunately, if this is your requirement, Normalizr is not for you. Alternatively, if you're looking for a list of "talks" by ID, you can use Object.keys(data.entities.talks)
Given an input that looks like this:
[
{
id: 11,
valueId: 22,
valueDescription: 'Some value',
referenceId: 33,
referenceDescription: 'Some reference',
groupId: 44,
groupDescription: 'Some group'
},
{
id: 55,
valueId: 66,
valueDescription: 'Another value',
referenceId: 77,
referenceDescription: 'Another reference',
groupId: 88,
groupDescription: 'Another group'
}
]
And a desired output of:
{
entities: {
types: {
"11": { id: 11, valueId: 22, referenceId: 33, groupId: 44 },
"55": { id: 55, valueId: 66, referenceId: 77, groupId: 88 },
},
values: {
"22": { id: 22, description: "Some value" },
"66": { id: 66, description: "Another value" },
},
references: {
"33": { id: 33, description: "Some reference" },
"77": { id: 77, description: "Another reference" },
},
groups: {
"44": { id: 44, description: "Some group" },
"88": { id: 88, description: "Another group" },
}
},
result: [ 11, 55 ]
}
I'm not sure how I'd define Schemas for my 4 entity types that would pull multiple fields off of the flattened root object and also rename them. I see there's the assignEntity argument I can pass which I think I would use for the renaming part, but I'm not sure how I would define a Schema to indicate that one flat object becomes four entities.
I have a JSON array and want to make a chart with C3.js. My example gives me only one line. I want to display however one line for every user. How can I achieve this?
Here is my code:
this.chart = c3.generate({
data: {
json: [
{
ExamID: 'Exam1',
result: 80,
user:"user1"
},
{
ExamID: 'Exam2',
result: 90,
user:"user1"
},
{
ExamID: 'Exam1',
result: 70,
user:"user2"
},
{
ExamID: 'Exam2',
result: 60,
user:"user2"
}
],
keys: {
x: 'ExamID',
value: ['result'],
},
},
axis: {
x: {
type: 'category',
}
}
});
You need to have a different JSON structure. Every object in the array (every exam) needs to have the results of all students in it. You can use a key for every student and rename the key for the legend with the attribute names. Here is a Fiddle: https://jsfiddle.net/b5pd3wn6/
var chart = c3.generate({
bindto: "#element",
data: {
json: [
{
ExamID: 'Exam1',
user1: 80,
user2: 50
}, {
ExamID: 'Exam2',
user1: 90,
user2: 40
}
],
keys: {
x: 'ExamID',
value: ['user1', 'user2']
},
names: {
'user1': 'Peter Miller',
'user2': 'Vladimir Peterhans'
}
},
axis: {
x: {
type: 'category'
}
}
});
Hi I am trying to make a Kendo UI radar chart. I want to know the correct format in order to display the data.
{
new {year = year, thisyear = new {satisfaction = pq1, organisation=pq2, expecations=pq3, teaching=pq3, consistent=pq4} } //cpe
};
Response.Cache.SetCacheability(HttpCacheability.ServerAndPrivate);
Response.Cache.SetMaxAge(new TimeSpan(24 * 31, 0, 0));
return Json(radata, JsonRequestBehavior.AllowGet);ยจ
Is this correct?
Your question is a little short on details, but if I understand correctly you could organize your data like this:
var data = [
{
"Criteria": "satisfaction ",
"Y2015": 5,
"Y2014": 8
},
{
"Criteria": "organisation",
"Y2015": 8,
"Y2014": 7
},
{
"Criteria": "expecations",
"Y2015": 6,
"Y2014": 9
},
{
"Criteria": "teaching",
"Y2015": 7,
"Y2014": 7
},
{
"Criteria": "consistent",
"Y2015": 5,
"Y2014": 9
},
]
$("#chart").kendoChart({
title: {
text: "Survey"
},
dataSource: data,
seriesDefaults: {
type: "radarLine",
style: "smooth"
},
series: [{
name: "2015",
field: "Y2015"
}, {
name: "2014",
field: "Y2014"
}],
categoryAxis: {
field: "Criteria"
},
valueAxis: {
max: 10
},
theme: "Fiori"
});
DEMO