Reputation: 2703
This code works fine for one record, but I'm trying to make it work with an array of ~500 records...
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)
This outputs:
Object {aaa_id: 123, abbr_name: "j_doe", name: "john doe", bbb_id: 789}
Here's what my JSON looks like for the other records:
var aaaRecords [ {
"first_name": "Hasheem",
"last_name": "Thabeet",
"aaa_player_id": "4562"
},
...
]
var bbbRecords [{
"first_name": "Hasheem",
"last_name": "Thabeet",
"aaa_player_id": "4562"
},
....
]
Any ideas on how to make this function work with these arrays? Thanks a lot for any help!
Upvotes: 0
Views: 53
Reputation: 868
This has been tested:
<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>
The resulting output:
Upvotes: 1