首页 > 解决方案 > 访问接收到的数据的值

问题描述

如何访问我在控制台中收到的数据值?

我如何在这里访问两个数组元素的标题?我必须过滤列表 acc。到所有数组元素的标题,所以我需要访问元素的所有标题这是控制台的 ss

https://ibb.co/tq8K67k

组件.ts 文件

export class AdminProductsComponent implements OnInit {
@ViewChild('f') searchform:NgForm
onsubmit(){

}
search=''
itemlist

onclick(){
this.search=this.searchform.value
this.itemlist=this.prservice.getallproducts()
console.log(this.itemlist)


}





headElements = ['S.No' ,'Title', 'Price'];




 listofproducts
 productreceived

constructor(private prservice:Productservice,private 
route:ActivatedRoute,private router:Router) { }

ngOnInit() {
this.listofproducts=this.prservice.getallproducts()

}
onclickedit(id){
// console.log(id)
this.router.navigate([id],{relativeTo:this.route})

}


 }

服务.ts 文件

export class Productservice{
updatedproduct=new BehaviorSubject<any>(1)
card=new Subject<any>()
cards=[]

addtocards(value){
    this.cards.push(value)
}
getallproducts(){
    return this.cards
}

getproductbyid(id){
    return this.cards[id]
}

updateproduct(id,product){
    this.cards[id]=product
    // console.log(this.cards[id])
    this.updatedproduct.next(this.cards[id])
}
deleteproduct(id){
    alert('Are you sure you want to delete this product permanently 
? Once deleted will not revert back')
    this.cards.splice(id,1)
    this.updatedproduct.next(this.cards)
}

}

标签: angular

解决方案


如果 'id' 是唯一字段,则将标题映射到 id 并使用它进行过滤。

在 component.ts 文件中:

titleList: Array<string> = [];

ngOnInit() {
this.listofproducts=this.prservice.getallproducts()
Object.values(listofproducts).forEach(prod => {
this.titleList[prod['id]] = prod['title'];
});

现在让我们考虑一下屏幕截图中的数据。

{title: bread,.... id:'32'}
{title: bread123,.... id:'38'}

当你在代码运行后 console.log(titleList) 时,你会得到这样的东西:

[ '32' : 'bread',
  '38' : 'bread123' ]

因此,现在您可以在 html/ts.xml 中使用 id 访问/过滤标题。


推荐阅读