Created
December 19, 2024 19:29
-
-
Save rozenmd/e3c969c98f1f8bfdaa0605fc7dda3bfe to your computer and use it in GitHub Desktop.
Cars to CSV
This file contains hidden or 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 data = window.__NEXT_DATA__.props.pageProps.searchData; | |
| const realData = data.ads.map((ad) => { | |
| const make = ad.attributes.find( | |
| (attribute) => attribute.key === "brand" | |
| ).value; | |
| const model = ad.attributes.find( | |
| (attribute) => attribute.key === "model" | |
| ).value; | |
| const year = ad.attributes.find( | |
| (attribute) => attribute.key === "regdate" | |
| ).value; | |
| const kms = ad.attributes.find( | |
| (attribute) => attribute.key === "mileage" | |
| ).value; | |
| return { | |
| URL: ad.url, | |
| Model: `${make} ${model}`, | |
| Price: ad.price[0], | |
| KMs: kms, | |
| Year: year, | |
| }; | |
| }); | |
| const header = Object.keys(realData[0]); | |
| const replacer = (key, value) => (value === null ? "" : value); | |
| const csv = [ | |
| header.join(","), // header row first | |
| ...realData.map((row) => | |
| header | |
| .map((fieldName) => JSON.stringify(row[fieldName], replacer)) | |
| .join(",") | |
| ), | |
| ].join("\r\n"); | |
| console.log(csv); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment