【问题标题】:Array inside of an Object inside of an Array in jQueryjQuery中数组内部的对象内部的数组
【发布时间】:2018-04-25 16:46:36
【问题描述】:

我有以下来源:

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 }]

嗯.. 我希望它是我想要使用的那种存储。 一个玩家可以拥有几辆汽车(用它的 ID 标识)。 例如

Player1 有

  • car-A (ID 20) 11 次
  • 汽车-B (ID 23) 6 次
  • car-C (ID 2) 5 次

等等...我使用选择器直接从网页接收这些信息。

我要做的是将信息插入到上面的这个数组中,然后输出到某个地方。

我的尝试:

        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);
            }
        }
    });

现在发生的事情很奇怪。有时

  • 什么都没有发生或
  • 不是“玩家 1 有 22 辆车,玩家 2 有 24 辆车”,而是写成“玩家 1 有 22 辆车,玩家 1 有 24 辆车”或
  • “玩家 1 1 辆汽车,玩家 1 1 辆汽车,玩家 1 1 辆汽车,玩家 1 1 辆汽车,玩家 1 1 辆汽车,玩家 1 1 辆汽车,玩家 1 1 辆汽车”等等。 ..

我每时每刻都检查了每一个变量,一切似乎都很好。但不存储信息。现在我不知道我能做什么......

这是存储我的信息的正确方式吗?或者我应该使用几个数组并在需要时尝试将它们组合起来?

哦,cmets 展示了我的其他尝试。例如。通过 push 并尝试将同一类的汽车组合成一行(并增加计数)。

我期待与你们一起找到解决方案 :) 尽管情况很奇怪......询问您是否需要更多信息。

编辑:

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

【问题讨论】:

  • 如果你可以使用 Jquery,那么使用 $.each 方法解析成数组变得更加容易。只需通过绘制草图或在 mspaint 中编写来分享预期的输出,让我为您编写输出代码。 @格普
  • “我直接从 HTML 的代码中接收这些信息”,这是什么意思?我不确定我是否理解您的问题。您从哪里获取数据?
  • 我从网页接收数据,从代码中选择它。 '
    ID1
    ' 例如。对不起,我的语言很糟糕......
  • @GePu。给我一些示例输入
  • 我编辑了这篇文章。希望现在可以更好地理解:)

标签: jquery arrays object


【解决方案1】:

试试这个,

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);

您可以看到输出对象包含每个人拥有的所有汽车的数量。

因此,要查看特定人的所有可用汽车,请执行此操作,

output["person name"]

output.personName //(personName is a variable)

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-10
    • 1970-01-01
    • 2020-10-23
    • 2017-08-13
    • 1970-01-01
    相关资源
    最近更新 更多