when i am trying to reach malzeme.aciklama im getting this error:
model.js:33 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'aciklama')
at model.js:33:9
at Array.forEach (<anonymous>)
at Module.loadDisplay (model.js:20:11)
at async controlDisplay (controller.js:10:5)
{
"id": "1",
"malzeme": {
"id": "1",
"malzeme_id": "1",
"locale": "tr",
"aciklama": "STRN-50M 1/2 HP, Monofaze",
"fiyat": "314.00",
"deleted_at": "13.09.2022 14:37",
"created_at": "0000-00-00 00:00:00",
"updated_at": "0000-00-00 00:00:00",
"stok_kodu": "0111STRN50M"
},
"stok_kodu": "0111STRN50M",
"aciklama": "",
"fiyat": ""
}
my code:
dataWeb.forEach(element => {
let newObj = {
id: '',
malzeme: '',
stok_kodu: '',
aciklama: '',
fiyat: '',
};
newObj.stok_kodu = element.stok_kodu;
newObj.id = element.id;
let malzeme_id =
dataLogo[
dataLogo.indexOf(dataLogo.find(item => item.malzeme_id === element.id))
].aciklama;
newObj.malzeme = malzeme_id;
newArray.push(newObj);
});
object key and value as output defined but when im trying to reach in foreach loop its giving undefined
CodePudding user response:
from the json you gave above, how to get malzeme.aciklama is just by this:
newObj.malzeme = element.malzeme.aciklama;
first because element.malzeme
isn't array. if it's an array, then you have to initialize first the dataLogo
to that array.
let dataLogo = element.malzeme
then you can iterate and find the element by using same id and get the aciklama