我需要你的帮助, 我有一个数组,包含每月(12个月)执行任务的数量,如下所示:
[
["Ilhem", "Ben Salah", 4, 1],
["Ilhem", "Ben Salah", 3, 2],
["Ilhem", "Ben Salah", 3, 3],
["Ilhem", "Ben Salah", 3, 4],
["Sghaier", "Mahmoud", 1, 1]
]
现在我需要一个这样的数组:
[
["Ilhem", "Ben Salah", 4, 1],
["Ilhem", "Ben Salah", 0, 2],
["Ilhem", "Ben Salah", 0, 3],
["Ilhem", "Ben Salah", 0, 4],
["Ilhem", "Ben Salah", 0, 5],
["Ilhem", "Ben Salah", 0, 6],
["Ilhem", "Ben Salah", 0, 7],
["Ilhem", "Ben Salah", 0, 8],
["Ilhem", "Ben Salah", 0, 9],
["Ilhem", "Ben Salah", 0, 10],
["Ilhem", "Ben Salah", 0, 11],
["Ilhem", "Ben Salah", 0, 12],
["Sghaier", "Mahmoud", 7, 1],
["Sghaier", "Mahmoud", 0, 2],
["Sghaier", "Mahmoud", 0, 3],
["Sghaier", "Mahmoud", 0, 4],
["Sghaier", "Mahmoud", 0, 5],
["Sghaier", "Mahmoud", 0, 6],
["Sghaier", "Mahmoud", 0, 7],
["Sghaier", "Mahmoud", 0, 8],
["Sghaier", "Mahmoud", 0, 9],
["Sghaier", "Mahmoud", 0, 10],
["Sghaier", "Mahmoud", 0, 11],
["Sghaier", "Mahmoud", 0, 12]
如何继续请我很困惑。
答案 0 :(得分:1)
首先创建一个使用名称作为键的对象和一个子对象,使用月份数作为键来存储该名称的现有月份的数组
然后使用1-12的循环并查看该名称是否存在月份,如果不是
则填写缺失的项目
Crypto.HmacSHA256(regionName, kDate);
const kSecret = '41575334774a616c725855746e46454d492f4b374d44454e472b62507852666943594558414d504c454b4559',
dateStamp = '20120215'
function _binaryToHex(array) {
return array.map(b => ('00' + b.toString(16)).slice(-2)).join('');
}
/**
* https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/importKey
*/
async function _importKey(keyDataArrayBuffer) {
return crypto.subtle.importKey(
'raw',
keyDataArrayBuffer,
{'name': 'hmac', 'hash': {'name': 'SHA-256'}},
false,
['sign']);
}
const kDate = _binaryToHex(
Array.from(
new Uint8Array(
await crypto.subtle.sign(
'HMAC',
await _importKey(
new TextEncoder().encode(kSecret)
),
new TextEncoder().encode(dateStamp)
)
)
)
);
console.log(kDate);
/* Must be
* '969fbb94feb542b71ede6f87fe4d5fa29c789342b0f407474670f0c2489e0a0d'
*/
function fillMonthHoles(data) {
var groupByName = {}
data.forEach(function(arr) {
var baseArr = arr.slice(0, 2), nameKey = baseArr.join('|');
this[nameKey] = this[nameKey] || {baseArr : baseArr};
this[nameKey][arr[3]] = arr;
}, groupByName);
return Object.keys(groupByName).reduce(function(a, key) {
var baseArr = groupByName[key].baseArr
for (var i = 1; i < 13; i++) {
var arr = groupByName[key][i];
if (!arr) {
arr = baseArr.slice().concat([0, i]);
}
a.push(arr)
}
return a
}, []);
}
var res = fillMonthHoles(data);
// loop over results and log each subarray for demo only
res.forEach(a => console.log(JSON.stringify(a)))
答案 1 :(得分:0)
这很有效。
RunInstances
&#13;