0

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

  • car-A (ID 20) 11 times
  • car-B (ID 23) 6 times
  • car-C (ID 2) 5 times

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

  • nothing happens or
  • instead of "22 cars by Player 1, 24 cars by Player 2" it writes "22 cars by Player 1, 24 cars by Player 1" or
  • "1 cars by Player 1, 1 cars by Player 1, 1 cars by Player 1, 1 cars by Player 1, 1 cars by Player 1, 1 cars by Player 1, 1 cars by Player 1" and so on ...

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
10
  • if you can Jquery then it becomes even more easier to parse into arrays by using $.each method. Just share the expected output by drawing sketch or writing in mspaint and let me write output code for you . @Gepu Commented Apr 25, 2018 at 16:53
  • "I receive these information directly from the code of the HTML", What does this mean? I am not sure if I understood your problem. Where are you getting the data from? Commented Apr 25, 2018 at 17:31
  • I receive the data from a webpage, selecting it from the code. '<div>ID1</div>' for example. Sorry quite bad language by me ... Commented Apr 25, 2018 at 17:34
  • @GePu. show me some sample input Commented Apr 25, 2018 at 17:36
  • I edited the post. Hopefully now for a better understanding :) Commented Apr 25, 2018 at 17:37

1 Answer 1

1

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.

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.