此代码适用于一条记录,但我正在尝试使用大约500条记录的数组...
var aaa = {
aaa_id: 123,
abbr_name: "j_doe",
name: "john doe"
}
var bbb = {
bbb_id: 789,
}
for (var attrname in bbb) {
aaa[attrname] = bbb[attrname];
}
console.log(aaa.aaa_id)
输出:
Object {aaa_id: 123, abbr_name: "j_doe", name: "john doe", bbb_id: 789}
以下是我的JSON对其他记录的看法:
var aaaRecords [ {
"first_name": "Hasheem",
"last_name": "Thabeet",
"aaa_player_id": "4562"
},
...
]
var bbbRecords [{
"first_name": "Hasheem",
"last_name": "Thabeet",
"aaa_player_id": "4562"
},
....
]
关于如何使这个功能适用于这些数组的任何想法?非常感谢您的帮助!
答案 0 :(得分:1)
已经过测试:
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script language="javascript">
var aaaRecords = [{
"first_name": "Hasheem",
"last_name": "Thabeet",
"aaa_player_id": "456"
},{
"first_name": "Mariah",
"last_name": "Carey",
"aaa_player_id": "721"
}];
var bbbRecords = [{
"first_name": "Hasheem",
"last_name": "Thabeet",
"bbb_player_id": "489"
},{
"first_name": "Mariah",
"last_name": "Carey",
"bbb_player_id": "198"
}];
$(document).ready(function() {
mergeRecords(function() {
displayRecords();
});
});
function mergeRecords(callback) {
for (var i = 0; i < aaaRecords.length; i++) {
aFName = aaaRecords[i].first_name;
aLName = aaaRecords[i].last_name;
for (var j = 0; j < bbbRecords.length; j++) {
bFName = bbbRecords[j].first_name;
bLName = bbbRecords[j].last_name;
bPlayerID = bbbRecords[j].bbb_player_id;
if (aFName == bFName && aLName == bLName) {
aaaRecords[i].bbb_player_id = bPlayerID;
}
}
} callback();
}
function displayRecords() {
for (var i = 0; i < aaaRecords.length; i++) {
$('#results').append('<tr><td>' +
aaaRecords[i].first_name + '</td><td>' +
aaaRecords[i].last_name + '</td><td>' +
aaaRecords[i].aaa_player_id + '</td><td>' +
aaaRecords[i].bbb_player_id + '</td></tr>');
}
}
</script>
</head>
<body>
<table id="results" border="1" cellpadding="2" cellspacing="2" width="400">
<tr>
<th>First Name:</th>
<th>Last Name:</th>
<th>Player ID (a):</th>
<th>Player ID (b):</th>
</tr>
<!-- Display results here -->
</table>
</body>
</html>
结果输出: