gpt4 book ai didi

javascript - 在 Javascript 中按他的名字移动对象的键

转载 作者:行者123 更新时间:2023-11-29 18:57:02 25 4
gpt4 key购买 nike

我实际上遇到了一个问题,已经有 3 天了,我正在搜索如何通过他的名字移动对象的键,如果没有给出值,则获取对象的第一个值。

但是,问题是我找不到任何答案,你能帮忙吗?

而且,我不想使用 jQuery 或任何库,都在纯原生 JS 中
这是 JS 代码:

window.onload = function(){
var uberTable = document.querySelector('.divTable');
for(var i in users){
showDatas(uberTable, users, i)+'<br>';
}
var keys = document.querySelector('.headRow');
showColumns(keys, users, i);
replaceFirstColumn(users, 3, 0);
};

function showDatas(id, table, iterator){
id.innerHTML += '<div class="divRow">'+
'<div class="divCell">'+
table[iterator].Name + '&nbsp</div><div class="divCell">' +
table[iterator].Email + '&nbsp</div><div class="divCell">' +
table[iterator].Gender + '&nbsp</div><div class="divCell">' +
table[iterator].Age + '&nbsp</div><div class="divCell">' +
table[iterator].Company +'</div>'
'</div>';
}

function showColumns(id, table, iterator){
var obj = Object.keys(table[iterator])
for(var k in obj){
id.innerHTML += '<div class="divCell">'+obj[k]+'</div>';
}
}

数据:

var users = [
{
"Name": "Random name",
"Email": "user@email.com",
"Gender": "Male",
"Age": 33,
"Company": "SUPCompany",
},
{
"Name": "username",
"Email": "User@email.ca",
"Gender": "Male",
"Age": 44,
"Company": "GolemAi",
},
{
"Name": "Different name",
"Email": "random@user.ca",
"Gender": "Male",
"Age": 23,
"Company": "SUPCompany",
},
{
"Name": "Nicolas Sarkozy",
"Email": "niconico@unemployed.prison",
"Gender": "Male",
"Age": 50,
"Company": "Unemployed",
},
{
"Name": "No Idea",
"Email": "No@Id.ea",
"Gender": "Female",
"Age": 20,
"Company": "The Random Company",
},
{
"Name": "Brigitte Macron",
"Email": "Brigitte@elysee.dz",
"Gender": "Female",
"Age": 180,
"Company": "Mummy of Manu'",
},
{
"Name": "Corentin Crésus",
"Email": "tintin@cre.sus",
"Gender": "Polygender",
"Age": 500,
"Company": "Crésus LGBT Corp",
},
];

这是表格的 CSS 代码:

@import url('https://fonts.googleapis.com/css?family=Marcellus');
@import url('https://fonts.googleapis.com/css?family=Roboto');
body{font-family: 'Roboto', 'Marcellus'}

.divTable
{
display: table;
width: auto;
background-color: #eee;
border: 1px solid #000;
border-spacing: 5px;
border-collapse: separate;
}

.headRow{font-weight: bold}
.headRow > .divCell{background: cyan}

.divRow > .divCell:nth-child(1){
background: cyan;
font-style: italic;
}

.divRow
{
display: table-row;
width: auto;
}

.divCell
{
float: left;
display: table-column;
text-align: center;
width: 300px;
background-color: #ccc;
border-style: ridge;
border-width: 1px;
}

HTML 代码:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="styles.css">
<title>Uber Table</title>
</head>
<body>
<div class="divTable">
<div class="headRow"></div>
</div>
<script src="app.js"></script>
<script src="db.js"></script>
</body>
</html>

最佳答案

使用这个函数:

function replaceFirstColumn($firstColIndex = 3, $replacedColIndex = 0){
var tableRows = Array.from(document.querySelectorAll('.divRow, .headRow'));

tableRows.forEach(function(element) {
var rowCells = Array.from(element.getElementsByClassName('divCell'));

var aux = rowCells[$firstColIndex].innerHTML;

rowCells[$firstColIndex].innerHTML = rowCells[$replacedColIndex].innerHTML;
rowCells[$replacedColIndex].innerHTML = aux;
});
}

关于javascript - 在 Javascript 中按他的名字移动对象的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48909584/

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