JavaScript Rxjs Map Array in Json Document to new Array type rxjs json array,rxjs map json array

I am retrieving a document from PouchDB in an Angular Service. The document is retrieved in the following format:

{
"_id":"segments",
"_rev":"1-4f0ed65cde23fe724db13bea1ae3bb13",
"segments":[
    { "name":"Aerospace" },
    { "name":"Auto repair" },
    { "name":"Commercial" },
    { "name":"Education" },
    { "name":"Energy" },
    { "name":"Farm/ranch" },
    { "name":"Furniture" },
    { "name":"Heavy Equipment" },
    { "name":"Hobbyist" },
    { "name":"Infrastructure" },
    { "name":"Luxury/Leisure" },
    { "name":"Military" },
    { "name":"MUP" },
    { "name":"Processing" },
    { "name":"Rail" },
    { "name":"Transportation" }
]}

And I want to map that to a new Array that would look like:

[
  { value: "Aerospace", viewValue: "Aerospace" },
  { value: "Auto Repair", viewValue: "Auto Repair" },
  { value: "Commercial", viewValue: "Commercial" }
  ...
 ]

To accomplish this, I have tried this code in my Service:

getSegments(): Observable<any[]> {
  return from(this.database.get('segments'))
  .pipe(
    map((results) => results.segments)
  );
}

And I transform the array in my Component like this:

segments: SegmentsLookup[] = [];
...
this.lookupDbService.getSegments()
  .subscribe(data => {
    data.forEach(element => {
      this.segments.push({value: element.name, viewValue: element.name});
  });
});

This works but I know there is a way to map this properly back in the Service code. Also, when done this way, the compiler complains about the "results.segments" stating "Property "segments" does not exist on type '{}'.

How do I map the data retrieved to the Array that I need in the Service's "getSegments" method?

Answer:1

You can do the transformation is 2 steps:

  • pipe/map to extract the segments
  • array/map to convert to the final data type

Please see an example here: https://stackblitz.com/edit/angular-ikb2eg?file=src%2Fapp%2Fapp.component.ts

let transformedData = observableData.pipe(
  map(data => {
    console.log(data, data.segments.length);
    return data.segments.map(element => {
      return { value: element["name"], viewValue: element["name"] };
    });
  })
);

transformedData.subscribe(data => {
  this.mylist = data;
});
Answer:2

I'm having trouble figuring this out. I've created a table, and I want to use clickable buttons (not the arrow keys) to navigate through each cell and mark that cell as yellow. The table builds and I ...

I'm having trouble figuring this out. I've created a table, and I want to use clickable buttons (not the arrow keys) to navigate through each cell and mark that cell as yellow. The table builds and I ...

  1. javascript using buttons
  2. javascript using radio buttons
  3. javascript calculator using radio buttons
  4. calculator program in javascript using buttons

Currently I've got the following JSON feed: var data = { "feeds": { "regions": [{ "name": "Lichtenberg", "id": "01408.b", "suburbs": [{ "name": "Fennpfuhl", ...

Currently I've got the following JSON feed: var data = { "feeds": { "regions": [{ "name": "Lichtenberg", "id": "01408.b", "suburbs": [{ "name": "Fennpfuhl", ...

  1. creating array with for loop python
  2. creating array with values java
  3. creating array with numpy
  4. creating array with malloc
  5. creating array with new c++
  6. creating array with new
  7. creating array with negative size
  8. creating array with python
  9. creating array with
  10. creating array with integers java
  11. creating array with string
  12. creating array with mdadm
  13. creating array in java
  14. creating array in javascript
  15. creating array in php
  16. creating array in c
  17. creating array in matlab
  18. creating array in r
  19. creating array in js
  20. creating array in typescript

I need a textarea to include a set of double quotes at the start and end of the textarea value. The below code works in that double quotes are added to the start and end of the field, but if the user ...

I need a textarea to include a set of double quotes at the start and end of the textarea value. The below code works in that double quotes are added to the start and end of the field, but if the user ...

I have an URL looking like this: https://www.website.com/dk/da/home/category/ I am trying to remove the last forward slash and the text before it, untill it reaches the new forwardslash. Meaning i ...

I have an URL looking like this: https://www.website.com/dk/da/home/category/ I am trying to remove the last forward slash and the text before it, untill it reaches the new forwardslash. Meaning i ...

  1. remove last forward slash javascript
  2. remove last forward slash c#
  3. php remove last forward slash
  4. bash remove last forward slash from string
  5. regex replace last forward slash