gpt4 book ai didi

jquery 创建数组,其中包含天数和 OT 的总和,后跟项目编号

转载 作者:行者123 更新时间:2023-12-01 04:11:12 24 4
gpt4 key购买 nike

我想生成如下 JSON 数组。

完整详细的数组;

MyArray = [{ prj : "P1", days : "8", ot : "2" }, { prj : "P2", days : "8", ot : "2" }, { prj : "P2", days : "8", ot : "2" }, { prj : "P1", days : "8", ot : "2" }{prj : "P3", days : "8", ot : "2" }, { prj : "P2", days : "8", ot : "2" }, { prj : "P3", days : "8", ot : "2" }];

但我想将其压缩如下;

MyArray = [{ prj : "P1", days : "16", ot : "4" }, { prj : "P2", days : "24", ot : "6" }, { prj : "P3", days : "16", ot : "4" }];

如果project_number存在,应该得到天数和加类时间的总和;

See the example

最佳答案

试试这个,这会满足您的需要。

var MyArray = [];

//This will be used to construct your JSON object
function ConstructJson(prj, days, ot) {

var xObj = {};
xObj.prj = prj;
xObj.days = days;
xObj.ot = ot;

MyArray[MyArray.length] = xObj;

}

//This will minimise your json object.
function MinimiseJson() {

var xTempArray = [];
var xTempObj = {};
var xPrj = '';

for (var i = 0; i < MyArray.length; i++) {

if (xPrj == '' && MyArray[i]) {

xPrj = MyArray[i].prj;

xTempObj.prj = xPrj;
xTempObj.days = MyArray[i].days;
xTempObj.ot = MyArray[i].ot;

for (var j = i + 1; j < MyArray.length; j++) {

if (MyArray[j] && MyArray[j].prj == xPrj) {

xTempObj.days += MyArray[j].days;
xTempObj.ot += MyArray[j].ot;

MyArray[j] = null;
}

}

xPrj = '';
xTempArray[xTempArray.length] = xTempObj;
xTempObj = {};
}
}

return xTempArray;

}

LIVE - DEMO

关于jquery 创建数组,其中包含天数和 OT 的总和,后跟项目编号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20309369/

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