Angular - Access values of object stored in array of objects inside template

Ashiv3r

I've got a single class as below:

export class SettingsTab {
  id: number;
  class: string;
  name: Object = {};
}

Also I mock it into simple array:

export const SETTINGSTABS: SettingsTab[] = [
  { id: 1, class: 'classOne', name: [{ "pl": "Tekst po polsku" }, { "en": "Text in english" }] },
  { id: 2, class: 'classTwo', name: [{ "pl": "Tekst po polsku" }, { "en": "Text in english" }] },
  { id: 3, class: 'classThree', name: [{ "pl": "Tekst po polsku" }, { "en": "Text in english" }] }
];

Now I would like to access certain value of name object in my template depending on which language I currently use. I get info about current language from localStorage as shown below:

language = localStorage.getItem('currentLang') != null ? localStorage.getItem('currentLang').replace("'", "") : "pl";

I managed to achieve it by code listed below:

<ng-container *ngFor="let name of settingsTab.name">
  <ng-container *ngFor="let key of ObjectKeys(name)">
    <span *ngIf="key == language">{{ name[key] }}</span>
  </ng-container>
</ng-container>

ObjectKeys are defined in my component as:

ObjectKeys = Object.keys;

But my question is there any easiest way to achieve what I want to do? Don't like two ng-components with *ngFor inside them. Also would like to achieve it in the most optimized way.

acdcjunior

Ideally, the property name:

{ ..., name: [{ "pl": "Tekst po polsku" }, { "en": "Text in english" }] },

Would have the format of not an array of objects, but an object:

{ ..., name: {"pl": "Tekst po polsku", "en": "Text in english"} },

Then you could simply use in your template:

<span>{{ settingsTab.name[language] }}</span>

Which is a constant-time lookup (just getting a property in an object).



Now, if you can't change the format of name, I'd use a method instead of those nested ng-containers:

export class MyComponent {
  ...
  lang(names: [any], language: string) {
    return (names.find(lang => lang[language]) || {})[language]
  }
  ...
}

And use it like:

<span>{{ lang(settingsTab.name, language) }}</span>

Of course, you can call lang whatever you see fit. Just notice this alternative has O(n) complexity, due to find having to iterate the name array. This probably won't impact the performance of you application at all, but I thought you should know. In the current format of the name, there is no quicker way here.

Demo: https://stackblitz.com/edit/angular-c5wzbk?file=src/app/app.component.ts

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how to access array inside an object and the array itself contain objects in angular?

Access Nested Object Inside Array in Angular NGXS

How do I access values on an array of objects in mustache template in moodle?

How to access keys of an object dynamically inside angular template

How to access JSON type object property stored inside data object in Vue's template?

React.js - Unable to access values inside array of objects

How to access key/pair values within objects inside of an array in Ansible?

Angular 2 how to display the array object inside an array of objects

Access Array of objects inside another object in React State

Access Values in Array of Objects?

Sort array of objects, using values inside each object (aggregation framework)

Sort each Object inside Array of Objects by its values

Access to an array inside an object

How to access values inside a multiple array JSON object?

Access (and count) just object values from Postgres JSONB array of objects

Access items inside of an array of objects

Vue3 JS - How to access/print values stored in an object onto document <template>

how to iterate array values inside json object in angular

Angular update an object in an array with new values, inside a reducer

merge object and objects inside array?

Overwrite an Object inside an Array of Objects

Destructuring an array of objects inside an object

How do I access values stored in an array inside a C struct returned to Python via ctypes?

Filter an array inside an object inside an object inside in array of objects

Find closest object values inside object of objects

Get values from an array stored inside a map

Objects inside Array inside Object Swift

access to an array inside a object in javascript?

How can I reach the values of an array that is inside an object that is part of an array of objects?