gpt4 book ai didi

javascript - 将缺失的键、值和数据添加到 json 对象

转载 作者:行者123 更新时间:2023-11-28 03:27:50 24 4
gpt4 key购买 nike

我有一大块动态变化的 json 数据,但是。每个 JSON 对象都有一个日期、site_nm 和更多值。日期并不适合所有人。此数据集中的不同日期为:2019-10-01、2019-10-02、2019-10-03、2019-10-04、2019-10-05。例如,在此数据集中,这是一个对象具有:{"site_nm": "GB", "date": "2019-10-01", "sigh": 1, "yo": 1, "wokay ": 1}{"site_nm": "GB", "date": "2019-10-03", "sigh": 3, "yo": 1, "wokay": 0 } 但在 2019-10-03,2019-10-04,2019-10-05 上没有值。无论如何,是否可以为每个站点添加缺失的日期,并将叹息、哟和沃凯的值设置为空?

例如,对于网站:GB 和日期:2019-10-01,2019-10-02,2019-10-03,2019-10-04,2019-10-05

我可以为每个站点完成数据集,使其看起来类似于:

  • {"site_nm": "GB", "date": "2019-10-01", "sigh": 1, "yo": 1, "wokay": 1}
  • {"site_nm": "GB", "date": "2019-10-02", "sigh": '', "yo": '', "wokay": ''}
  • {"site_nm": "GB", "date": "2019-10-03", "sigh": 3, "yo": 1, "wokay": 0}
  • {"site_nm": "GB", "date": "2019-10-04", "sigh": '', "yo": '', "wokay": ''}
  • {"site_nm": "GB", "date": "2019-10-05", "sigh": '', "yo": '', "wokay": ''}

我可以使用 inf reduce 根据站点名称分隔值。但我怎样才能添加缺失的值呢?

const comp = (key, order = 'asc') => {
return function(a, b) {
if (!a.hasOwnProperty(key) || !b.hasOwnProperty(key)) {
return 0;
}

const varA = (typeof a[key] === 'string') ?
a[key].toUpperCase() : a[key];
const varB = (typeof b[key] === 'string') ?
b[key].toUpperCase() : b[key];

let comparison = 0;
if (varA > varB) {
comparison = 1;
} else if (varA < varB) {
comparison = -1;
}
return (
(order === 'desc') ? (comparison * -1) : comparison
);
};
}

const convert_date = function(d) {
return moment(d.date, 'YYYY-MM-DD').unix() * 1000;
};

let json = [{"site_nm": "gs Universe", "date": "2019-10-01", "sigh": 1, "yo": 1, "wokay": 0}, {"site_nm": "gs GP", "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": "RS", "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": "Car", "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 GP", "date": "2019-10-04", "sigh": 6, "yo": 6, "wokay": 2}, {"site_nm": "RS", "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}]

let sites = [...new Set(json.map(({
site_nm
}) => site_nm))].sort();
let dates = [...new Set(json.map(({
date
}) => date))].sort();
// console.log(dates);
// console.log(sites);
$('#sites').text(sites);
$('#dates').text(dates);


series = sites.reduce((a, c, i) => {
console.log(a);
cur_data = json.filter(d => {
return d.site_nm === c
}).sort(comp('date'));

return [...a, {
name: c.replace(/_/g, ' '),
data: cur_data.reduce((aa, cc) => {
return [...aa, {
x: +convert_date(cc),
y: +cc.sigh,
yo: +cc.yo, wokay: +cc.wokay
}];
}, [])
}];
}, []);



<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>

<p id='sites'></p>
<p id='dates'></p>
<div id="stuff"></div>

最佳答案

只需提取所有日期,从最低到最高迭代并检查它是否存在

for (var date = moment("2019-10-01", "YYYY-MM-DD"); date <= moment("2019-10-05", "YYYY-MM-DD"); date = date.add(1, 'days'))
console.log(date.format("YYYY-MM-DD"));

var data = [
{"site_nm": "GB", "date": "2019-10-01", "sigh": 1, "yo": 1, "wokay": 1},
{"site_nm": "GB", "date": "2019-10-03", "sigh": 3, "yo": 1, "wokay": 0}
];

var existingDates = new Set(data.map(x => x.date));

for (
var date = moment("2019-10-01", "YYYY-MM-DD");
date <= moment("2019-10-05", "YYYY-MM-DD");
date = date.add(1, 'days')) {
var dateFormatted = date.format("YYYY-MM-DD");
if (!existingDates.has(dateFormatted)) {
data.push({"site_nm": "GB", "date": dateFormatted, "sigh": "", "yo": "", "wokay": ""});
}
}

console.log(data.sort((a, b) => moment(a.date, "YYYY-MM-DD") - moment(b.date, "YYYY-MM-DD")));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>

关于javascript - 将缺失的键、值和数据添加到 json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58469585/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com