0

When I want to get data from the object like this

const translate = {home:title:'Home'}}

we use home.title, and this is ok.

But I need to change this home.title to [home][title]

I need this, because, I made pipe, and with this pipe, I send value from HTML view like

{{ "home.title" | getTranslate}}

and I need to take value from the array, let's say arr[home.title] but this not working, I need this arr[home][title].

EDITED:

I convert a value from HTML to format as I want

let b = value.split(".");
let keyTranslate = "[" + b[0] + "]" + "" + "[" + b[1] + "]";
console.log(keyTranslate)    // [home][title]

And now I pass this value to another function

this.store.select(fromStore.TranslateSelector.translateKey(keyTranslate ));

And here I want to take value by this key

export const translateKey = (key: any) => createSelector(
    getTranslate, 
    translate => console.log(translate[key]));  // this not working. 

How to set "key" to translate array?

Arter
  • 2,224
  • 3
  • 29
  • 66
  • `arr` is not an array currently, it's an object with a property that's also an object. `arr[x][y]` is only used in 2d arrays. But you can access properties using the keys as strings, as `arr["home"]["title"]`. Not sure if this is what you're looking for. – igg Jan 21 '20 at 09:01
  • i edited my question, pls take a look – Arter Jan 21 '20 at 09:15
  • looks like you may want to look into transloco though :) https://github.com/ngneat/transloco – maxime1992 Jan 21 '20 at 10:32

1 Answers1

0

I found soution if someone else stuck with this, with ECMAscript 6 it is easy

key.split(".").reduce((o, i) => o[i], translate)); --> // translate.home.title

Here is full explanation https://stackoverflow.com/a/6394168/7167775

Arter
  • 2,224
  • 3
  • 29
  • 66