我有一系列交易,我想根据键timeSent
对交易进行分组。
但是timeSent
以毫秒为单位,并且是唯一的。我希望数据按日期分组。
如何可能根据键及其值对数组进行分组? 我应该如何更改我的功能?
const transactions = [
{
"transactionHash": "0x5677f30d52ccea78e03a80597f978561abf763ec32bc2b2b9b7e3013527c0066",
"toAddress": "0x933b3d3129d53a0173be68ae478902f41f78d9a9",
"fromAddress": "0x1b5a27f4db96580b6e1e35514d28572fb5b3843c",
"timeSent": 1534488092742,
"gasPrice": "0",
"gasLimit": "2000000",
"timeMined": 1534488101395,
"blockNumber": "699851",
"gasUsed": "28056",
"cumulativeGasUsed": "28056",
"action": "addSignatory",
"extraData": "documentId=69b1d781-7b56-4e97-9bc7-c6051df232ca",
"property": "178"
},
{
"transactionHash": "0x043d6f936d89dbe235dfa63c6cc511ddf82c3dc0d7c8a522adee3404ee72120b",
"toAddress": "0x933b3d3129d53a0173be68ae478902f41f78d9a9",
"fromAddress": "0x1b5a27f4db96580b6e1e35514d28572fb5b3843c",
"timeSent": 1534488050259,
"gasPrice": "0",
"gasLimit": "2000000",
"timeMined": 1534488057529,
"blockNumber": "699842",
"gasUsed": "31512",
"cumulativeGasUsed": "31512",
"action": "addDocument",
"extraData": "documentId=69b1d781-7b56-4e97-9bc7-c6051df232ca",
"property": "178"
},
{
"transactionHash": "0xc4a8aef93084733a2155a71ee934dd31466a1de268c77ca3b48e3edf5aaa70ac",
"toAddress": "0x933b3d3129d53a0173be68ae478902f41f78d9a9",
"fromAddress": "0x1b5a27f4db96580b6e1e35514d28572fb5b3843c",
"timeSent": 1534487694591,
"gasPrice": "0",
"gasLimit": "4300000",
"timeMined": 1534487695066,
"blockNumber": "699768",
"gasUsed": "697650",
"cumulativeGasUsed": "697650",
"action": "addSpatialUnit",
"extraData": "propertyId=1"
},
{
"transactionHash": "0x65a289854f5afc2a765e7217f1b5e1614a4318baa7ad9c298e2aa9f83cb4bce6",
"fromAddress": "0x08a863e866cd8fe15d3c1e45c5d66e85440d724f",
"timeSent": 1534485760789,
"gasPrice": "0",
"gasLimit": "2000000",
"timeMined": 1534485766989,
"blockNumber": "699384",
"gasUsed": "54344",
"cumulativeGasUsed": "54344",
"contractAddress": "0xfffd1812abccfd7e43ad326411060a3138ad7dfc",
"action": "createIdentity"
},
{
"transactionHash": "0x0146176d40fbe8c38ce904c18b5c20f9bc984c8694bdcac896707b0c7bbfd13a",
"fromAddress": "0x65cb3d6c18ddf6971a07bfae6e606eefafe2246d",
"timeSent": 1534485733545,
"gasPrice": "0",
"gasLimit": "2000000",
"timeMined": 1534485743949,
"blockNumber": "699379",
"gasUsed": "54344",
"cumulativeGasUsed": "54344",
"contractAddress": "0xf7238628f4f76f4bf15fea9621d83a414fc312c3",
"action": "createIdentity"
},
{
"transactionHash": "0x185381e0546607164ba53daa0080ec632be4362fbfc218e34cfeef139f1221e7",
"fromAddress": "0xafafd671d866271578ce6490a54b60f1a30d3736",
"timeSent": 1534485713502,
"gasPrice": "0",
"gasLimit": "2000000",
"timeMined": 1534485722077,
"blockNumber": "699375",
"gasUsed": "54344",
"cumulativeGasUsed": "54344",
"contractAddress": "0x0edf346fb0021d3534a1522994ce16adc63f8c27",
"action": "createIdentity"
},
{
"transactionHash": "0x12d2d340428fe68da8e9c92ba87c71c765bc9048fa93b8b13be6e87f968b9130",
"fromAddress": "0x6db1dd4b5bee10d98300addf827145992a0b0bc6",
"timeSent": 1534485695572,
"gasPrice": "0",
"gasLimit": "2000000",
"timeMined": 1534485701597,
"blockNumber": "699371",
"gasUsed": "54344",
"cumulativeGasUsed": "54344",
"contractAddress": "0xf816368997e1dbfedf8de4da4ae834b566619fac",
"action": "createIdentity"
},
{
"transactionHash": "0xb10fb02b1fdaa99a69f178bf2a53559e4c1f4cee11f80e5d38ed27c0feebf3be",
"fromAddress": "0x3e1d3eb628da78ce87dac0c0fdb518b6630d5a1b",
"timeSent": 1534485668659,
"gasPrice": "0",
"gasLimit": "2000000",
"timeMined": 1534485677395,
"blockNumber": "699366",
"gasUsed": "54344",
"cumulativeGasUsed": "54344",
"contractAddress": "0x1d22cae5b6376cf2ec82ade5b8c2090df745948e",
"action": "createIdentity"
},
{
"transactionHash": "0xbd42f9ce7a302c5e227e8186194b48a9cafed49ee819dcbec59848525996368a",
"fromAddress": "0xb1506cebb611394e84b731d6d857d6885108cbfb",
"timeSent": 1534485648476,
"gasPrice": "0",
"gasLimit": "2000000",
"timeMined": 1534485656795,
"blockNumber": "699362",
"gasUsed": "54344",
"cumulativeGasUsed": "54344",
"contractAddress": "0x3468040e6060208fb3c16a12fd10b53e9fc5da9c",
"action": "createIdentity"
},
{
"transactionHash": "0xb66a693d2258e2270a602404cf1680e93757abacce16ce04e5ab17c35ef21c4f",
"fromAddress": "0x0541be5fb74771dcc95ca6f99a651d635d618ddb",
"timeSent": 1534485630149,
"gasPrice": "0",
"gasLimit": "2000000",
"timeMined": 1534485637442,
"blockNumber": "699358",
"gasUsed": "54344",
"cumulativeGasUsed": "54344",
"contractAddress": "0x85c2852c1cf8d2f8a4e73dee16116b48e378d6ba",
"action": "createIdentity"
},
{
"transactionHash": "0x79a6a4db781c9d7d4a75f66e4501b854c589679f797baa73695cdb1fda9557fd",
"fromAddress": "0xcd1955bcf1cebc22e51e2d96d54fd1a4b1909985",
"timeSent": 1534485599691,
"gasPrice": "0",
"gasLimit": "2000000",
"timeMined": 1534485606434,
"blockNumber": "699352",
"gasUsed": "54344",
"cumulativeGasUsed": "54344",
"contractAddress": "0xf57e8580b4cf17adb7b557bb6e11e76a951eeeb1",
"action": "createIdentity"
},
{
"transactionHash": "0x192661f41251bfc7cef9dbdccc84b367f2ee77553c10427262eaaeda01baf8cc",
"fromAddress": "0x1b5a27f4db96580b6e1e35514d28572fb5b3843c",
"timeSent": 1534485568393,
"gasPrice": "0",
"gasLimit": "2000000",
"timeMined": 1534485576474,
"blockNumber": "699346",
"gasUsed": "54344",
"cumulativeGasUsed": "54344",
"contractAddress": "0x933b3d3129d53a0173be68ae478902f41f78d9a9",
"action": "createIdentity"
}
]
console.log(
_.groupBy(transactions,'timeSent'))
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js"></script>
。
答案 0 :(得分:2)
因为您已经使用 lodash ,所以只需要在第二个参数中使用 function 并返回要分组的值即可。
const transactions = getTransactions();
// Map (keys are real Dates)
let transactionsMap = new Map();
transactions.forEach( value => transactionsMap.set(new Date(value.timeSent), value) );
const firstEntry = transactionsMap.entries().next().value;
console.log("transactionsMap first entry:", firstEntry);
console.log("First entry of transactionsMap: key is Date?", firstEntry[0] instanceof Date);
// reducer (keys are strings)
console.log(transactions.reduce( (reduced, value) =>
({...reduced, [new Date(value.timeSent).toUTCString()]: value}), {}));
function getTransactions() {
return [
{
"transactionHash": "0x5677f30d52ccea78e03a80597f978561abf763ec32bc2b2b9b7e3013527c0066",
"timeSent": 1534488092742,
},
{
"transactionHash": "0x043d6f936d89dbe235dfa63c6cc511ddf82c3dc0d7c8a522adee3404ee72120b",
"timeSent": 1534488050259,
},
{
"transactionHash": "0xc4a8aef93084733a2155a71ee934dd31466a1de268c77ca3b48e3edf5aaa70ac",
"timeSent": 1534487694591,
},
{
"transactionHash": "0x65a289854f5afc2a765e7217f1b5e1614a4318baa7ad9c298e2aa9f83cb4bce6",
"timeSent": 1534485760789,
},
{
"transactionHash": "0x0146176d40fbe8c38ce904c18b5c20f9bc984c8694bdcac896707b0c7bbfd13a",
"timeSent": 1534485733545,
},
{
"transactionHash": "0x185381e0546607164ba53daa0080ec632be4362fbfc218e34cfeef139f1221e7",
"timeSent": 1534485713502,
},
{
"transactionHash": "0x12d2d340428fe68da8e9c92ba87c71c765bc9048fa93b8b13be6e87f968b9130",
"timeSent": 1534485695572,
},
{
"transactionHash": "0xb10fb02b1fdaa99a69f178bf2a53559e4c1f4cee11f80e5d38ed27c0feebf3be",
"timeSent": 1534485668659,
},
{
"transactionHash": "0xbd42f9ce7a302c5e227e8186194b48a9cafed49ee819dcbec59848525996368a",
"timeSent": 1534485648476,
},
{
"transactionHash": "0xb66a693d2258e2270a602404cf1680e93757abacce16ce04e5ab17c35ef21c4f",
"timeSent": 1534485630149,
},
{
"transactionHash": "0x79a6a4db781c9d7d4a75f66e4501b854c589679f797baa73695cdb1fda9557fd",
"timeSent": 1534485599691,
},
{
"transactionHash": "0x192661f41251bfc7cef9dbdccc84b367f2ee77553c10427262eaaeda01baf8cc",
"timeSent": 1534485568393,
}
];
}
_.groupBy(transactions,({timeSent}) => new Date(timeSent).toDateString())
const transactions = [
{
"transactionHash": "0x5677f30d52ccea78e03a80597f978561abf763ec32bc2b2b9b7e3013527c0066",
"toAddress": "0x933b3d3129d53a0173be68ae478902f41f78d9a9",
"fromAddress": "0x1b5a27f4db96580b6e1e35514d28572fb5b3843c",
"timeSent": 1534488092742,
"gasPrice": "0",
"gasLimit": "2000000",
"timeMined": 1534488101395,
"blockNumber": "699851",
"gasUsed": "28056",
"cumulativeGasUsed": "28056",
"action": "addSignatory",
"extraData": "documentId=69b1d781-7b56-4e97-9bc7-c6051df232ca",
"property": "178"
},
{
"transactionHash": "0x043d6f936d89dbe235dfa63c6cc511ddf82c3dc0d7c8a522adee3404ee72120b",
"toAddress": "0x933b3d3129d53a0173be68ae478902f41f78d9a9",
"fromAddress": "0x1b5a27f4db96580b6e1e35514d28572fb5b3843c",
"timeSent": 1534488050259,
"gasPrice": "0",
"gasLimit": "2000000",
"timeMined": 1534488057529,
"blockNumber": "699842",
"gasUsed": "31512",
"cumulativeGasUsed": "31512",
"action": "addDocument",
"extraData": "documentId=69b1d781-7b56-4e97-9bc7-c6051df232ca",
"property": "178"
},
{
"transactionHash": "0xc4a8aef93084733a2155a71ee934dd31466a1de268c77ca3b48e3edf5aaa70ac",
"toAddress": "0x933b3d3129d53a0173be68ae478902f41f78d9a9",
"fromAddress": "0x1b5a27f4db96580b6e1e35514d28572fb5b3843c",
"timeSent": 1534487694591,
"gasPrice": "0",
"gasLimit": "4300000",
"timeMined": 1534487695066,
"blockNumber": "699768",
"gasUsed": "697650",
"cumulativeGasUsed": "697650",
"action": "addSpatialUnit",
"extraData": "propertyId=1"
},
{
"transactionHash": "0x65a289854f5afc2a765e7217f1b5e1614a4318baa7ad9c298e2aa9f83cb4bce6",
"fromAddress": "0x08a863e866cd8fe15d3c1e45c5d66e85440d724f",
"timeSent": 1534485760789,
"gasPrice": "0",
"gasLimit": "2000000",
"timeMined": 1534485766989,
"blockNumber": "699384",
"gasUsed": "54344",
"cumulativeGasUsed": "54344",
"contractAddress": "0xfffd1812abccfd7e43ad326411060a3138ad7dfc",
"action": "createIdentity"
},
{
"transactionHash": "0x0146176d40fbe8c38ce904c18b5c20f9bc984c8694bdcac896707b0c7bbfd13a",
"fromAddress": "0x65cb3d6c18ddf6971a07bfae6e606eefafe2246d",
"timeSent": 1534485733545,
"gasPrice": "0",
"gasLimit": "2000000",
"timeMined": 1534485743949,
"blockNumber": "699379",
"gasUsed": "54344",
"cumulativeGasUsed": "54344",
"contractAddress": "0xf7238628f4f76f4bf15fea9621d83a414fc312c3",
"action": "createIdentity"
},
{
"transactionHash": "0x185381e0546607164ba53daa0080ec632be4362fbfc218e34cfeef139f1221e7",
"fromAddress": "0xafafd671d866271578ce6490a54b60f1a30d3736",
"timeSent": 1534485713502,
"gasPrice": "0",
"gasLimit": "2000000",
"timeMined": 1534485722077,
"blockNumber": "699375",
"gasUsed": "54344",
"cumulativeGasUsed": "54344",
"contractAddress": "0x0edf346fb0021d3534a1522994ce16adc63f8c27",
"action": "createIdentity"
},
{
"transactionHash": "0x12d2d340428fe68da8e9c92ba87c71c765bc9048fa93b8b13be6e87f968b9130",
"fromAddress": "0x6db1dd4b5bee10d98300addf827145992a0b0bc6",
"timeSent": 1534485695572,
"gasPrice": "0",
"gasLimit": "2000000",
"timeMined": 1534485701597,
"blockNumber": "699371",
"gasUsed": "54344",
"cumulativeGasUsed": "54344",
"contractAddress": "0xf816368997e1dbfedf8de4da4ae834b566619fac",
"action": "createIdentity"
},
{
"transactionHash": "0xb10fb02b1fdaa99a69f178bf2a53559e4c1f4cee11f80e5d38ed27c0feebf3be",
"fromAddress": "0x3e1d3eb628da78ce87dac0c0fdb518b6630d5a1b",
"timeSent": 1534485668659,
"gasPrice": "0",
"gasLimit": "2000000",
"timeMined": 1534485677395,
"blockNumber": "699366",
"gasUsed": "54344",
"cumulativeGasUsed": "54344",
"contractAddress": "0x1d22cae5b6376cf2ec82ade5b8c2090df745948e",
"action": "createIdentity"
},
{
"transactionHash": "0xbd42f9ce7a302c5e227e8186194b48a9cafed49ee819dcbec59848525996368a",
"fromAddress": "0xb1506cebb611394e84b731d6d857d6885108cbfb",
"timeSent": 1534485648476,
"gasPrice": "0",
"gasLimit": "2000000",
"timeMined": 1534485656795,
"blockNumber": "699362",
"gasUsed": "54344",
"cumulativeGasUsed": "54344",
"contractAddress": "0x3468040e6060208fb3c16a12fd10b53e9fc5da9c",
"action": "createIdentity"
},
{
"transactionHash": "0xb66a693d2258e2270a602404cf1680e93757abacce16ce04e5ab17c35ef21c4f",
"fromAddress": "0x0541be5fb74771dcc95ca6f99a651d635d618ddb",
"timeSent": 1534485630149,
"gasPrice": "0",
"gasLimit": "2000000",
"timeMined": 1534485637442,
"blockNumber": "699358",
"gasUsed": "54344",
"cumulativeGasUsed": "54344",
"contractAddress": "0x85c2852c1cf8d2f8a4e73dee16116b48e378d6ba",
"action": "createIdentity"
},
{
"transactionHash": "0x79a6a4db781c9d7d4a75f66e4501b854c589679f797baa73695cdb1fda9557fd",
"fromAddress": "0xcd1955bcf1cebc22e51e2d96d54fd1a4b1909985",
"timeSent": 1534485599691,
"gasPrice": "0",
"gasLimit": "2000000",
"timeMined": 1534485606434,
"blockNumber": "699352",
"gasUsed": "54344",
"cumulativeGasUsed": "54344",
"contractAddress": "0xf57e8580b4cf17adb7b557bb6e11e76a951eeeb1",
"action": "createIdentity"
},
{
"transactionHash": "0x192661f41251bfc7cef9dbdccc84b367f2ee77553c10427262eaaeda01baf8cc",
"fromAddress": "0x1b5a27f4db96580b6e1e35514d28572fb5b3843c",
"timeSent": 1534485568393,
"gasPrice": "0",
"gasLimit": "2000000",
"timeMined": 1534485576474,
"blockNumber": "699346",
"gasUsed": "54344",
"cumulativeGasUsed": "54344",
"contractAddress": "0x933b3d3129d53a0173be68ae478902f41f78d9a9",
"action": "createIdentity"
}
]
console.log(_.groupBy(transactions,({timeSent}) => new Date(timeSent).toDateString()))
答案 1 :(得分:1)
如果要按天分组,则可以执行以下操作:
_.groupBy(transactions, function(trans) {
var dt = new Date(trans.timeSent);
return dt.getFullYear() + "/" + (dt.getMonth() + 1) + "/" + dt.getDate();
})
答案 2 :(得分:1)
这很简单。只需传递将按日期生成密钥的分组标准作为函数,然后将其传递给groupBy
函数即可。
const transactions = [
{
"transactionHash": "0x5677f30d52ccea78e03a80597f978561abf763ec32bc2b2b9b7e3013527c0066",
"toAddress": "0x933b3d3129d53a0173be68ae478902f41f78d9a9",
"fromAddress": "0x1b5a27f4db96580b6e1e35514d28572fb5b3843c",
"timeSent": 1534488092742,
"gasPrice": "0",
"gasLimit": "2000000",
"timeMined": 1534488101395,
"blockNumber": "699851",
"gasUsed": "28056",
"cumulativeGasUsed": "28056",
"action": "addSignatory",
"extraData": "documentId=69b1d781-7b56-4e97-9bc7-c6051df232ca",
"property": "178"
},
{
"transactionHash": "0x043d6f936d89dbe235dfa63c6cc511ddf82c3dc0d7c8a522adee3404ee72120b",
"toAddress": "0x933b3d3129d53a0173be68ae478902f41f78d9a9",
"fromAddress": "0x1b5a27f4db96580b6e1e35514d28572fb5b3843c",
"timeSent": 1534488050259,
"gasPrice": "0",
"gasLimit": "2000000",
"timeMined": 1534488057529,
"blockNumber": "699842",
"gasUsed": "31512",
"cumulativeGasUsed": "31512",
"action": "addDocument",
"extraData": "documentId=69b1d781-7b56-4e97-9bc7-c6051df232ca",
"property": "178"
},
{
"transactionHash": "0xc4a8aef93084733a2155a71ee934dd31466a1de268c77ca3b48e3edf5aaa70ac",
"toAddress": "0x933b3d3129d53a0173be68ae478902f41f78d9a9",
"fromAddress": "0x1b5a27f4db96580b6e1e35514d28572fb5b3843c",
"timeSent": 1534487694591,
"gasPrice": "0",
"gasLimit": "4300000",
"timeMined": 1534487695066,
"blockNumber": "699768",
"gasUsed": "697650",
"cumulativeGasUsed": "697650",
"action": "addSpatialUnit",
"extraData": "propertyId=1"
},
{
"transactionHash": "0x65a289854f5afc2a765e7217f1b5e1614a4318baa7ad9c298e2aa9f83cb4bce6",
"fromAddress": "0x08a863e866cd8fe15d3c1e45c5d66e85440d724f",
"timeSent": 1534485760789,
"gasPrice": "0",
"gasLimit": "2000000",
"timeMined": 1534485766989,
"blockNumber": "699384",
"gasUsed": "54344",
"cumulativeGasUsed": "54344",
"contractAddress": "0xfffd1812abccfd7e43ad326411060a3138ad7dfc",
"action": "createIdentity"
},
{
"transactionHash": "0x0146176d40fbe8c38ce904c18b5c20f9bc984c8694bdcac896707b0c7bbfd13a",
"fromAddress": "0x65cb3d6c18ddf6971a07bfae6e606eefafe2246d",
"timeSent": 1534485733545,
"gasPrice": "0",
"gasLimit": "2000000",
"timeMined": 1534485743949,
"blockNumber": "699379",
"gasUsed": "54344",
"cumulativeGasUsed": "54344",
"contractAddress": "0xf7238628f4f76f4bf15fea9621d83a414fc312c3",
"action": "createIdentity"
},
{
"transactionHash": "0x185381e0546607164ba53daa0080ec632be4362fbfc218e34cfeef139f1221e7",
"fromAddress": "0xafafd671d866271578ce6490a54b60f1a30d3736",
"timeSent": 1534485713502,
"gasPrice": "0",
"gasLimit": "2000000",
"timeMined": 1534485722077,
"blockNumber": "699375",
"gasUsed": "54344",
"cumulativeGasUsed": "54344",
"contractAddress": "0x0edf346fb0021d3534a1522994ce16adc63f8c27",
"action": "createIdentity"
},
{
"transactionHash": "0x12d2d340428fe68da8e9c92ba87c71c765bc9048fa93b8b13be6e87f968b9130",
"fromAddress": "0x6db1dd4b5bee10d98300addf827145992a0b0bc6",
"timeSent": 1534485695572,
"gasPrice": "0",
"gasLimit": "2000000",
"timeMined": 1534485701597,
"blockNumber": "699371",
"gasUsed": "54344",
"cumulativeGasUsed": "54344",
"contractAddress": "0xf816368997e1dbfedf8de4da4ae834b566619fac",
"action": "createIdentity"
},
{
"transactionHash": "0xb10fb02b1fdaa99a69f178bf2a53559e4c1f4cee11f80e5d38ed27c0feebf3be",
"fromAddress": "0x3e1d3eb628da78ce87dac0c0fdb518b6630d5a1b",
"timeSent": 1534485668659,
"gasPrice": "0",
"gasLimit": "2000000",
"timeMined": 1534485677395,
"blockNumber": "699366",
"gasUsed": "54344",
"cumulativeGasUsed": "54344",
"contractAddress": "0x1d22cae5b6376cf2ec82ade5b8c2090df745948e",
"action": "createIdentity"
},
{
"transactionHash": "0xbd42f9ce7a302c5e227e8186194b48a9cafed49ee819dcbec59848525996368a",
"fromAddress": "0xb1506cebb611394e84b731d6d857d6885108cbfb",
"timeSent": 1534485648476,
"gasPrice": "0",
"gasLimit": "2000000",
"timeMined": 1534485656795,
"blockNumber": "699362",
"gasUsed": "54344",
"cumulativeGasUsed": "54344",
"contractAddress": "0x3468040e6060208fb3c16a12fd10b53e9fc5da9c",
"action": "createIdentity"
},
{
"transactionHash": "0xb66a693d2258e2270a602404cf1680e93757abacce16ce04e5ab17c35ef21c4f",
"fromAddress": "0x0541be5fb74771dcc95ca6f99a651d635d618ddb",
"timeSent": 1534485630149,
"gasPrice": "0",
"gasLimit": "2000000",
"timeMined": 1534485637442,
"blockNumber": "699358",
"gasUsed": "54344",
"cumulativeGasUsed": "54344",
"contractAddress": "0x85c2852c1cf8d2f8a4e73dee16116b48e378d6ba",
"action": "createIdentity"
},
{
"transactionHash": "0x79a6a4db781c9d7d4a75f66e4501b854c589679f797baa73695cdb1fda9557fd",
"fromAddress": "0xcd1955bcf1cebc22e51e2d96d54fd1a4b1909985",
"timeSent": 1534485599691,
"gasPrice": "0",
"gasLimit": "2000000",
"timeMined": 1534485606434,
"blockNumber": "699352",
"gasUsed": "54344",
"cumulativeGasUsed": "54344",
"contractAddress": "0xf57e8580b4cf17adb7b557bb6e11e76a951eeeb1",
"action": "createIdentity"
},
{
"transactionHash": "0x192661f41251bfc7cef9dbdccc84b367f2ee77553c10427262eaaeda01baf8cc",
"fromAddress": "0x1b5a27f4db96580b6e1e35514d28572fb5b3843c",
"timeSent": 1534485568393,
"gasPrice": "0",
"gasLimit": "2000000",
"timeMined": 1534485576474,
"blockNumber": "699346",
"gasUsed": "54344",
"cumulativeGasUsed": "54344",
"contractAddress": "0x933b3d3129d53a0173be68ae478902f41f78d9a9",
"action": "createIdentity"
}
]
console.log(_.groupBy(transactions, t => {
const d = new Date(t.timeSent)
return d.getDate()+'-'+d.getMonth()+'-'+d.getFullYear()
}))
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js"></script>
答案 3 :(得分:0)
您可以通过循环到分组结果来为预期的输出创建新的最终数组。像这样:
const transactions = [{
"transactionHash": "0x5677f30d52ccea78e03a80597f978561abf763ec32bc2b2b9b7e3013527c0066",
"toAddress": "0x933b3d3129d53a0173be68ae478902f41f78d9a9",
"fromAddress": "0x1b5a27f4db96580b6e1e35514d28572fb5b3843c",
"timeSent": 1534488092742,
"gasPrice": "0",
"gasLimit": "2000000",
"timeMined": 1534488101395,
"blockNumber": "699851",
"gasUsed": "28056",
"cumulativeGasUsed": "28056",
"action": "addSignatory",
"extraData": "documentId=69b1d781-7b56-4e97-9bc7-c6051df232ca",
"property": "178"
},
{
"transactionHash": "0x043d6f936d89dbe235dfa63c6cc511ddf82c3dc0d7c8a522adee3404ee72120b",
"toAddress": "0x933b3d3129d53a0173be68ae478902f41f78d9a9",
"fromAddress": "0x1b5a27f4db96580b6e1e35514d28572fb5b3843c",
"timeSent": 1534488050259,
"gasPrice": "0",
"gasLimit": "2000000",
"timeMined": 1534488057529,
"blockNumber": "699842",
"gasUsed": "31512",
"cumulativeGasUsed": "31512",
"action": "addDocument",
"extraData": "documentId=69b1d781-7b56-4e97-9bc7-c6051df232ca",
"property": "178"
},
{
"transactionHash": "0xc4a8aef93084733a2155a71ee934dd31466a1de268c77ca3b48e3edf5aaa70ac",
"toAddress": "0x933b3d3129d53a0173be68ae478902f41f78d9a9",
"fromAddress": "0x1b5a27f4db96580b6e1e35514d28572fb5b3843c",
"timeSent": 1534487694591,
"gasPrice": "0",
"gasLimit": "4300000",
"timeMined": 1534487695066,
"blockNumber": "699768",
"gasUsed": "697650",
"cumulativeGasUsed": "697650",
"action": "addSpatialUnit",
"extraData": "propertyId=1"
},
{
"transactionHash": "0x65a289854f5afc2a765e7217f1b5e1614a4318baa7ad9c298e2aa9f83cb4bce6",
"fromAddress": "0x08a863e866cd8fe15d3c1e45c5d66e85440d724f",
"timeSent": 1534485760789,
"gasPrice": "0",
"gasLimit": "2000000",
"timeMined": 1534485766989,
"blockNumber": "699384",
"gasUsed": "54344",
"cumulativeGasUsed": "54344",
"contractAddress": "0xfffd1812abccfd7e43ad326411060a3138ad7dfc",
"action": "createIdentity"
},
{
"transactionHash": "0x0146176d40fbe8c38ce904c18b5c20f9bc984c8694bdcac896707b0c7bbfd13a",
"fromAddress": "0x65cb3d6c18ddf6971a07bfae6e606eefafe2246d",
"timeSent": 1534485733545,
"gasPrice": "0",
"gasLimit": "2000000",
"timeMined": 1534485743949,
"blockNumber": "699379",
"gasUsed": "54344",
"cumulativeGasUsed": "54344",
"contractAddress": "0xf7238628f4f76f4bf15fea9621d83a414fc312c3",
"action": "createIdentity"
},
{
"transactionHash": "0x185381e0546607164ba53daa0080ec632be4362fbfc218e34cfeef139f1221e7",
"fromAddress": "0xafafd671d866271578ce6490a54b60f1a30d3736",
"timeSent": 1534485713502,
"gasPrice": "0",
"gasLimit": "2000000",
"timeMined": 1534485722077,
"blockNumber": "699375",
"gasUsed": "54344",
"cumulativeGasUsed": "54344",
"contractAddress": "0x0edf346fb0021d3534a1522994ce16adc63f8c27",
"action": "createIdentity"
},
{
"transactionHash": "0x12d2d340428fe68da8e9c92ba87c71c765bc9048fa93b8b13be6e87f968b9130",
"fromAddress": "0x6db1dd4b5bee10d98300addf827145992a0b0bc6",
"timeSent": 1534485695572,
"gasPrice": "0",
"gasLimit": "2000000",
"timeMined": 1534485701597,
"blockNumber": "699371",
"gasUsed": "54344",
"cumulativeGasUsed": "54344",
"contractAddress": "0xf816368997e1dbfedf8de4da4ae834b566619fac",
"action": "createIdentity"
},
{
"transactionHash": "0xb10fb02b1fdaa99a69f178bf2a53559e4c1f4cee11f80e5d38ed27c0feebf3be",
"fromAddress": "0x3e1d3eb628da78ce87dac0c0fdb518b6630d5a1b",
"timeSent": 1534485668659,
"gasPrice": "0",
"gasLimit": "2000000",
"timeMined": 1534485677395,
"blockNumber": "699366",
"gasUsed": "54344",
"cumulativeGasUsed": "54344",
"contractAddress": "0x1d22cae5b6376cf2ec82ade5b8c2090df745948e",
"action": "createIdentity"
},
{
"transactionHash": "0xbd42f9ce7a302c5e227e8186194b48a9cafed49ee819dcbec59848525996368a",
"fromAddress": "0xb1506cebb611394e84b731d6d857d6885108cbfb",
"timeSent": 1534485648476,
"gasPrice": "0",
"gasLimit": "2000000",
"timeMined": 1534485656795,
"blockNumber": "699362",
"gasUsed": "54344",
"cumulativeGasUsed": "54344",
"contractAddress": "0x3468040e6060208fb3c16a12fd10b53e9fc5da9c",
"action": "createIdentity"
},
{
"transactionHash": "0xb66a693d2258e2270a602404cf1680e93757abacce16ce04e5ab17c35ef21c4f",
"fromAddress": "0x0541be5fb74771dcc95ca6f99a651d635d618ddb",
"timeSent": 1534485630149,
"gasPrice": "0",
"gasLimit": "2000000",
"timeMined": 1534485637442,
"blockNumber": "699358",
"gasUsed": "54344",
"cumulativeGasUsed": "54344",
"contractAddress": "0x85c2852c1cf8d2f8a4e73dee16116b48e378d6ba",
"action": "createIdentity"
},
{
"transactionHash": "0x79a6a4db781c9d7d4a75f66e4501b854c589679f797baa73695cdb1fda9557fd",
"fromAddress": "0xcd1955bcf1cebc22e51e2d96d54fd1a4b1909985",
"timeSent": 1534485599691,
"gasPrice": "0",
"gasLimit": "2000000",
"timeMined": 1534485606434,
"blockNumber": "699352",
"gasUsed": "54344",
"cumulativeGasUsed": "54344",
"contractAddress": "0xf57e8580b4cf17adb7b557bb6e11e76a951eeeb1",
"action": "createIdentity"
},
{
"transactionHash": "0x192661f41251bfc7cef9dbdccc84b367f2ee77553c10427262eaaeda01baf8cc",
"fromAddress": "0x1b5a27f4db96580b6e1e35514d28572fb5b3843c",
"timeSent": 1534485568393,
"gasPrice": "0",
"gasLimit": "2000000",
"timeMined": 1534485576474,
"blockNumber": "699346",
"gasUsed": "54344",
"cumulativeGasUsed": "54344",
"contractAddress": "0x933b3d3129d53a0173be68ae478902f41f78d9a9",
"action": "createIdentity"
}
];
var group = _.groupBy(transactions, 'timeSent');
var res = [];
Object.keys(group).forEach(key => res.push({[new Date(+key).toISOString()]: group[key]}));
console.log(res);
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js"></script>
答案 4 :(得分:0)