我不久前开始使用 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);
});
添加回答
举报
0/150
提交
取消