我正在尝试向对象数组中添加两个属性,因此我决定从原始对象数组中创建一个新对象数组,然后设置新属性。 (我意识到这可能是更简单的方法)Javascript - 对象数组中的未定义属性
我的问题是,当我尝试访问新数组内的属性时,它是未定义的。什么是错误可能是明显的,但对我来说不是。帮帮我!
var join = [];
for (linksIndex = 0; linksIndex < links.length; ++linksIndex) {
join.push([{source:links[linksIndex].source,target:links[linksIndex].target, x1:0, y1:0, x2:0, y2:0}]);
};
for (joinIndex = 0; joinIndex < join.length; ++joinIndex) {
// console.log("join in loop");console.log(join); // ok array of objects
// console.log("join[joinIndex]");console.log(join[joinIndex]); // on object
console.log("join[joinIndex].source");console.log(join[joinIndex].source); // undefined why?
for (nodesIndex = 0; nodesIndex < nodes.length; ++nodesIndex) {
if (nodes[nodesIndex].name == join[joinIndex].source) {
join[joinIndex].x1=nodes[nodesIndex].x; // match source
join[joinIndex].y1=nodes[nodesIndex].y; // match source
};
if (nodes[nodesIndex].name == join[joinIndex].target) {
join[joinIndex].x2=nodes[nodesIndex].x; // match target
join[joinIndex].y2=nodes[nodesIndex].y; // match target
} ;
}
}