I have following api data:
{
"status": "1",
"message": "OK",
"result": [
{
"blockNumber": "14376792",
"timeStamp": "1642218997",
"hash": "0xe7a8a5793d1e85ec6f6de891f8be9bee2bf455b086236a98365be00febb8dec9",
"nonce": "852",
"blockHash": "0x45b51bbac31bd1aef3215db88d05faa1172f9b9b7cb1f8f570b67e0bcef4e954",
"from": "0x6d038b92c6aa2fc4db2ca80dc9dc5d63ddafe33d",
"contractAddress": "0xb46acb1f8d0ff6369c2f00146897aea1dfcf2414",
"to": "0x931b22a138893258c58f3e4143b17086a97862f6",
"value": "72405000000000000000",
"tokenName": "Andromeda V2",
"tokenSymbol": "M31",
"tokenDecimal": "18",
"transactionIndex": "25",
"gas": "266039",
"gasPrice": "6000000000",
"gasUsed": "201266",
"cumulativeGasUsed": "2552933",
"input": "deprecated",
"confirmations": "670"
},
{
"blockNumber": "14376072",
"timeStamp": "1642216837",
"hash": "0x3ffe38506478a3a81ac39933d46521aa0624cd8b89bc36f79f20c2d0b81c5f92",
"nonce": "9",
"blockHash": "0x672eeba6792d41c298057fefd3a31b1ab8acf8b877d8ef3f4c790234f3b4dd1b",
"from": "0xb46acb1f8d0ff6369c2f00146897aea1dfcf2414",
"contractAddress": "0xb46acb1f8d0ff6369c2f00146897aea1dfcf2414",
"to": "0x931b22a138893258c58f3e4143b17086a97862f6",
"value": "1917760707567583701190",
"tokenName": "Andromeda V2",
"tokenSymbol": "M31",
"tokenDecimal": "18",
"transactionIndex": "102",
"gas": "433404",
"gasPrice": "5000000000",
"gasUsed": "350916",
"cumulativeGasUsed": "17811460",
"input": "deprecated",
"confirmations": "1390"
},
{
"blockNumber": "14375679",
"timeStamp": "1642215658",
"hash": "0x433638955630652e6263f14dd3322668eafe447de5ad26664a307d8283c593da",
"nonce": "5",
"blockHash": "0x5ab95c904b703b5b197758772bcadefa7464f2ba4528c2e9615961199ac41517",
"from": "0x931b22a138893258c58f3e4143b17086a97862f6",
"contractAddress": "0xb46acb1f8d0ff6369c2f00146897aea1dfcf2414",
"to": "0x7345c01c595d69a02c95d65551499c67ae3edf4f",
"value": "620431183157188467115",
"tokenName": "Andromeda V2",
"tokenSymbol": "M31",
"tokenDecimal": "18",
"transactionIndex": "171",
"gas": "215186",
"gasPrice": "5000000000",
"gasUsed": "169374",
"cumulativeGasUsed": "34069029",
"input": "deprecated",
"confirmations": "1783"
},
{
"blockNumber": "14375679",
"timeStamp": "1642215658",
"hash": "0x433638955630652e6263f14dd3322668eafe447de5ad26664a307d8283c593da",
"nonce": "5",
"blockHash": "0x5ab95c904b703b5b197758772bcadefa7464f2ba4528c2e9615961199ac41517",
"from": "0x931b22a138893258c58f3e4143b17086a97862f6",
"contractAddress": "0xb46acb1f8d0ff6369c2f00146897aea1dfcf2414",
"to": "0xb46acb1f8d0ff6369c2f00146897aea1dfcf2414",
"value": "68936798128576496346",
"tokenName": "Andromeda V2",
"tokenSymbol": "M31",
"tokenDecimal": "18",
"transactionIndex": "171",
"gas": "215186",
"gasPrice": "5000000000",
"gasUsed": "169374",
"cumulativeGasUsed": "34069029",
"input": "deprecated",
"confirmations": "1783"
},
{
"blockNumber": "14375177",
"timeStamp": "1642214152",
"hash": "0x3d499ef6fe397cf121608aaff9a1c02e863fdacd41afea946f548f1f38c630df",
"nonce": "563",
"blockHash": "0xdceb19ad995f0cd0aae6e911744be3c6d3bf81b9e707e710bf0f06f6c1379030",
"from": "0x931b22a138893258c58f3e4143b17086a97862f6",
"contractAddress": "0xb46acb1f8d0ff6369c2f00146897aea1dfcf2414",
"to": "0x6d4d24e50c1bad7df10fafb979de12b6cc6cdac8",
"value": "1023172053263716043052",
"tokenName": "Andromeda V2",
"tokenSymbol": "M31",
"tokenDecimal": "18",
"transactionIndex": "93",
"gas": "215186",
"gasPrice": "5000000000",
"gasUsed": "169374",
"cumulativeGasUsed": "12183067",
"input": "deprecated",
"confirmations": "2285"
}
]
}
Two rows of this data has same hash id 0x433638955630652e6263f14dd3322668eafe447de5ad26664a307d8283c593da (you can search it yourself) I want to merge some variables in this 2 rows to become one.
Here is the logic:
If rows hash are same merge (sum) value data.
My current code is like:
if(myData[id].length > 0) {
for (let i in myData[id]) {
const number = myData[id][i].value;
// other values.....
console.log('values is: ', number);
// doing something with result
}
}
Sample code above return value of each row in API but as I mentioned I need to sum value of rows with same hash.
Crystal clear
Based on sample API above I have 5 rows, I need to get 3 rows. One of them which data are summed up from 2 rows with same hash.
Any idea?
CodePudding user response:
You can make use of Map and forEach here as:
const data = {
status: '1',
message: 'OK',
result: [
{
blockNumber: '14376792',
timeStamp: '1642218997',
hash: '0xe7a8a5793d1e85ec6f6de891f8be9bee2bf455b086236a98365be00febb8dec9',
nonce: '852',
blockHash:
'0x45b51bbac31bd1aef3215db88d05faa1172f9b9b7cb1f8f570b67e0bcef4e954',
from: '0x6d038b92c6aa2fc4db2ca80dc9dc5d63ddafe33d',
contractAddress: '0xb46acb1f8d0ff6369c2f00146897aea1dfcf2414',
to: '0x931b22a138893258c58f3e4143b17086a97862f6',
value: '72405000000000000000',
tokenName: 'Andromeda V2',
tokenSymbol: 'M31',
tokenDecimal: '18',
transactionIndex: '25',
gas: '266039',
gasPrice: '6000000000',
gasUsed: '201266',
cumulativeGasUsed: '2552933',
input: 'deprecated',
confirmations: '670',
},
{
blockNumber: '14376072',
timeStamp: '1642216837',
hash: '0x3ffe38506478a3a81ac39933d46521aa0624cd8b89bc36f79f20c2d0b81c5f92',
nonce: '9',
blockHash:
'0x672eeba6792d41c298057fefd3a31b1ab8acf8b877d8ef3f4c790234f3b4dd1b',
from: '0xb46acb1f8d0ff6369c2f00146897aea1dfcf2414',
contractAddress: '0xb46acb1f8d0ff6369c2f00146897aea1dfcf2414',
to: '0x931b22a138893258c58f3e4143b17086a97862f6',
value: '1917760707567583701190',
tokenName: 'Andromeda V2',
tokenSymbol: 'M31',
tokenDecimal: '18',
transactionIndex: '102',
gas: '433404',
gasPrice: '5000000000',
gasUsed: '350916',
cumulativeGasUsed: '17811460',
input: 'deprecated',
confirmations: '1390',
},
{
blockNumber: '14375679',
timeStamp: '1642215658',
hash: '0x433638955630652e6263f14dd3322668eafe447de5ad26664a307d8283c593da',
nonce: '5',
blockHash:
'0x5ab95c904b703b5b197758772bcadefa7464f2ba4528c2e9615961199ac41517',
from: '0x931b22a138893258c58f3e4143b17086a97862f6',
contractAddress: '0xb46acb1f8d0ff6369c2f00146897aea1dfcf2414',
to: '0x7345c01c595d69a02c95d65551499c67ae3edf4f',
value: '620431183157188467115',
tokenName: 'Andromeda V2',
tokenSymbol: 'M31',
tokenDecimal: '18',
transactionIndex: '171',
gas: '215186',
gasPrice: '5000000000',
gasUsed: '169374',
cumulativeGasUsed: '34069029',
input: 'deprecated',
confirmations: '1783',
},
{
blockNumber: '14375679',
timeStamp: '1642215658',
hash: '0x433638955630652e6263f14dd3322668eafe447de5ad26664a307d8283c593da',
nonce: '5',
blockHash:
'0x5ab95c904b703b5b197758772bcadefa7464f2ba4528c2e9615961199ac41517',
from: '0x931b22a138893258c58f3e4143b17086a97862f6',
contractAddress: '0xb46acb1f8d0ff6369c2f00146897aea1dfcf2414',
to: '0xb46acb1f8d0ff6369c2f00146897aea1dfcf2414',
value: '68936798128576496346',
tokenName: 'Andromeda V2',
tokenSymbol: 'M31',
tokenDecimal: '18',
transactionIndex: '171',
gas: '215186',
gasPrice: '5000000000',
gasUsed: '169374',
cumulativeGasUsed: '34069029',
input: 'deprecated',
confirmations: '1783',
},
{
blockNumber: '14375177',
timeStamp: '1642214152',
hash: '0x3d499ef6fe397cf121608aaff9a1c02e863fdacd41afea946f548f1f38c630df',
nonce: '563',
blockHash:
'0xdceb19ad995f0cd0aae6e911744be3c6d3bf81b9e707e710bf0f06f6c1379030',
from: '0x931b22a138893258c58f3e4143b17086a97862f6',
contractAddress: '0xb46acb1f8d0ff6369c2f00146897aea1dfcf2414',
to: '0x6d4d24e50c1bad7df10fafb979de12b6cc6cdac8',
value: '1023172053263716043052',
tokenName: 'Andromeda V2',
tokenSymbol: 'M31',
tokenDecimal: '18',
transactionIndex: '93',
gas: '215186',
gasPrice: '5000000000',
gasUsed: '169374',
cumulativeGasUsed: '12183067',
input: 'deprecated',
confirmations: '2285',
},
],
};
const map = new Map();
data.result.forEach((o) => {
map.has(o.hash)
? map.set(o.hash, { ...map.get(o.hash), ...o })
: map.set(o.hash, o);
});
data.result = [...map.values()];
console.log(data);
/* This is not a part of answer. It is just to give the output full height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }
CodePudding user response:
const data = {
status: "1",
message: "OK",
result: [
{
blockNumber: "14376792",
timeStamp: "1642218997",
hash:
"0xe7a8a5793d1e85ec6f6de891f8be9bee2bf455b086236a98365be00febb8dec9",
nonce: "852",
blockHash:
"0x45b51bbac31bd1aef3215db88d05faa1172f9b9b7cb1f8f570b67e0bcef4e954",
from: "0x6d038b92c6aa2fc4db2ca80dc9dc5d63ddafe33d",
contractAddress: "0xb46acb1f8d0ff6369c2f00146897aea1dfcf2414",
to: "0x931b22a138893258c58f3e4143b17086a97862f6",
value: "72405000000000000000",
tokenName: "Andromeda V2",
tokenSymbol: "M31",
tokenDecimal: "18",
transactionIndex: "25",
gas: "266039",
gasPrice: "6000000000",
gasUsed: "201266",
cumulativeGasUsed: "2552933",
input: "deprecated",
confirmations: "670"
},
{
blockNumber: "14376072",
timeStamp: "1642216837",
hash:
"0x3ffe38506478a3a81ac39933d46521aa0624cd8b89bc36f79f20c2d0b81c5f92",
nonce: "9",
blockHash:
"0x672eeba6792d41c298057fefd3a31b1ab8acf8b877d8ef3f4c790234f3b4dd1b",
from: "0xb46acb1f8d0ff6369c2f00146897aea1dfcf2414",
contractAddress: "0xb46acb1f8d0ff6369c2f00146897aea1dfcf2414",
to: "0x931b22a138893258c58f3e4143b17086a97862f6",
value: "1917760707567583701190",
tokenName: "Andromeda V2",
tokenSymbol: "M31",
tokenDecimal: "18",
transactionIndex: "102",
gas: "433404",
gasPrice: "5000000000",
gasUsed: "350916",
cumulativeGasUsed: "17811460",
input: "deprecated",
confirmations: "1390"
},
{
blockNumber: "14375679",
timeStamp: "1642215658",
hash:
"0x433638955630652e6263f14dd3322668eafe447de5ad26664a307d8283c593da",
nonce: "5",
blockHash:
"0x5ab95c904b703b5b197758772bcadefa7464f2ba4528c2e9615961199ac41517",
from: "0x931b22a138893258c58f3e4143b17086a97862f6",
contractAddress: "0xb46acb1f8d0ff6369c2f00146897aea1dfcf2414",
to: "0x7345c01c595d69a02c95d65551499c67ae3edf4f",
value: "620431183157188467115",
tokenName: "Andromeda V2",
tokenSymbol: "M31",
tokenDecimal: "18",
transactionIndex: "171",
gas: "215186",
gasPrice: "5000000000",
gasUsed: "169374",
cumulativeGasUsed: "34069029",
input: "deprecated",
confirmations: "1783"
},
{
blockNumber: "14375679",
timeStamp: "1642215658",
hash:
"0x433638955630652e6263f14dd3322668eafe447de5ad26664a307d8283c593da",
nonce: "5",
blockHash:
"0x5ab95c904b703b5b197758772bcadefa7464f2ba4528c2e9615961199ac41517",
from: "0x931b22a138893258c58f3e4143b17086a97862f6",
contractAddress: "0xb46acb1f8d0ff6369c2f00146897aea1dfcf2414",
to: "0xb46acb1f8d0ff6369c2f00146897aea1dfcf2414",
value: "68936798128576496346",
tokenName: "Andromeda V2",
tokenSymbol: "M31",
tokenDecimal: "18",
transactionIndex: "171",
gas: "215186",
gasPrice: "5000000000",
gasUsed: "169374",
cumulativeGasUsed: "34069029",
input: "deprecated",
confirmations: "1783"
},
{
blockNumber: "14375177",
timeStamp: "1642214152",
hash:
"0x3d499ef6fe397cf121608aaff9a1c02e863fdacd41afea946f548f1f38c630df",
nonce: "563",
blockHash:
"0xdceb19ad995f0cd0aae6e911744be3c6d3bf81b9e707e710bf0f06f6c1379030",
from: "0x931b22a138893258c58f3e4143b17086a97862f6",
contractAddress: "0xb46acb1f8d0ff6369c2f00146897aea1dfcf2414",
to: "0x6d4d24e50c1bad7df10fafb979de12b6cc6cdac8",
value: "1023172053263716043052",
tokenName: "Andromeda V2",
tokenSymbol: "M31",
tokenDecimal: "18",
transactionIndex: "93",
gas: "215186",
gasPrice: "5000000000",
gasUsed: "169374",
cumulativeGasUsed: "12183067",
input: "deprecated",
confirmations: "2285"
}
]
};
//hashes
const hashes = [];
data.result = [
...data.result.filter((item) => {
if (!hashes.includes(item.hash)) {
hashes.push(item.hash);
return item;
}
})
];
console.log(data.result);
@mafortis Please try this. I hope it will work.
Mutate the result array and add the object that has no duplicated hash id
