Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/423.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当对象位于数组中时,如何获取一个对象属性?_Javascript_Angular_Frontend - Fatal编程技术网

Javascript 当对象位于数组中时,如何获取一个对象属性?

Javascript 当对象位于数组中时,如何获取一个对象属性?,javascript,angular,frontend,Javascript,Angular,Frontend,我正在开发一个Angular 8应用程序,我必须在表中显示当前注册的用户。我正在使用ng2智能表向用户显示,但我有一个问题:显然,来自后端的响应是一个数组,但每个数组都有一个对象,我需要获取每个数组的属性(本例中的名称) 以下是我的代码: this.mainService.getAllUsers().subscribe(res => { console.log(res); this.users = res; this.source = new LocalDataSource

我正在开发一个Angular 8应用程序,我必须在表中显示当前注册的用户。我正在使用
ng2智能表
向用户显示,但我有一个问题:显然,来自后端的响应是一个数组,但每个数组都有一个对象,我需要获取每个数组的属性(本例中的名称)

以下是我的代码:

  this.mainService.getAllUsers().subscribe(res => {
  console.log(res);
  this.users = res;
  this.source = new LocalDataSource(this.users);  
res
的格式是以下数组:

{
_id: "5e5688bd1ef392001775c8c5"
nombre: "NICOLAS"
apellido: "SANABRIA"
email: "nico@ex.co"
nivel: 1
cargo: "Gerente EAP"
password: "$2a$10$jvqdJnIRKC4C9b5oSdmguOZA.UqqV.B7BnN1RhoMelFUk750K5BBW"
empresa: {_id: "5e5688bc1ef392001775c8c4", nombre: "P4", tipo: "EAP", estado: "pendiente", createdAt: "2020-02-26T15:03:24.638Z", …}
estado: "pendiente"
createdAt: "2020-02-26T15:03:25.067Z"
}
我需要的对象是“empresa”


非常感谢您的帮助

如果您需要empresa名称属性列表,可以将要转换的用户数组映射为empresa名称
以下是您如何做到这一点:

this.mainService.getAllUsers().subscribe(res => {
  console.log(res);

  const empresaNamesList = res.map(user => user.empresa.nombre);

  ...
}

您可以使用
valuePrepareFunction
filterFunction
来显示所需的属性,同时保持过滤功能

我已经创建了一个快捷菜单

唯一的更改是在列定义中,如下所示:

设置={
栏目:{
//其他栏目
empresa:{
标题:“Empresa”,
valuePrepareFunction:(empresa)=>{
//我选择显示名称
返回empresa.nombre;
},
filterFunction:(empresa?:any,search?:string)=>{
如果(search.length>0){
//按empresa.nombre过滤
返回empresa.nombre.match(搜索);
}
}
}
}
};

res[index].empresa
?其中
index
来自for循环/foreach你想要一个emprasa属性数组吗?我的意思是,ng2智能表会自动设置相应列中的数据,但当我指定“empresa”数据时,我不想指定对象,但名称和名称是一个属性。我已经尝试过了,但我不需要将emprasa显示为对象,但是它的一个属性,你明白我的意思吗?是的,我明白你想要什么,只要看看这个代码,我已经映射到你想要显示的确切属性
res.map(user=>user.empresa.nombre)
它为我带来了empresa名称,但我不需要仅使用名称,还需要res数据的其余部分。问题在于LocalSourceData,它会自动将数据分配到我的表中,因此在empresa的列中我得到[Object Object],但我需要名称,明白吗?标记为up的注释对我有效,谢谢帮助!谢谢,这就是我需要的,谢谢你的帮助!