为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

梦里花落0921 2022-10-27 14:36:43
我不久前开始使用 JS,我想知道如何将 products 变量转换为 JavaScript 中的 detailProductsconst 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'   }   ]   
查看完整描述

1 回答

?
DIEA

TA贡献1820条经验 获得超2个赞

和中的title和submitterAvatarUrl属性相同。您可以通过解析项目的属性来生成属性:productdetailedProductsdetailsubmitterAvatarUrlproducts


const 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);

});


查看完整回答
反对 回复 2022-10-27
  • 1 回答
  • 0 关注
  • 107 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信