I have array of options that is coming from server
const options = [
{key: 1, label: 'label1'},
{key: 2, label: 'label2'},
{key: 3, label: 'label3'},
{key: 4, label: 'label4'},
{key: 5, label: 'label5'},
{key: 6, label: 'label6'},
];
what I need is to map each array item with translation based on its key and return array of translated options
const options = [
{key: "key1", label: 'label1', translated: 'translation 1'},
{key: "key2", label: 'label2', translated: 'translation 2'},
{key: "key3", label: 'label3', translated: 'translation 3'},
{key: "key4", label: 'label4', translated: 'translation 4'},
{key: "key5", label: 'label5', translated: 'translation 5'},
{key: "key6", label: 'label6', translated: 'translation 6'},
];
I managed to do this using from(options) but it obviously emits each member of array separatelly - I need full array instead. In the code below I'm tryingtried to zip all translations, but I receive array of translated strings only - how do I map them back into each option?
import { of, from, timer, zip } from 'rxjs';
import { map, mergeMap } from 'rxjs/operators';
const getTranslation = (key) => of('translation ' + key)
const options = [
{ key: "key1", label: 'label1' },
{ key: "key2", label: 'label2' },
{ key: "key3", label: 'label3' },
{ key: "key4", label: 'label4' },
{ key: "key5", label: 'label5' },
{ key: "key6", label: 'label6' },
];
of(options).pipe(
mergeMap(options =>
zip(...options.map(option =>
getTranslation(option.key)))
)).subscribe(console.log);
here I created playground on stackblitz