0

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!

3
  • are the arrays the same length? are there only two arrays ever? Commented Oct 24, 2014 at 23:06
  • No the arrays are different lengths but there will only be two arrays Commented Oct 24, 2014 at 23:17
  • if the arrays aren't parallel, it's not clear how a merge should be performed. i would play around with underscore examples and see if i couldn't find something close that can be modified. Commented Oct 24, 2014 at 23:19

1 Answer 1

1

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:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.