使用 map 迭代对象数组时访问数组内的嵌套对象值
我的数组由多个对象组成,如下所示:
{
"id": 2,
"nom_operation": "opSavCreationTest2",
"num_serie": "1203250cd81b2101",
"address_mac": "04:15:d9:00:00:49",
"description": "opSavCreationTest2",
"adminId": 82,
"produitId": 1,
"clientId": 2,
"status": "produit_recu",
"lieu_achat_produit": "opSavCreationTest2",
"garanti_au": "07-02-2021",
"createdAt": "2020-03-27T15:15:55.207Z",
"updatedAt": "2020-03-27T15:15:55.207Z",
"produit": {
"id": 1,
"nom_produit": "PURE BLACK MAT",
"type_produit": "Pure",
"couleur_produit": "black_mat",
"nbr_op_sav_en_cours": 7,
"img": "img",
"createdAt": "2020-03-30T08:41:39.597Z",
"updatedAt": "2020-03-30T15:37:09.294Z"
},
"client": {
"id": 2,
"nomClient": "opSavCreationTest2",
"numTel": "5775757575",
"email": "opSavCreationTest2",
"addresse": "opSavCreationTest2",
"ville": "opSavCreationTest2",
"pays": "opSavCreationTest2",
"createdAt": "2020-03-27T15:15:55.043Z",
"updatedAt": "2020-03-27T15:15:55.043Z"
},
"admin": {
"id": 82,
"nom": "administrateur",
"email": "[email protected]",
"mot_de_passe": "efefr3lnr8Fr4IwEsc=",
"last_login": null,
"role": "administrateur",
"createdAt": "2020-03-23T09:08:43.585Z",
"updatedAt": "2020-03-23T09:08:43.585Z"
}
}
当我尝试通过使用
当我尝试访问 produit 的属性“nom_produit”的值时,出现此错误:
TypeError: Cannot read property 'nom_produit' of undefined
尽管如此,我已成功记录嵌套对象
produit
!!
我真的找不到这个奇怪结果的解释。如果 produit 对象已成功记录,为什么当我尝试访问其属性之一时它会被视为未定义?
map
迭代数组来访问嵌套对象
produit
内的值时,发生了一些奇怪的事情>
this.state.operationSavInformation.map(
(operationSavInformation, i) => {
const operationSavLink = `/home/operationSav/${operationSavInformation.id}`;
console.log("operationSavInformation ", i);
console.log("operationSavInformation.produit"); // Works
console.log(operationSavInformation.produit);
/** LOGS THIS:
*
* id: 1
* nom_produit: "PURE BLACK MAT"
* type_produit: "Pure"
* couleur_produit: "black_mat"
* nbr_op_sav_en_cours: 7
* img: "img"
* createdAt: "2020-03-30T08:41:39.597Z"
* updatedAt: "2020-03-30T15:37:09.294Z"
*/
console.log(
operationSavInformation.produit["nom_produit"]
); //ERROR
/**TypeError: Cannot read property 'nom_produit' of undefined */
console.log("operationSavInformation.produit"); // Works
console.log(operationSavInformation.produit);
/** LOGS THIS:
*
* id: 1
* nom_produit: "PURE BLACK MAT"
* type_produit: "Pure"
* couleur_produit: "black_mat"
* nbr_op_sav_en_cours: 7
* img: "img"
* createdAt: "2020-03-30T08:41:39.597Z"
* updatedAt: "2020-03-30T15:37:09.294Z"
*/
这简直太荒谬了。
我添加了一个
if else
,它突然就起作用了:
this.state.operationSavInformation.map(
(operationSavInformation, i) => {
const operationSavLink = `/home/operationSav/${operationSavInformation.id}`;
console.log("operationSavInformation ", i);
console.log("operationSavInformation.produit"); // Works
console.log(operationSavInformation.produit);
const produit = operationSavInformation.produit;
console.log(
"produit = operationSavInformation.produit;"
);
console.log(produit);
if (produit !== undefined) {
// This gets executed despite the error without if else says that produit is undefined!!
console.log("produit is not undefined");
console.log(produit.nom_produit);
} else {
// This doesn't get executed despite the error without the if else is that it cannot read nom_produit of undefined
console.log("produit is undefined");
}}
但是,如果我删除该条件,我仍然会收到 产品未定义错误 ,但条件检查它不是未定义的,并且它起作用了!
根据您显示的日志:
** LOGS THIS:
*
* id: 1
* nom_produit: "PURE BLACK MAT" // " nom_produit" should be right
* type_produit: "Pure"
我猜,您错过了此属性的一个空格,请尝试
console.log(operationSavInformation.produit[" nom_produit"])
const test = {
"id": 2,
"nom_operation": "opSavCreationTest2",
"num_serie": "1203250cd81b2101",
"address_mac": "04:15:d9:00:00:49",
"description": "opSavCreationTest2",
"adminId": 82,
"produitId": 1,
"clientId": 2,
"status": "produit_recu",
"lieu_achat_produit": "opSavCreationTest2",
"garanti_au": "07-02-2021",
"createdAt": "2020-03-27T15:15:55.207Z",
"updatedAt": "2020-03-27T15:15:55.207Z",
"produit": {
"id": 1,
" nom_produit": "PURE BLACK MAT", //You should check this attribute, is it " nom_produit" or "nom_produit"
"type_produit": "Pure",
"couleur_produit": "black_mat",
"nbr_op_sav_en_cours": 7,
"img": "img",
"createdAt": "2020-03-30T08:41:39.597Z",
"updatedAt": "2020-03-30T15:37:09.294Z"
},
"client": {
"id": 2,
"nomClient": "opSavCreationTest2",
"numTel": "5775757575",
"email": "opSavCreationTest2",
"addresse": "opSavCreationTest2",
"ville": "opSavCreationTest2",
"pays": "opSavCreationTest2",
"createdAt": "2020-03-27T15:15:55.043Z",
"updatedAt": "2020-03-27T15:15:55.043Z"
},
"admin": {
"id": 82,
"nom": "administrateur",
"email": "[email protected]",
"mot_de_passe": "efefr3lnr8Fr4IwEsc=",
"last_login": null,
"role": "administrateur",
"createdAt": "2020-03-23T09:08:43.585Z",
"updatedAt": "2020-03-23T09:08:43.585Z"
}
}
console.log(test.produit[" nom_produit"])
console.log(test.produit["nom_produit"])