我们如何将 products 变量转换为 detailProducts?

我不久前开始使用 JS,我想知道如何将 products 变量转换为 JavaScript 中的 detailProducts


const products = [ 

  { title: 'Yellow Pail', 

  submitterAvatarUrl: 'images/avatars/daniel.jpg', 

  productImageUrl: 'images/products/image-aqua.png', 

  }, 

  { title: 'Supermajority: The Fantasy Congress League', 

  submitterAvatarUrl: 'images/avatars/kristy.png', 

  productImageUrl: 'images/products/image-rose.png', 

  }, 

  { title: 'The art of cooking', 

  submitterAvatarUrl: 'images/avatars/james.png', 

  productImageUrl: 'images/products/image-gold.png', 

  } 

  ] 

  const detailedProducts = [ 

  { 

  detail: 'This book was submitted by Daniel', 

  title: 'Yellow Pail', 

  submitterAvatarUrl: 'images/avatars/Daniel.jpg' 

  }, { 

  detail: 'This book was submitted by Kristy', 

  title: 'Supermajority: The Fantasy Congress League', 

  submitterAvatarUrl: 'images/avatars/Kristy.png' 

  }, 

  { 

  detail: 'This book was submitted by James ', 

  title: 'The art of cooking', 

  submitterAvatarUrl: 'images/avatars/James.png' 

  } 

  ] 

  


梦里花落0921
浏览 108回答 1
1回答

DIEA

和中的title和submitterAvatarUrl属性相同。您可以通过解析项目的属性来生成属性:productdetailedProductsdetailsubmitterAvatarUrlproductsconst detailedProducts = [];products.forEach(p => {   const parts = p.submitterAvatarUrl.split('/');   const name = parts[parts.length - 1].split('.')[0];   const capitalizedName = name.charAt(0).toUpperCase() + name.slice(1);   const detailedProduct = {       detail: 'This book was submitted by ' + capitalizedName,       title : p.title,       submitterAvatarUrl: p.submitterAvatarUrl   };   detailedProducts.push(detailedProduct);});
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript