javascript - 将图像添加到 Array.push 元素

标签 javascript dynamic-arrays

我正在使用选择控件来级联从联赛、球队到球员的过滤器选择。我试图向每个玩家添加玩家个人资料图像,但我似乎无法在 Array.push 中添加图像。关于如何使用选择控件在玩家姓名旁边渲染图像有什么想法吗?我为许多值设置了级联过滤器,因此如果可能的话,我尝试不必为级联选择控件及其值数组创建新逻辑。

我当前的代码是:

    playerLists["MIN - Minnesotta Vikings"].push(["Player 1"]);         
    playerLists["ARI - Arizona Cardinals"].push(["Player 1"]);          
    playerLists["DAL - Dallas Cowboys"].push(["QB Tony Romo "]);    
    playerLists["DAL - Dallas Cowboys"].push(["RB Tyler Clutts"]); 
    playerLists["DAL - Dallas Cowboys"].push(["WR Cole Beasley"]); 

...等等....

感谢您提供的任何帮助。

最佳答案

为什么你不推送图像网址并像这样使用它:

playerLists["MIN - Minnesotta Vikings"].push({ name: "Player 1", url: "/path_to_image.png" });         
playerLists["ARI - Arizona Cardinals"].push({ name: "Player 2", url: "/path_to_image.png" });         
playerLists["DAL - Dallas Cowboys"].push({ name: "Player 3", url: "/path_to_image.png" });         
playerLists["DAL - Dallas Cowboys"].push({ name: "Player 4", url: "/path_to_image.png" }); 

之后,当您进行迭代时,您可以执行以下操作:

_.each(playerLists, function(player){
  console.log(player.name);
  console.log(player.url);
})

(要使用_.each,您必须安装http://underscorejs.org/)

关于javascript - 将图像添加到 Array.push 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26023450/

相关文章:

javascript - react , enzyme : Cannot read property 'history' of undefined

javascript - 如何使用 PHP、AJAX 和 AngularJS 创建实时聊天?

javascript - Cypress:为自定义命令设置超时

c - 动态数组 : Separating Numbers into Even and Odd Using C

c++ - 数组随机访问 C++

javascript - 如何使用 Puppeteer 保持 Chromium 打开?

dynamic-arrays - 动态数组中的移动次数

generics - Ada:具有可变大小数组属性的对象

c++ - 动态扩展指针数组

javascript - Jquery 多页 html