Iterate over array of objects in Typescript. You can use the built-in forEach function for arrays. Use the below Typescript example:
1 2 3 4 | //this sets all product descriptions to a max length of 10 characters data.products.forEach( (element) => { element.product_desc = element.product_desc.substring(0,10); }); |
Your version wasn’t wrong though. It should look more like this:
1 2 3 | for(let i=0; i<data.products.length; i++){ console.log(data.products[i].product_desc); //use i instead of 0 } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.