i am having an observable which returns something like this:
"#odata.context": "here is the context URL",
"value": [
{
"Id": 1,
"Value": "A"
},
{
"Id": 2,
"Value": "B"
},
{
"Id": 3,
"Value": "C"
},
{
"Id": 4,
"Value": "A"
}
]
}
Using RxJS i would like to get into the "value" property and use distinct on it to limit the response to something like this:
"#odata.context": "here is the context URL",
"value": [
{
"Id": 1,
"Value": "A"
},
{
"Id": 2,
"Value": "B"
},
{
"Id": 3,
"Value": "C"
}
]
}
could you help me with it?
It pretty much depends on how exactly you want to use this RxJS chain but I hope this leads you the right direction:
const source = of(data)
.pipe(
mergeMap(d => d.value),
distinct(v => v.Value),
toArray(),
)
.subscribe(console.log);
mergeMap will reiterate the array of values as separate emissions where distinct will ignore duplicate Values. Then toArray() will just collect all results.
Live demo: https://stackblitz.com/edit/rxjs-is1zp4
Related
while a user A is editing the permissions of user B, user A needs to see both the permissions it has and the permissions that user B has. For this, I thought of something like this and added something like and it gives me a nice output yes!
Controller:
PermissionCategoryResource::collection(PermissionCategory::with([
'permissions' => fn ($query) => $query->whereHas('adminUsers', fn ($query) => $query->where('admin_users.id', $this->user()->id)),
'selected' => fn ($query) => $query->whereHas('adminUsers', fn ($query) => $query->where('admin_users.id', $id)),
])
->select('id','name')
->get());
output:
{
"id": 2,
"name": "user",
"permissions": {
"permissions": [
[{
"id": 2,
"name": "userCreate"
},
{
"id": 3,
"name": "userUpdate"
},
{
"id": 4,
"name": "userDelete"
}
]
],
"selected": [
[{
"id": 2,
"name": "userCreate"
},
{
"id": 3,
"name": "userUpdate"
}
]
]
}
},
selected: The permissions of user B, edited by user A.
However, there is a situation like this. In order to compare the permissions in permissions with the permissions in selected, I need to put them both in a foreach loop. I don't like using a nested foreach loop. And I think Laravel has a solution for this. I'm new to Laravel and I'm trying to learn something so forgive me. Actually, I want an output like this. Let's say we loop the permissions in Permissions. Inside the loop: Does the permission in Permissions also exist in selected ? If it exists, I need to give the selected: true key and value to its permission in Permissions. So, to explain briefly, it is as follows:
{
"id": 2,
"name": "user",
"permissions": {
"permissions": [
[{
"id": 2,
"name": "userCreate"
"selected":true
},
{
"id": 3,
"name": "userUpdate"
"selected":true
},
{
"id": 4,
"name": "userDelete"
}
]
],
"selected": [
[{
"id": 2,
"name": "userCreate"
},
{
"id": 3,
"name": "userUpdate"
}
]
]
}
},
yes, it is better to explain with this example.
I tried this with resources and array map but failed. Do you have a solution suggestion for this issue?
It is possible to make a search by the results of another search?. For example:
// index: A
{ "ID": 1, "status": "done" }
{ "ID": 2, "status": "processing" }
{ "ID": 3, "status": "done" }
{ "ID": 4, "status": "done" }
// index: B
{ "ID": 1, "user": 1, "value": 10 }
{ "ID": 1, "user": 2, "value": 3 }
{ "ID": 2, "user": 1,"value": 1 }
{ "ID": 3, "user": 1, "value": 3 }
{ "ID": 4, "user": 1, "value": 7 }
Q1: Search in index "A" status == "done" and return the ID
RES: 1,3,4
Q2: From the results in Q1 search value > 5 and return the ID
RES: 1,4
My current solution is use two queries and download the results of "Q1" and make a second search in "Q2" but is very complicated because have 30k of results.
the problem to me seems to be more of a traditional union of filters in 2 indexes sort of a join , what we have in relational databases , not sure of the exact solution but recently had used a plug-in for the joins -> https://siren.io/siren-federate-20-0-introducing-a-scalable-inner-join-for-elasticsearch/ this might help
i'm very new to normalizr.
I'm trying to normalize a json api answer that looks like this: http://myjson.com/15st3f
There are some nested elements of same entity. For exemple:
{
"id": 1,
"name": "a",
[...]
"children": [
"id": 2,
"name": "b",
[...]
"children": [
"id": 3,
"name": "c"
]
]
}
How should I start this? I'm a bit confuse. Can it be normalized? Can you have array of same entity, or something like that ? Can I do something like this ? :
const uor = new schema.Entity('uors', {
UorChildren: [ uor ]
})
Use the define instance method:
const uor = new schema.Entity('uors');
uor.define({ children: [ uor ] });
Given data model structure like this,
{
Id: 123,
"string_facet": [
{
"name": "make",
"value": "Audi"
},
{
"name": "carListType",
"value": "PERSON EU"
},
{
"name": "modelType",
"value": ""
},
{
"name": "engineBrand",
"value": "APT"
},
{
"name": "typeDescription",
"value": "8D2"
}
],
"number_facet": [
{
"name": "typeNumber",
"value": 4614
},
{
"name": "serialNumber",
"value": 2
},
{
"name": "engineSize",
"value": 18
},
{
"name": "horsePower",
"value": 125
},
{
"name": "kw",
"value": 92
},
{
"name": "engineVolume",
"value": 1781
},
{
"name": "listType",
"value": 0
}
],
"dateTime_facet": [
{
"name": "fromDate",
"value": "1999-04-01T00:00:00"
},
{
"name": "toDate",
"value": "2000-10-01T00:00:00"
}
]
}
I want to get aggregates facet names, and values per name. However, I'm only interested in facets that have specific names, such as: make and engineBrand. Note that facets are of type nested.
I have tried the following .NEST expression, but it still returns all of the facet names.
.Global("global", g => g
.Aggregations(ag => ag
.Filter("global_makes", f => f
.Filter(ff => ff
.Nested(n => n
.Path("string_facet")
.Filter(pf => pf.Term("string_facet.name", "make")))
)
.Aggregations(agg => agg
.Nested("nested_string_facet", nested => nested
.Path("string_facet")
.Aggregations(stringFacet => stringFacet
.Terms("name", nameAgg => nameAgg.Field("string_facet.name").Size(0)
.Aggregations(nameAggNext => nameAggNext
.Terms("value", valueAgg => valueAgg.Field("string_facet.value").Size(0))
)
)
)
)
)
)
)
)
);
I have a filter within global (to lose scope of a passed in query), and then filter only on string_facet.name which match "make", but results still include all other names as well. How do I filter out aggregation to include only buckets where name is "make"?
This helped. https://github.com/elastic/elasticsearch/issues/4449
Essentially had to move filter part deeper into aggregation.
Pushing nested data into a Map inside a List
Can anyone tell me:
How do i push a task to either of these users (List items) ideally by specific user id?
Thanks in advance.
My code:
const initialState = Immutable.List([
Immutable.Map({
"id": 1,
"name": "Abe Bell",
"tasks": [
{
"id": 1,
"title": "Get haircut",
"status": false
}
]
}),
Immutable.Map({
"id": 2,
"name": "Chad Dim",
"tasks": [
{
"id": 2,
"title": "Get real job",
"status": false
}
]
})
])
First, the way you're building this structure, the tasks array will not be an immutable instance, I think that is not what you want, you can use Immutable.fromJS to transform all the nested arrays and maps into a Immutable instance.
The way your data is structured you'll have to navigate through the list of users and perform the update when the id matches.
One way of doing that is using map
const initialState = Immutable.fromJS([
{
"id": 1,
"name": "Abe Bell",
"tasks": [
{
"id": 1,
"title": "Get haircut",
"status": false
}
]
},
{
"id": 2,
"name": "Chad Dim",
"tasks": [
{
"id": 2,
"title": "Get real job",
"status": false
}
]
}
]);
let userId = 2;
let newState = initialState.map(user => {
if (user.get('id') !== userId) {
return user;
}
return user.update('tasks', tasks => {
return tasks.push(Immutable.fromJS({
id: 3,
title: "new task",
status: false
}))
});
});
Although this will do what you want,I think you should change your data to a map instead of a list if this kind of operation is something recurrent in your application. This will make things easier and faster.
const initialState = Immutable.fromJS({
"1": {
"id": 1,
"name": "Abe Bell",
"tasks": [
{
"id": 1,
"title": "Get haircut",
"status": false
}
]
},
"2": {
"id": 2,
"name": "Chad Dim",
"tasks": [
{
"id": 2,
"title": "Get real job",
"status": false
}
]
}
});
let userId = "2";
let newState = initialState.updateIn([userId, 'tasks'], tasks => {
return tasks.push(Immutable.fromJS({
id: 3,
title: "new task",
status: false
}));
});