【发布时间】:2023-03-10 12:27:01
【问题描述】:
大家好,我正在尝试获取“review”的数值,发生的情况是在 JSON 中它将它作为字符串返回,因此我无法执行我所做的求和,导致毫无意义价值
目标是,当有人对 X 产品发表评论时,数据以 JSON 格式保存在 Firebase 中,问题是“评论”被保存为字符串而不是数字
有没有办法把这一条数据转换成数字?
这是 JSON 响应
"[{\"review\":\"4\",\"comment\":\"Good product \",\"name\":\"SnowFall\",\"image\":\"https://lh3.googleusercontent.com\",\"method\":\"google\"}]"
这是我的打字稿
callback(i, totalReviews) {
if (!this.render) {
this.render = true;
let globalRating = 0;
let globalReviews = 0;
setTimeout(function () {
totalReviews.forEach((review, index) => {
globalRating += review.length;
for (const i in review) {
globalReviews += review[i].review;
console.log("globalReviews", globalReviews);
}
})
console.log("Raiting", globalRating);
console.log("Reviews", globalReviews);
let averageReviews = Math.round(globalReviews / globalRating);
let precentage = Math.round(globalReviews * 100 / (globalRating * 5));
$(".globalRating").html(globalRating);
$(".percentage").html(precentage);
let averageRating = DinamicReviews.fnc(averageReviews);
$(".br-theme-fontawesome-stars").html(`
<select class="ps-rating reviewsOption" data-read-only="true"></select>
`)
for (let i = 0; i < averageRating.length; i++) {
$(".reviewsOption").append(`
<option value="${averageRating[i]}">${i + 1}</option>
`)
}
Rating.fnc();
}, i * 10)
}
}
这就是它在 FireBase 中的保存方式FireBase RealTime Database
这是它在我的前端的外观 FrontEnd
【问题讨论】:
标签: javascript arrays json typescript arraylist