Search code examples
javascriptangularloopsngforangular-ng-if

Replace values in angular with ngif or other


I have a GET Request that is returning tasks:

"tasks": [
    {
      "partner_id": 1,
      "title": "aa",
      "description": "dddd",
      "priority": "High",
    }]

and i have another GET Request representing the values of the partner.

"partners": [
    {
      "id": 1,
      "value": "str"
    }]

When implementing the view, and doing a ngfor on the tasks, i'd like to replace the task.partner_id for the real name on the partner.

//getting the id of the task.partner_id and represent the partner.value
<span><span class="bold">For</span> {{task.partner_id}}</span>

enter image description here

Instead of 1 being the partner name equivalent to id: 1.

How can I do this without manipulating the request that is coming from the API?

UPDATE: I've used the solutions suggested by @errorau and @Clint


getParentValue(parents, parentId) {
  return Object.keys(parents).find(key => parents[key] === parentId);
}

and it worked fine, i'm rendering the right values on the frontend. but right now i'm receiving an error: enter image description here


Solution

  • You can't manipulating your api you can write basic a funtion for get parent value like

    <span><span class="bold">For</span> {{getTask(task.partner_id)}}</span>
    

    you can write this function like that

      function getTask(id){
        return partners.find((p) => p.id === id).value
    }