数组ID作为未定义传递

时间:2017-08-19 10:47:46

标签: javascript jquery

我创建了这个JavaScript array,其中我创建了一个键值对,并希望分别传递键和值,如下所示:

我的阵列:

var str = {
    "attacker": $.ajax({
        dataType: "text",
        url: "http://localhost/liberate/threeee/PAGES/Information/content.html",
        success: function (data) {
            console.log(data);
            $(".movie").append(data)
        }
    }),
    "defender": $.ajax({
        dataType: "text",
        url: "http://localhost/liberate/threeee/PAGES/Information/content.html",
        success: function (data) {
            console.log(data);
            $("#attacker").append(data)
        }
    }),
    "midfielder": $.ajax({
        dataType: "text",
        url: "http://localhost/liberate/threeee/PAGES/Information/content.html",
        success: function (data) {
            console.log(data);
            $("#attacker").append(data)
        }
    }),
    "goalkeeper": $.ajax({
        dataType: "text",
        url: "http://localhost/liberate/threeee/PAGES/Information/content.html",
        success: function (data) {
            console.log(data);
            $("#attacker").append(data)
        }
    })
}

现在传递密钥和值:

for (var i = 0; i < totalSpheres; i++) {
    var xp = centerX + Math.sin(startRadians) * circleRadius;
    var zp = centerZ + Math.cos(startRadians) * circleRadius;
    group.add(new Element(Object.keys(str), str[i], new THREE.Vector3(xp, 0, zp), new THREE.Vector3(0, i * incrementAngle * (Math.PI / 180.0), 0)));
    startRadians += incrementRadians;
}
scene.add(group);

我使用了str.id来获取密钥,但它正在传递,因为未定义,有人可以告诉他如何传递密钥。

2 个答案:

答案 0 :(得分:0)

您需要将Object.keys用于JSON对象,例如

var str = {
    "attacker": $.ajax({
        dataType: "text",
        url: "http://localhost/liberate/threeee/PAGES/Information/content.html",
        success: function (data) {
            console.log(data);
            $(".movie").append(data)
        }
    }),
    "defender": $.ajax({
        dataType: "text",
        url: "http://localhost/liberate/threeee/PAGES/Information/content.html",
        success: function (data) {
            console.log(data);
            $("#attacker").append(data)
        }
    }),
    "midfielder": $.ajax({
        dataType: "text",
        url: "http://localhost/liberate/threeee/PAGES/Information/content.html",
        success: function (data) {
            console.log(data);
            $("#attacker").append(data)
        }
    }),
    "goalkeeper": $.ajax({
        dataType: "text",
        url: "http://localhost/liberate/threeee/PAGES/Information/content.html",
        success: function (data) {
            console.log(data);
            $("#attacker").append(data)
        }
    })
};

var allKeys = Object.keys(str);
console.log(allKeys[0])
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

答案 1 :(得分:0)

这是一个对象,而不是数组。

简单的for ... in会有所帮助:

for (key in str)
    group.add( new Element( key, str[key], new THREE.Vector3(xp, 0, zp), new THREE.Vector3(0, i*incrementAngle * (Math.PI/180.0), 0) ) );