我正在创建一个商店网站,但是我收到的数据馈送是一种用竖杠而不是逗号分隔的格式。我希望能够从文本文件中读取内容,以将内容显示到网页中,并允许客户搜索商品。
示例行:
710734240|8mm - Men's Freemason Ring / Masonic Ring - Gold and Black Inlay Tungsten Ring Comfort Fit|73628|
我尝试遍历分离出的项目并将它们放入自己的数组中。
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();
答案 0 :(得分:0)
也许您想从这里开始:
ob_end_clean();
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');
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;
}