Created
August 26, 2024 14:27
-
-
Save dimmduh/84447383149d39c5a0170f74d7afa76a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const sales = 2871; | |
//result of https://api.dtf.ru/v2.31/comments?contentId=2922979&sorting=da | |
const comments = require('./input.json'); | |
console.log(comments.result.items.length); | |
let result = comments.result.items | |
.filter(function (item) { | |
return item.level === 0; | |
}) | |
.map(function (item) { | |
return { | |
id: item.id, | |
author: { | |
id: item.author.id, | |
name: item.author.name | |
}, | |
text: item.text, | |
parsedNumber: item.text.match(/\d+/g) | |
}; | |
}) | |
.filter(function (item) { | |
return item.parsedNumber !== null | |
}) | |
; | |
result = result.flatMap (function (item) { | |
return item.parsedNumber.map(function (parsedNumber) { | |
return { | |
id: item.id, | |
parsedNumber: parsedNumber, | |
author: item.author, | |
diff: Math.abs(parsedNumber - sales), | |
text: item.text, | |
}; | |
}) | |
}) | |
result.sort(function (a, b) { | |
return a.diff - b.diff; | |
}); | |
const tolerance = 100; | |
console.log(result.filter(function (item) { | |
return item.diff < tolerance; | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment