Skip to content

Instantly share code, notes, and snippets.

@rozenmd
Created December 19, 2024 19:29
Show Gist options
  • Select an option

  • Save rozenmd/e3c969c98f1f8bfdaa0605fc7dda3bfe to your computer and use it in GitHub Desktop.

Select an option

Save rozenmd/e3c969c98f1f8bfdaa0605fc7dda3bfe to your computer and use it in GitHub Desktop.
Cars to CSV
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