javascript - How does Vue traverse data from two JSONs and assign values ​​to the DOM based on the same Key?
黄舟
黄舟 2017-05-18 11:02:34
0
2
611

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?

  • Destroyer

  • Aircraft carrier

  • Battleship

  • Cruiser

Thanks!

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(2)
伊谢尔伦

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

Or use computed properties

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

...

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template