如果我想复制它的每一行并使它成为一个新的二维数组,我该如何处理二维数组呢?
这是二维数组的格式:
<!DOCTYPE html>
<head><meta charset="UTF-8">
<title>I am a test page</title>
<!--bootstrap 3.3.6 and jquery 2.23-->
<script src="./js/jquery-2.2.3.min.js"></script>
<script src="./js/bootstrap.min.js"></script>
<link href="./css/bootstrap.min.css" rel="stylesheet"/>
<style>
.main
{
background-color: rgba(98,159,210, 0.3);
border-radius: 1px;
font-size: 14px;
color: black;
width: 400px;
margin-left: auto;
margin-right: auto;
height: 50px;
padding:3px;
cursor: pointer;
}
.button1
{
}
.button2
{
}
.light1
{
height: 100px;
width: 100px;
background-color: yellow;
}
.light2
{
height: 100px;
width: 100px;
background-color: red;
}
</style>
</head>
<body>
<div class="container">
<br><br>
<div class="main">
<button class="button1">button1</button>
<button class="button2">button2</button>
</div>
<br>
<br><br><br><br>
<div class="light1"></div>
<div class="light2"></div>
</div>
</body>
<script>
$(document).on("click", ".button1, .main", function()
{
$(".light1").toggle();
});
$(document).on("click", ".button2", function()
{
$(".light2").toggle();
});
</script>
看起来应该是这样的:
{
"localTrain": "T7",
"TC": "2",
"TimeSheet": [
["01", "London", "BXP", "T7", "1632", "1640"],
["02", "Shanghai", "QWE", "T7", "1200", "1240"],
["03", "LosAngeles", "DFG", "T7", "1300", "1340"],
["04", "NewDelhi", "VGH", "T7", "1400", "1440"],
["05", "Sydney", "SAW", "T7", "1500", "1540"],
["06", "Tokyo", "SAT", "T7", "1600", "1640"],
["07", "Seoul", "BBT", "T7", "1700", "1740"],
["08", "CapeTown", "OOP", "T7", "1800", "1840"],
]
}
答案 0 :(得分:1)
只需创建一个与旧对象类似的新对象,除了遍历每个TimeSheet
数组元素,然后将其推送到新对象的TimeSheet
数组,而不是一次,而是两次。
const oldObj = {
"localTrain": "T7",
"TC": "2",
"TimeSheet": [
["01", "London", "BXP", "T7", "1632", "1640"],
["02", "Shanghai", "QWE", "T7", "1200", "1240"],
["03", "LosAngeles", "DFG", "T7", "1300", "1340"],
["04", "NewDelhi", "VGH", "T7", "1400", "1440"],
["05", "Sydney", "SAW", "T7", "1500", "1540"],
["06", "Tokyo", "SAT", "T7", "1600", "1640"],
["07", "Seoul", "BBT", "T7", "1700", "1740"],
["08", "CapeTown", "OOP", "T7", "1800", "1840"],
]
};
let newTimeSheet = [];
oldObj.TimeSheet.forEach(arrForOneCity => {
newTimeSheet.push(arrForOneCity);
newTimeSheet.push(arrForOneCity);
});
let newObj = {
localTrain: oldObj.localTrain,
TC: oldObj.TC,
TimeSheet: newTimeSheet
};
console.log(JSON.stringify(newObj));