Dev Me
asked on
RxJS loops
Hi Experts,
I am trying to loop through json object and filter it into different arrays based on 'group'. Also, trying to assign particular field to a const....I couldn't go beyond
I would like have this.group1, this.group2, this.gropu3 to have respective Fields after this execution along with this.fieldName1, this.fieldName2 having their respective field values.
Field object looks like this
I am trying to loop through json object and filter it into different arrays based on 'group'. Also, trying to assign particular field to a const....I couldn't go beyond
from(Field).pipe(groupBy(fie => fie.group), mergeMap(group => group.pipe(toArray())));
I would like have this.group1, this.group2, this.gropu3 to have respective Fields after this execution along with this.fieldName1, this.fieldName2 having their respective field values.
Field object looks like this
Field : [
{ name : fieldname,
value: FieldValue,
group: FieldGroup
}
]
ASKER
So "item.group" is not a number but a string , how would array take string as index number?
Change the filteredArray to an object
let filteredArray = {};
Now the index acts as a property and you can use strings
ASKER
It still gives the same error. "[ts] Type 'String' cannot be used as an index type."
ASKER
Experts any help please?
Please post your full code showing your implementation. My code works here so I need to see how you have interpreted that.
Just implemented it a test Typescript Angular project - works perfectly.
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComonent implements OnInit {
data = [{
name: 'Test Name 1',
field: 'Field 1',
group: 'Group One'
},{
name: 'Test Name 1',
field: 'Field 1',
group: 'Group One'
},{
name: 'Test Name 2',
field: 'Field 2',
group: 'Group Two'
},{
name: 'Test Name 3',
field: 'Field 3',
group: 'Group Three'
},{
name: 'Test Name 4',
field: 'Field 4',
group: 'Group One'
},{
name: 'Test Name 5',
field: 'Field 5',
group: 'Group Four'
},{
name: 'Test Name 6',
field: 'Field 6',
group: 'Group Three'
},{
name: 'Test Name 7',
field: 'Field 7',
group: 'Group Two'
},{
name: 'Test Name 8',
field: 'Field 8',
group: 'Group Four'
},{
name: 'Test Name 9',
field: 'Field 9',
group: 'Group One'
},{
name: 'Test Name 10',
field: 'Field 10',
group: 'Group Two'
}];
ngOnInit() {
let filteredArray = {};
this.data.forEach(item => {
filteredArray[item.data] = filteredArray[item.data] || [];
filteredArray[item.data].push(item);
});
console.log(filteredArray);
}
}
ASKER
In your latest post, you have used
in your first suggestion you had
here is my code, TS-lint still failing with the error I mentioned.
this.data.forEach(item => {
filteredArray[item.data] = filteredArray[item.data] || [];
filteredArray[item.data].push(item);
});
in your first suggestion you had
let filteredArray = [];
data.forEach(item => {
filteredArray[item.group] = filteredArray[item.group] || [];
filteredArray[item.group].push(item);
});
here is my code, TS-lint still failing with the error I mentioned.
ngOnInit() {
// lets make a call and initialized the root object and children
this.objectService.getObject().subscribe(root => {
this.objectRoot = root;
this.objectRoot.Object.Field.forEach(fi => {
this.setUpObjectFieldData(fi);
const filterArray = {};
filterArray[fi.group] = filterArray[fi.group] || [];
filterArray[fi.group].push(fi);
});
});
My first post had item.group and filteredArray = []
I then changed the filteredArray to an object. The second post was a sample to test the concept.
However, the problem is that you have not implemented the suggested code to match your data model.
Look at line 7 - you have that inside your loop - instead of outside of it look at where it is in my code on line 53
Next if your code is erroring - please post the error and the line number (Relative to your posted code) so I can see what is going on.
Finally, please dump your this.objectRoot.Object.Fie ld property and paste here so I can see what your data looks like.
I then changed the filteredArray to an object. The second post was a sample to test the concept.
However, the problem is that you have not implemented the suggested code to match your data model.
Look at line 7 - you have that inside your loop - instead of outside of it look at where it is in my code on line 53
Next if your code is erroring - please post the error and the line number (Relative to your posted code) so I can see what is going on.
Finally, please dump your this.objectRoot.Object.Fie
PS. I have not seen your full component code but remember to implement OnDestory and unsubscribe from the
this.objectService.getObje ct()
To do that you need to do the following
this.objectService.getObje
To do that you need to do the following
import { Component, OnInit, OnDestroy } from '@angular/core';
...
@Component({
selector: 'component-selector',
templateUrl: './template.component.html',
styleUrls: ['./style.component.css']
})
export class YourComponent implements OnInit, OnDestroy {
private svcSub: ISubscription;
...
ngOnInit() {
this.svcSub = this.objectService.getObject().subscribe(root => {
...
}
}
ngOnDestroy() {
this.svcSub.unsubscribe();
}
}
ASKER
This will be a logical issue. Ok put const outside of foreach loop.
Look at line 7 - you have that inside your loop - instead of outside of it look at where it is in my code on line 53
below is TS-lint error I am talking about.
Below is the sample for "fi" property that I am "consoling..."
{name: "Impact", $: "capability/functionality", group: "howbad"}
{name: "Info-req-on", $: "09/10/2003 03:00:00", group: "system"}
{name: "Integrated-releases", $: "002.005(026)", group: "version"}
{name: "Integrated-releases", $: "002.005(025)", group: "version"}
And finally thanks for your note on onDestroy(). I wasn't doing it.
ASKER
on a side note...
seems like I can't directly "unsubscribe" from a service...meaning I can't do something like
I have to assign subscribe call to a subscription and unsubscribe that subscription?
is that my understanding correct?
seems like I can't directly "unsubscribe" from a service...meaning I can't do something like
this.service.undersubscribe()
I have to assign subscribe call to a subscription and unsubscribe that subscription?
this.subscription = this.service.getObject().subscribe
OnDesctroy()
{
this.subscription.unsubscribe()
}
is that my understanding correct?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ah...interesting...thanks for the solution. Changing 'String' to 'string' worked. Never would have realized that.
Rule of thumb - stick to the basic types (lower case versions) - they are good for most instances.
Open in new window