-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
28 lines (24 loc) · 1.05 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/* Data Processor Actor - responsible for filtering the data from the Amazon Actor */
const Apify = require('apify');
Apify.main(async () => {
const input = await Apify.getInput();
const response = await Apify.client.datasets.getItems({ datasetId: input.resource.defaultDatasetId })
const data = response.items
console.log(`Start to process - ${data.length} products`)
const bestOfferMap = new Map()
for (const item of data) {
const currentPrice = Number(item.price.replace(/\D/g,''))
const lastItem = bestOfferMap.get(item.asin)
if (!lastItem) {
bestOfferMap.set(item.asin, item)
} else {
const lastLowestPrice = Number(lastItem.price.replace(/\D/g,''))
if (currentPrice < lastLowestPrice) {
bestOfferMap.set(item.asin, item)
}
}
}
const processedData = Array.from(bestOfferMap, ([_, value]) => ( value ));
await Apify.pushData(processedData)
console.log(`Done process to process. Filtered products - ${processedData.length}`)
});