search

Home  >  Q&A  >  body text

javascript - How does Vue traverse data from two JSONs and assign values ​​to the DOM based on the same Key?

Existing data 1 shipTypes

{
    "Destroyer": "驅逐艦",
    "AirCarrier": "航空母艦",
    "Battleship": "主力艦",
    "Cruiser": "巡洋艦"
}

and data 2 shipTypeImages

{
    "Destroyer": {
        "image": "http://glossary-asia-static.gcdn.co/icons/wows/current/vehicle/types/Destroyer/normal.png",
    },
    "AirCarrier": {
        "image": "http://glossary-asia-static.gcdn.co/icons/wows/current/vehicle/types/AirCarrier/normal.png",
    },
    "Battleship": {
        "image": "http://glossary-asia-static.gcdn.co/icons/wows/current/vehicle/types/Battleship/normal.png",
    },
    "Cruiser": {
        "image": "http://glossary-asia-static.gcdn.co/icons/wows/current/vehicle/types/Cruiser/normal.png",
    }
}

Vue DOM structure (pug syntax)

ul
    li
        img {{shipTypeImages.image}}
        span {{shipTypes.value}}

How do I match the Key values ​​of two data (such as "Destroyer") with the data, as shown in the following example?

Thanks!

黄舟黄舟2838 days ago645

reply all(2)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-05-18 11:04:34

    Two arrays foreach loop to form a new array to render to the page

    Or use computed properties

    reply
    0
  • 巴扎黑

    巴扎黑2017-05-18 11:04:34

    let ships = Object.keys(shipTypes).map((type) => {
        return {
            type,
            name: shipTypes[type],
            image: shipTypeImages[type].image
        }
    })
    console.log(ships);
    

    ...

    reply
    0
  • Cancelreply