gpt4 book ai didi

javascript - 如何使用一个函数用对象替换数组中的多个特定字符串?

转载 作者:行者123 更新时间:2023-12-02 22:21:51 26 4
gpt4 key购买 nike

What the code does (written in norwegian)

这就是表格的样子

当我按书(在格兰博行)时,我希望格兰博行(在“vinter”下)中的字段更改为“utleid”(在挪威语中表示“不可用”)。我可以像这样对每一行进行硬编码:

       function book(){
main.innerHTML = " "

for(i = 0; i < hytte.length; i++){
hytte[1].vinterferie = "utleid";
main.innerHTML += `
<tr style="background-color: grey;">
<td>${hytte[i].navn}</td>
<td>${hytte[i].jul}</td>
<td>${hytte[i].vinterferie}</td>
<td>${hytte[i].paske}</td>
</tr>
`
}

但我只想拥有一个适用于所有领域的函数。

这是数组中 1 个对象的示例:

      let hytte = [
{
navn: "Granstua",
jul: "utleid",
vinterferie: "utleid",
paske: "ledig",
plasser: 4,
stand: "hoy",
bad: "ja",
book: `<button onclick="book()">Book</button>`
},

我知道这可能很难理解。

最佳答案

已编辑:

这是我之前的意思的高级概念演示。它显示“Book”和“UnBook”功能。我要求您掌握这个概念并根据您的要求进行修改。

注意:此示例仅显示一个类别(VinterfeireTider)。剩下的你必须决定如何进一步推进。并且不要忘记根据您的要求修改它。

<script>

function getAllData(){
let hytte = [
{
sno:1, // introduce a sequence number for all objects in array
navn: "Granstua",
jul: "utleid",
vinterferie: "utleid",
paske: "ledig",
plasser: 4,
stand: "hoy",
bad: "ja"
},{
sno:2, // introduce a sequence number for all objects in array
navn: "Granbo",
jul: "utleid",
vinterferie: "ledig",
paske: "ledig",
plasser: 4,
stand: "hoy",
bad: "ja"
},{
sno:3, // introduce a sequence number for all objects in array
navn: "Grantoppen",
jul: "utleid",
vinterferie: "ledig",
paske: "ledig",
plasser: 4,
stand: "hoy",
bad: "ja"
},{
sno:4, // introduce a sequence number for all objects in array
navn: "Granhaug",
jul: "utleid",
vinterferie: "ledig",
paske: "ledig",
plasser: 4,
stand: "hoy",
bad: "ja"
}];
return hytte;
}

window.onload = function(){
loadAllData();
}

function loadAllData(){
let data = getAllData();
let tBody1 = document.getElementById("tbody1");
let tBody2 = document.getElementById("tbody2");
let tBody1Data = "";
let tBody2Data = "";
let columnToUpdate = 2; // you need to decide this dynamically based on how you have built your HTML file.
for(let i=0;i<data.length;i++){
tBody1Data += tr(td(data[i].navn) + td(data[i].jul) + td(data[i].vinterferie) + td(data[i].paske))
let button = "";
if(data[i].vinterferie.toLowerCase() === "ledig"){
button = '<button onclick="book(this,' + i + ',' + columnToUpdate + ')">Book</button>';
}
else if(data[i].vinterferie.toLowerCase() === "utleid"){
button = '<button onclick="unBook(this,' + i + ',' + columnToUpdate + ')">UnBook</button>';
}
tBody2Data += tr(td(data[i].navn) + td(data[i].plasser) + td(data[i].stand) + td(data[i].bad) + td(button))
}
tBody1.innerHTML = tBody1Data;
tBody2.innerHTML = tBody2Data;
}

function tr(value){
return '<tr>' + value + '</tr>'
}

function td(value){
return '<td>' + value + '</td>'
}

function book(btn, sno, col){
updateStatus(sno, col, "utleid")
changeButtonBehaviour(btn, function(){ unBook(btn, sno, col) }, "UnBook");

}

function unBook(btn, sno, col){
updateStatus(sno, col, "ledig")
changeButtonBehaviour(btn, function(){ book(btn, sno, col) }, "Book");
}

function changeButtonBehaviour(btn, click, text){
btn.onclick = click;
btn.textContent = text;
}

function updateStatus(sno, col, status){
var tbody1 = document.getElementById("tbody1");
let rows = tbody1.children

if(rows.length >= sno){
let rowToUpate = rows[sno];
rowToUpate.children[col].textContent = status;
}
}
</script>
<body>
<h1>sample</h1>
<table border="1">
<thead>
<th>Hytte</th><th>Jul</th><th>Vinter</th><th>Paske</th>
</thead>

<tbody id="tbody1">

</tbody>
</table>
<h5>VinterfeireTider</h5>
<table>
<tbody id="tbody2">

</tbody>
</table>

</body>

关于javascript - 如何使用一个函数用对象替换数组中的多个特定字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59202704/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com