1

http://jsfiddle.net/rw0z9e2j/

var sports = [{
    "id": 1,
        "name": "baseball"
}, {
    "id": 2,
        "name": "Football"
}];

var playersData = [{
    "sport_id": 2,
        "id": "nv12",
        "name": "James"
}, {
    "sport_id": 2,
        "id": "nv11",
        "name": "Jean"
}];

var arr = [],
    tempObj = {};

$.each(sports, function (i, obj) {
    var sport_id = obj.id;

    $.each(playersData, function (i, obj) {
        if (sport_id == obj.sport_id) {
            tempObj = {
                "sport_id": obj.sport_id,
                    "id": obj.id,
                    "name": obj.name
            };
            arr.push(tempObj);
        }

    });
    obj.players = arr;
});

console.log(sports);

I try to build an array of players and put them within sports group according to sport_id but above logic has failed. It didn't group properly, the player who's in sport_id = 1 should go to sport which its id = 1 but why it didn't?

what's wrong with above loop there?

3
  • please post a wanted result Commented Aug 19, 2015 at 9:28
  • @NinaScholz the player who's in sport_id = 1 should go to sport which its id = 1. Commented Aug 19, 2015 at 9:29
  • 1
    you habe obj twice, as well as i. Commented Aug 19, 2015 at 9:39

4 Answers 4

2

I suppose this is what you want:

var sports = [{
    "id": 1,
    "name": "baseball"
}, {
    "id": 2,
    "name": "Football"
}];

var playersData = [{
    "sport_id": 2,
    "id": "nv12",
    "name": "James"
}, {
    "sport_id": 2,
    "id": "nv11",
    "name": "Jean"
}];

sports.forEach(function (a) {
    var arr = [];
    playersData.forEach(function (b) {
        if (a.id == b.sport_id) {
            arr.push(b);
        }
    });
    a.players = arr;
});

document.write('<pre>' + JSON.stringify(sports, 0, 4) + '</pre>');

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

2 Comments

Where is my problem mate?
can u take a look at Jai's answer below, he need not to clear the arr.
1

You're declaring your temp vars outside of your loops, these should be scoped to your loops and thrown away after each operation.

var arr = [],
    tempObj = {};

http://jsfiddle.net/samternent/rw0z9e2j/2/

Comments

0

You have to put it after push:

arr.push(tempObj);
obj.players = arr;

Actually you need this:

var sports = [{
    "id": 1,
        "name": "baseball"
}, {
    "id": 2,
        "name": "Football"
}];

var playersData = [{
    "sport_id": 2,
        "id": "nv12",
        "name": "James"
}, {
    "sport_id": 2,
        "id": "nv11",
        "name": "Jean"
}];

var arr = [];

$.each(sports, function (i, obj) {
    $.each(playersData, function (i, player) {
        if (obj.id === player.sport_id) {
           var tempObj = {
                "sport_id": player.sport_id,
                    "id": player.id,
                    "name": player.name
            };
            arr.push(tempObj);
            obj.players = arr;
        }

    });
    
});

console.log(sports);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

6 Comments

this doesn't work, obj.players will refer to the playersData as it's in the $.each loop.
i have changed the arguments name for readability you can see first one in the each is obj and in second each is player instead of obj. so obj.players actually referring to the first one.
can u help to take a look here? jsfiddle.net/2sn9vk2a it doesn't work, I tried to follow ur logic.
you can check your data if they have proper keys in it and i guess you need to ask to the person whose answer you have accepted.
both of your code worked, but I don't know what's the different, and which one has better performaces.
|
0

Hope you want to put inside the Sports group, but you are adding inside the player array, please notice, so please call

obj.sports = arr;

Hope it solve your problem .

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.