【发布时间】:2021-01-26 18:04:24
【问题描述】:
我想仅使用纯 JS 显示 点赞总数。 我从 JSON 文件中获取了数字,但我不知道如何获得喜欢的总数, 我尝试使用 reduce 方法它不起作用,也许我做错了方式
这就是我想做的click here for the photo 这是我的代码
function photographerWork(JsonData, homeElement){
const homeElt = homeElement.id;
JsonData.media.forEach(element => {
if(homeElt == element.photographerId){
const domDiv = document.getElementById('photographer-work');
const newDiv = document.createElement("div");
///// the code i'am trying ////
const allTheLiks = element.likes
console.log(allTheLiks)
//////////////////////
const workTemplate = `
<div class="photo-box">
<div class="photo">
${videoOrImage(element.image, element.video, element)}
</div>
<div class="text">
<p> ${element.tags}<b>${element.price} €   ${element.likes} <i class="fas fa-heart"></i></b></p>
</div>
</div>
`
newDiv.innerHTML = workTemplate;
domDiv.appendChild(newDiv);
likesAndPrice(element, allTheLiks);
}
})
}
这是我的结果click hereenter code here
这是我的 JSON 文件示例
"media": [
{
"id": 342550,
"photographerId": 82,
"image": "../Photos/Tracy/Fashion_Yellow_Beach.jpg",
"tags": ["fashion"],
"likes": 62,
"date": "2011-12-08",
"price": 55
},
{
"id": 8520927,
"photographerId": 82,
"image": "../Photos/Tracy/Fashion_Urban_Jungle.jpg",
"tags": ["fashion"],
"likes": 11,
"date": "2011-11-06",
"price": 55
},
{
"id": 9025895,
"photographerId": 82,
"image": "../Photos/Tracy/Fashion_Pattern_on_Pattern.jpg",
"tags": ["fashion"],
"likes": 72,
"date": "2013-08-12",
"price": 55
},
{
"id": 9275938,
"photographerId": 82,
"image": "../Photos/Tracy/Event_WeddingGazebo.jpg",
"tags": ["events"],
"likes": 69,
"date": "2018-02-22",
"price": 55
},
{
"id": 2053494,
"photographerId": 82,
"image": "../Photos/Tracy/Event_Sparklers.jpg",
"tags": ["events"],
"likes": 2,
"date": "2020-05-25",
"price": 55
},
【问题讨论】:
-
在循环中总结它们
-
嗨! @EgzonBerisha 我有事。我会尽快添加答案。但只是为了确认您到目前为止所做的 reduce 公式是什么?
-
对可能回答者的说明:这个问题以及类似的问题在 Stack Overflow 上被以相对较高的频率提出。这种情况经常发生,我们有几个规范的重复。在回答问题之前,请至少粗略搜索一下重复项。
-
@EgzonBerisha 我已经为这个问题提供了一个单一的答案 - 我花了几个小时才弄清楚。希望有帮助
标签: javascript