如何在 Javascript 中向对象添加键和值 [关闭]

问题描述 投票:0回答:0

İ 我正在尝试从 roadNetwork 对象创建一个图形,RoadNetwork 只是一个具有车道位置及其周围的对象,我需要帮助的代码位于底部(第二部分),我已经创建了节点(键) 和 edges(values) 但我不知道如何连接它们并输出图形。请帮助。

const roadNetwork = {
  23250: [
    {
      //laneZero at (167,23250)
      167: {
        location: [{ x: laneZero, y: 23250 }],
        forward: [{ x: laneZero, y: 22150 }],
        left: null,

        right: [{ x: laneOne, y: 23250 }],
      },

      //laneOne (300,23250)
      300: {
        location: [{ x: laneOne, y: 23250 }],
        forward: [{ x: laneOne, y: 22150 }],
        left: [{ x: laneZero, y: 23250 }],
        right: [{ x: laneTwo, y: 23250 }],
      },

      //laneTwo (433,23250 )
      433: {
        location: [{ x: laneTwo, y: 23250 }],
        forward: [{ x: laneTwo, y: 22150 }],
        left: [{ x: laneOne, y: 23300 }],
        right: null,
      },

      //  Entry at (800,23300)
      800: {
        location: [{ x: 800, y: 23300 }],
        forward: null,
        left: [{ x: laneTwo, y: 23250 }],
        right: null,
      },
    },
  ],

  22150: [
    {
      //laneZero at (167,22150)
      167: {
        location: [{ x: laneZero, y: 22150 }],
        forward: [{ x: laneZero, y: 21050 }],
        left: null,
        right: [{ x: laneOne, y: 22150 }],
      },
      //laneOne (300,22150)
      300: {
        location: [{ x: laneOne, y: 22150 }],
        forward: [{ x: laneOne, y: 21050 }],
        left: [{ x: laneZero, y: 22150 }],
        right: null,
      },
      //laneTwo (433,22150)
      433: {
        location: [{ x: laneOne, y: 22150 }],
        forward: [{ x: laneOne, y: 21050 }],
        left: [{ x: laneZero, y: 22150 }],
        right: [{ x: 800, y: 22100 }],
      },
      //  Exit at (800,22100)
      800: {
        location: [{ x: 22150, y: 22100 }],
        forward: null,
        left: null,
        right: null,
      },
    },
  ],
  21050: [
    {
      // laneZero at (167,21050)
      167: {
        location: [{ x: laneZero, y: 21050 }],
        forward: [{ x: laneZero, y: 19950 }],
        left: null,
        right: [{ x: laneOne, y: 21050 }],
      },
      // laneOne at (300,21050)
      300: {
        location: [{ x: laneOne, y: 21050 }],
        forward: [{ x: laneOne, y: 19950 }],
        left: [{ x: laneZero, y: 21050 }],
        right: [{ x: laneTwo, y: 21050 }],
      },
      // laneTwo at (433,21100)
      433: {
        location: [{ x: laneTwo, y: 21050 }],
        forward: [{ x: laneTwo, y: 19950 }],
        left: [{ x: laneOne, y: 21050 }],
        right: null,
      },
      // entry at (800,21100)
      800: {
        location: [{ x: 800, y: 21100 }],
        forward: null,
        left: [{ x: laneTwo, y: 21050 }],
        right: null,
      },
    },
  ],
  19950: [
    {
      // laneZero at (167,19950)
      167: {
        location: [{ x: laneZero, y: 19950 }],
        forward: [{ x: laneZero, y: 18850 }],
        left: null,
        right: [{ x: laneOne, y: 19950 }],
      },

      // laneOne at (300,19950)
      300: {
        location: [{ x: laneOne, y: 19950 }],
        forward: [{ x: laneOne, y: 18850 }],
        left: [{ x: laneZero, y: 19950 }],
        right: null,
      },

      // laneTwo at (433,19950)
      433: {
        location: [{ x: laneTwo, y: 19950 }],
        forward: [{ x: laneTwo, y: 18850 }],
        left: [{ x: laneOne, y: 19950 }],
        right: [{ x: 800, y: 19900 }],
      },

      // exit at (800,19900)
      800: {
        location: [{ x: 800, y: 19900 }],
        forward: null,
        left: null,
        right: null,
      },
    },
  ],

  18850: [
    {
      // laneZero at (167,18850)
      167: {
        location: [{ x: laneZero, y: 18850 }],
        forward: [{ x: laneZero, y: 17750 }],
        left: null,
        right: [{ x: laneOne, y: 18850 }],
      },

      // laneOne at (300,18850)
      300: {
        location: [{ x: laneOne, y: 18850 }],
        forward: [{ x: laneOne, y: 17750 }],
        left: [{ x: laneZero, y: 18850 }],
        right: [{ x: laneTwo, y: 18850 }],
      },

      // laneTwo at (433,18850)
      433: {
        location: [{ x: laneTwo, y: 18850 }],
        forward: [{ x: laneTwo, y: 17750 }],
        left: [{ x: laneOne, y: 18850 }],
        right: null,
      },

      // entry at (800,18900)
      800: {
        location: [{ x: 800, y: 18850 }],
        forward: null,
        left: [{ x: laneTwo, y: 18900 }],
        right: null,
      },
    },
  ],
  17750: [
    {
      // laneZero at (167,17750)
      167: {
        location: [{ x: laneZero, y: 17750 }],
        forward: [{ x: laneZero, y: 16650 }],
        left: null,
        right: [{ x: laneOne, y: 17750 }],
      },

      // laneOne at (300,17750)
      300: {
        location: [{ x: laneOne, y: 17750 }],
        forward: [{ x: laneOne, y: 16650 }],
        left: [{ x: laneZero, y: 17750 }],
        right: null,
      },

      // laneTwo at (433,17750)
      433: {
        location: [{ x: laneTwo, y: 17750 }],
        forward: [{ x: laneTwo, y: 16650 }],
        left: [{ x: laneOne, y: 17750 }],
        right: [{ x: 800, y: 17700 }],
      },

      // exit at (800,17700)
      800: {
        location: [{ x: 800, y: 17700 }],
        forward: null,
        left: null,
        right: null,
      },
    },
  ],
};
const graphMaker = (roadNetwork) => {
  const graph = {}; //graph is an object

  let currentLocation;
  let node;
  let lanesArray = [167, 300, 433, 800];

  for (let i = 23250; i >= -19650; i -= 1100) {
    let temp = i;
    i < 0 ? (i = i * -1000) : (i = i);

    //looping through the lanes
    for (let j = 0; j < lanesArray.length; j++) {
      currentLocation = lanesArray.pop();
      lanesArray.unshift(currentLocation);
      let edges = [];

      node = roadNetwork[i][0][currentLocation]["location"]; //node is the location of all lanes
      

      //if forward is not null then link it to the next location
      if (roadNetwork[i][0][currentLocation]["forward"] !== null) {
        edges.push(roadNetwork[i + 1100][0][currentLocation]["location"]);
      }
      //if left is not null then link it to the next lane on its left
      if (roadNetwork[i][0][currentLocation]["left"] !== null) {
        //check if the current lane is the entry lane
        let subtrator = 133;
        currentLocation === 800 ? (subtrator = 367) : (subtrator = 133);

        //add the location of the next lane to the edges array
        edges.push(roadNetwork[i][0][currentLocation - subtrator]["location"]);
      }
      //if right is not null then link it to the next lane on its right
      if (roadNetwork[i][0][currentLocation]["right"] !== null) {
        //check if the current lane is the exit lane
        let adder = 133;
        currentLocation === 800 ? (adder = 367) : (adder = 133);

        //add the location of the next lane to the edges array
        edges.push(roadNetwork[i][0][currentLocation + adder]["location"]);
      }
    }
    i = temp;
  }
  return graph;
}; 
javascript data-structures html5-canvas game-development directed-graph
© www.soinside.com 2019 - 2024. All rights reserved.