-1

I have a simple json data,I need to get the name from object by comparing its id.Suppose I have a number 2,I need to compare with object's id,if it is equal to 2 then I need to get matching object's property 'name' from object.Like here it will be matching name 'dog' in my json. Here is the code below https://stackblitz.com/edit/angular-dtyvrc?file=src%2Fapp%2Fapp.component.ts

app.component.html

<hello name="{{ name }}"></hello>
<p>
  Start editing to see some magic happen :)
</p>

app.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  name = 'Angular';
  ngOnInit() {
  let statusdata1 = [{"id":1,"name":"cat"},{"id":2,"name":"dog"}]
  console.log('dog')
  }
}
carreankush
  • 621
  • 2
  • 9
  • 32
  • Does this answer your question? [How do I check if an array includes a value in JavaScript?](https://stackoverflow.com/questions/237104/how-do-i-check-if-an-array-includes-a-value-in-javascript) – Pushkin Mar 11 '20 at 04:41
  • You mean like this?? https://stackblitz.com/edit/angular-fdmsam – Maniraj Murugan Mar 11 '20 at 04:43

6 Answers6

1

You can get your output by this way.

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  name = 'Angular';
  ngOnInit() {
    let statusdata1 = [{ id: 1, name: "cat" }, { id: 2, name: "dog" }];
    console.log("dog");
    let value = this.getName(statusdata1, 2)
    alert(value);
  }
  getName(dataList, id) {
    let data = dataList.find(a => a.id == 2);
    if (!!data) {
      return data.name;
    }
  }
}
0

Use Array find

let statusdata1 = [{"id":1,"name":"cat"},{"id":2,"name":"dog"}] 

let foo = statusdata1.find(x => x.id === matchingId)

console.log(foo)
Pushkin
  • 3,336
  • 1
  • 17
  • 30
0
objName: any = "";// declare it as a global variable
id=2;
statusdata1.forEach((obj)=>{if(obj["id"]==id)objName=obj["name"]});
console.log(objName);
Gopi krishna
  • 308
  • 1
  • 9
0
statusdata1.find(x => x.id === 2)?.name;
Sami
  • 3,686
  • 4
  • 17
  • 28
0

try like this

 const data = statusdata1.find(x => x.id === 2)?.name;
Sayooj V R
  • 2,255
  • 2
  • 11
  • 23
  • While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – Igor F. Mar 11 '20 at 09:21
0

Below is code to be added in app.component.ts

  result: any; // variable to hold name for mathched id
  idToCompare = 2; // id to comapre from array 

  ngOnInit() {
   let statusdata1 = [{"id":1,"name":"cat"},{"id":2,"name":"dog"}]

   this.result = statusdata1.find(x => x.id === this.idToCompare).name;
   console.log(this.result); // result will be dog
 }

app.component.html

<p>Name of compared id found result:-><b> {{result}}</b> </p>

Below is an updated URL for a working example How to find object property name by matching its id

Hope this helps.

prasad07
  • 201
  • 1
  • 5