数组 ID 作为未定义传递
Array ID is passed as undefined
我制作了这个 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 获取密钥,但它以未定义的形式传递,谁能告诉我如何只传递他的密钥。
您需要对 JSON 个对象使用 Object.keys
,例如
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>
那是一个对象,不是数组。
一个简单的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) ) );
我制作了这个 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 获取密钥,但它以未定义的形式传递,谁能告诉我如何只传递他的密钥。
您需要对 JSON 个对象使用 Object.keys
,例如
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>
那是一个对象,不是数组。
一个简单的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) ) );