GePu
GePu

Reputation: 320

Array inside of an Object inside of an Array in jQuery

I have the following source:

let source = [{ name: "Player1", cars:[{id: 20, count: 11}, {id: 23, count: 6}, {id: 2, count: 5}], cars_total: 22 }, { name: "Player2", cars:[{id: 11, count: 5}, {id: 24, count: 5}, {id: 42, count: 4}], cars_total: 14 }]

Well .. i expect it is that kind of storage i want to use. A player can have a several amount of cars (identified with its ID). e.g.

Player1 has

and so on ... I receive these information directly from a webpage using selectors.

What i want to do is to insert the information into this array above and then output it somewhere.

My try:

        intVehicleId = $('a').attr('vehicle_type_id');
        if(intVehicleId == "undefined") intVehicleId = null;           

        strProfileLinkText = $('a').children('a[href^="/profile/"]').text();

        if(!strProfileLinkText == 0) {
            intGetNamePos = -1;
            intGetCarPos = -1;

            for (var i = 0; i < arrNames.length; i++){
                if(arrNames[i].name == strProfileLinkText) {
                    intGetNamePos = i;
                    break;
                }
            }
            intGetNamePos = -1;
            if(intGetNamePos > -1) {
                //if(arrNames[intGetNamePos].cars.length == 0) intGetCarPos = -1;
                //else intGetCarPos = $.each(arrNames[intGetNamePos].cars, function(idx2, val2) { if(val2.id == intVehicleId) return idx2; });

                //arrNames[intGetNamePos].cars_total++;

                for (var j = 0; j < arrNames[intGetNamePos].cars.length; j++){
                    //if(arrNames[intGetNamePos].cars[j].id == intVehicleId) intGetCarPos = j; break;
                }

                if(intGetCarPos > -1) {
                    //arrNames[intGetNamePos].cars[intGetCarPos].count++; arrNames[intGetNamePos].cars_total++;
                } else {
                    //arrNames[intGetNamePos].cars.push({id:intVehicleId, count:1}); arrNames[intGetNamePos].cars_total++;
                }
            } else {
                //arrNames.push({name:strProfileLinkText, cars:[{id:intVehicleId, count:1}], cars_total:1});

                intCarsId.id = 0; //intVehicleId
                intCarsId.count = 1;
                arrCars.push(intCarsId);

                strTemp.name = strProfileLinkText;
                strTemp.cars = arrCars;
                strTemp.cars_total = 1;
                console.log(strProfileLinkText);
                arrNames.push(strTemp);
            }
        }
    });

Now what happens is pretty weird. Sometimes

I checked every single variable at every moment and everything seems fine. But not storing the information. Now i dont have any clue what i can do ...

Is that the right way to store my information? Or should i use several arrays and try to combine them when i need them?

Oh the comments show some other trys by me. E.g. via push and a try to combine cars of the same class to one row (and increment the count).

Im looking forward to find a solution with you guys :) despite this weird situation ... Ask if you need more information.

EDIT:

Input (later i replace the IDs with names):
ID 1 - Player 1
ID 1 - Player 2
ID 2 - Player 2
ID 3 - Player 3
ID 2 - Player 3
ID 1 - Player 2
ID 2 - Player 2

Expected output (of course in a table or whatever):
4x Player 2 | 3x ID 1, 1x ID 2
2x Player 3 | 1x ID 3, 1x ID 2
1x Player 1 | 1x ID 1

Upvotes: 0

Views: 308

Answers (1)

Satish Kumar
Satish Kumar

Reputation: 601

Try this,

const arrIDCars = ["Car A", "Car B", "Car C", "Car D", "Car E"];
const vehicleIdArr = [1, 1, 2, 3, 2, 1, 2, 3, 3, 2, 1, 2, 3, 2, 1, 1];
const strProfileLinkTextArr = ["p1", "p1", "p2", "p2", "p3", "p3", "p2", "p2", "p1", "p1", "p2", "p2", "p3", "p3", "p2", "p2"];

let output = {};
strProfileLinkTextArr.forEach((text, index) => {
    if (typeof output[text] == "undefined") {
        output[text] = {};
    }

    const vId = vehicleIdArr[index];

    if (typeof output[text][arrIDCars[vId - 1]] == "undefined") {
        output[text] = { ...output[text], [arrIDCars[vId - 1]]: 0 };
    }

    output[text][arrIDCars[vId - 1]]++;
});
console.log(output);

You can see the output object holds the count of all cars each person has.

So to see all available cars of a particular person do this,

output["person name"]

or

output.personName //(personName is a variable)

Hope this helps.

Upvotes: 1

Related Questions