我有一系列看起来像的对象:
我想基于site_nm
添加对象。我希望能够使这个对象数组均匀。我想通过增加日期为每个site_nm添加相等数量的对象。所以我在做:
let data = [{
"site_nm": "gs Universe",
"date": "2019-10-01",
"sigh": 1,
"yo": 1,
"wokay": 0
}, {
"site_nm": "gs Gameplay",
"date": "2019-10-01",
"sigh": 4,
"yo": 3,
"wokay": 2
}, {
"site_nm": "gs Universe Trailers",
"date": "2019-10-01",
"sigh": 5,
"yo": 5,
"wokay": 0
}, {
"site_nm": "TR",
"date": "2019-10-01",
"sigh": 4,
"yo": 4,
"wokay": 0
}, {
"site_nm": "gs",
"date": "2019-10-01",
"sigh": 5,
"yo": 5,
"wokay": 2
}, {
"site_nm": "GB",
"date": "2019-10-01",
"sigh": 1,
"yo": 1,
"wokay": 1
}, {
"site_nm": "cn",
"date": "2019-10-01",
"sigh": 4,
"yo": 4,
"wokay": 1
}, {
"site_nm": "Roadshow",
"date": "2019-10-01",
"sigh": 1,
"yo": 1,
"wokay": 0
}, {
"site_nm": "TV",
"date": "2019-10-01",
"sigh": 1,
"yo": 1,
"wokay": 0
}, {
"site_nm": "ZD",
"date": "2019-10-01",
"sigh": 2,
"yo": 2,
"wokay": 0
}, {
"site_nm": "Carfection",
"date": "2019-10-01",
"sigh": 1,
"yo": 1,
"wokay": 0
}, {
"site_nm": "gs Trailers",
"date": "2019-10-01",
"sigh": 3,
"yo": 3,
"wokay": 0
}, {
"site_nm": "gs News",
"date": "2019-10-01",
"sigh": 3,
"yo": 3,
"wokay": 0
}, {
"site_nm": "gs Mobile",
"date": "2019-10-01",
"sigh": 1,
"yo": 1,
"wokay": 0
}, {
"site_nm": "gs Universe Trailers",
"date": "2019-10-02",
"sigh": 1,
"yo": 1,
"wokay": 0
}, {
"site_nm": "gs Gameplay",
"date": "2019-10-04",
"sigh": 6,
"yo": 6,
"wokay": 2
}, {
"site_nm": "Roadshow",
"date": "2019-10-02",
"sigh": 2,
"yo": 2,
"wokay": 0
}, {
"site_nm": "gs",
"date": "2019-10-02",
"sigh": 3,
"yo": 3,
"wokay": 2
}, {
"site_nm": "TR",
"date": "2019-10-02",
"sigh": 4,
"yo": 3,
"wokay": 0
}, {
"site_nm": "cn Highlights",
"date": "2019-10-03",
"sigh": 8,
"yo": 8,
"wokay": 0
}, {
"site_nm": "gs Universe",
"date": "2019-10-02",
"sigh": 1,
"yo": 1,
"wokay": 0
}, {
"site_nm": "cn en Espa\u00f1ol",
"date": "2019-10-02",
"sigh": 6,
"yo": 6,
"wokay": 0
}, {
"site_nm": "gs Trailers",
"date": "2019-10-02",
"sigh": 2,
"yo": 2,
"wokay": 0
}, {
"site_nm": "ZD",
"date": "2019-10-02",
"sigh": 4,
"yo": 4,
"wokay": 1
}, {
"site_nm": "cn",
"date": "2019-10-05",
"sigh": 5,
"yo": 5,
"wokay": 1
}, {
"site_nm": "TV",
"date": "2019-10-02",
"sigh": 1,
"yo": 1,
"wokay": 0
}, {
"site_nm": "CH",
"date": "2019-10-02",
"sigh": 1,
"yo": 1,
"wokay": 0
}, {
"site_nm": "GB",
"date": "2019-10-02",
"sigh": 3,
"yo": 3,
"wokay": 3
}, {
"site_nm": "DLNow",
"date": "2019-10-02",
"sigh": 1,
"yo": 0,
"wokay": 0
}, {
"site_nm": "gs News",
"date": "2019-10-02",
"sigh": 2,
"yo": 2,
"wokay": 0
}];
//sort by site_nm
function compare(a, b) {
const site_a = a.site_nm.toUpperCase();
const site_b = b.site_nm.toUpperCase();
let comparison = 0;
if (site_a > site_b) {
comparison = 1;
} else if (site_a < site_b) {
comparison = -1;
}
return comparison;
}
json = data.sort(compare);
console.log(json.length);
let sites = [...new Set(json.map(({
site_nm
}) => site_nm))].sort();
let dates = [...new Set(json.map(({
date
}) => date))].sort();
for (var date = moment(dates[0], "YYYY-MM-DD"); date <= moment(dates[dates.length - 1], "YYYY-MM-DD"); date = date.add(1, 'days')) {
// console.log(date);
var dateFormatted = date.format("YYYY-MM-DD");
console.log(dateFormatted);
function iterate(item) {
if (!dates.includes(dateFormatted)) {
json.push({
"site_nm": item,
"date": dateFormatted,
"sigh": "",
"yo": "",
"wokay": ""
});
}
}
}
sites.forEach(iterate);
console.log(json.length);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
它增加了我的日期,但是我无法通过site_nm添加它。对于每个网站,我要在对象中添加缺少的日期。我总共有18个不同的站点,总共有5天,所以我希望JSON的总长度为18 * 5 = 90。每个站点5个对象。如何为每个站点进行forEach?