0

I am creating a store website, but the datafeed I receive is a format separated by bars instead of commas. I want to be able to read from the text file to display the content into a web page and allow the client to search through the merchandise.

Example row:

710734240|8mm - Men's Freemason Ring / Masonic Ring - Gold and Black Inlay Tungsten Ring Comfort Fit|73628|

I tried the looping over the separated items and putting each of them into their own array.

        const my_data = 'test_data.txt';

        async function getData() {

            const response = await fetch(my_data);
            const data = await response.text();
            //console.log(data);

            const rows = data.split('\n').splice(1);
            rows.forEach(elt => {
                const row = elt.split('|');

                row.forEach(item => {
                    const items = item.split(',');
                    //    const filtered = items.filter(function (el){
                    //    return el != ""; 
                    const lens = items.length;
                    //document.getElementById('data').innerHTML = rows + "<br>";

                    var goods = {
                        ProductId: row[0],
                        Name: row[1],
                        MechantId: row[2],
                        Mechant: row[3],
                        link: row[4],
                        thumbnail: row[5],
                        bigImage: row[6],
                        Price: row[7],
                        RetailPrice: row[8],
                        mainCat: row[9],
                        subCat: row[10],
                        Description: row[11],

                    };
                    document.getElementById('data').innerHTML = goods.Description + "<br>";
                });


                console.log(row);
                //console.log(lens);



            });
        }
        getData();
mplungjan
  • 169,008
  • 28
  • 173
  • 236

1 Answers1

0

Perhaps you want to start with this:

let list = {};

const rows = `710734240|8mm - Men's Freemason Ring / Masonic Ring - Gold and Black Inlay Tungsten Ring Comfort Fit|73628|
710734241|9mm - Men's Freemason Ring / Masonic Ring - Gold and Black Inlay Tungsten Ring Comfort Fit|73628|
710734242|10mm - Men's Freemason Ring / Masonic Ring - Gold and Black Inlay Tungsten Ring Comfort Fit|73628|`
  .split("\n")
  .forEach(row => { // destructing the rows curtesy of Code Maniac
    ( 
      ([ProductId, Description, subCat]) => list[ProductId] = { Description, subCat }  
    )( row.split("|") )
  })


// displaying example

let dl = document.createElement("dl");
let content = []
Object.keys(list).forEach(
  k => content.push(`<dt>${k}</dt><dd>${list[k].Description}</dd>`)
)
dl.innerHTML = content.join("");
document.getElementById("container").appendChild(dl)
dl {
  display: flex;
  flex-flow: row wrap;
  border: solid #333;
  border-width: 1px 1px 0 0;
}

dt {
  flex-basis: 10%;
  padding: 2px 4px;
  background: #333;
  text-align: right;
  color: #fff;
}

dd {
  flex-basis: 80%;
  flex-grow: 1;
  margin: 0;
  padding: 2px 4px;
  border-bottom: 1px solid #333;
}
<div id="container"></div>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • This looks promising I wish there was a way to send you the file and you can see fro yourself but this is in the ball park. – Coding Junior Sep 03 '19 at 06:39