0

I have two related objects:

itemsObj = {
    'item1' : 'A1',
    'item2' : 'A2',
    'item3'  : 'B',
    'item4'  : 'C',
    'item5'  : 'D'
};

And:

itemsObj2 = {
    'A1' : 'Very conservative and up',
    'A2' : 'Conservative and up',
    'B'  : 'Balanced and up',
    'C'  : 'Growth and up',
    'D'  : 'Aggressive'
};

Interpolating {{itemsObj.item1}} will return A1 and {{itemsObj2.A1}} will return Very conservative and up.

How to concatenate itemsObj.item1 with itemsObj2.A1 to return Very conservative and up?

My attempt:

{{itemsObj2.itemsObj.item1}} but of course this is wrong. So may I know how to concatenate in my case?

2
  • 3
    {{itemsObj2[itemsObj.item1]}} Commented May 25, 2018 at 10:37
  • Do you want to display only one value or display them all and have the possibility to loop over them? Commented May 25, 2018 at 10:55

3 Answers 3

2

Do this:

itemsObj2[itemsObj.item1]    //setting itemsObj.item1's value as itemsObj2's key

DEMO:

itemsObj = {
    'item1' : 'A1',
    'item2' : 'A2',
    'item3'  : 'B',
    'item4'  : 'C',
    'item5'  : 'D'
};

itemsObj2 = {
    'A1' : 'Very conservative and up',
    'A2' : 'Conservative and up',
    'B'  : 'Balanced and up',
    'C'  : 'Growth and up',
    'D'  : 'Aggressive'
};

console.log(itemsObj2[itemsObj.item1])

Sign up to request clarification or add additional context in comments.

1 Comment

Should be the accepted solution, just for the snippet.
2

TS

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  public items: string[];

  constructor() {
    const itemsObj = {
      'item1': 'A1',
      'item2': 'A2',
      'item3': 'B',
      'item4': 'C',
      'item5': 'D'
    };

    const itemsObj2 = {
      'A1': 'Very conservative and up',
      'A2': 'Conservative and up',
      'B': 'Balanced and up',
      'C': 'Growth and up',
      'D': 'Aggressive'
    };

    this.items = Object
      .values(itemsObj)
      .map(itemKey => itemsObj2[itemKey]);
  }
}

HTML

<div *ngFor="let item of items">
  {{ item }}
</div>

Output:

Very conservative and up
Conservative and up
Balanced and up
Growth and up
Aggressive

Stackblitz demo:

https://stackblitz.com/edit/angular-pbdfux?file=src%2Fapp%2Fapp.component.html

9 Comments

Too much for so little. I'd go for Sajeet's solution
True. But this solution provides a way to loop over it. If OP don't need that fine, otherwise here it is. (I just doubt that with a set of different keys he just want to display one, but maybe)
I agree, but that's not what he asked. Although it's perfectly valid and working code, you're giving an overkill solution, hence my downvote.
Wow downvote for 3 lines / valid solution. I personally downvote for wrong or very incomplete solutions with a lack of explanations. Not for valid solutions with demo and all ¯\_(ツ)_/¯
And I downvote sot the solution that isn't best suited for a question isn't a the top of the page. To each his own way of voting.
|
1

Since you need to match the key , you can do

   <h1> {{itemsObj2[itemsObj.item1]}}</h1>

STACKBLITZ DEMO

3 Comments

Guys if you're downvoting anyone add the reason
Those who have upvoted my comment, please go further up and upvote answer too :P
Yeah I'm tired of getting downvotes with no explanation, so I only make looooong answers with explanations, so quick downvoters don't even read it (And I upvoted the solution already)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.