简体   繁体   English

如何在JavaScript中添加数组项?

[英]How to add array items in javascript?

In a wordpress plugin, there is a field box called 'data' where I can add code for it to pull, below in the code where it says 'data =' that is me referencing the box. 在wordpress插件中,有一个名为“数据”的字段框,我可以在其中添加要拉取的代码,在代码下方,该框显示“ data =”是我引用的框。 In the box: 箱子里头:

What I put in the field box entitled 'data': 我在标题为“数据”的字段中输入的内容:

{
  contacts: [
    { name: "Name 1", email: "email1@test.com" },
    { name: "Name 2", email: "email2@test.com" }
  ]
};

This is what I put in the global field box, for it to apply to everything. 这就是我在全局字段框中输入的内容,它适用于所有内容。

function (jQueryPopoverObj, mapObject, mapsvgInstance) {

  // "this" = clicked mapObject
  if(this.mapsvg_type == "region"){
    return '<b>'+this.id+'</b>' +
      this.data.contacts.map(function(contact) {
        return contact.name + '<br>' +
          '<a href="mailto:'+contact.email+ '">' + contact.email + '</a>'
         }).join('<br>');

  } else if (this.mapsvg_type == "marker"){
    return 'Marker - <b>'+this.id+'</b>, contact: '+this.data.email;
 }

}

I want to also add { seat: "County Seat"} to the data portion and add it in the function. 我还想将{seat:“ County Seat”}添加到数据部分,并将其添加到函数中。 I tried adding a line in the contacts, and then adding + '<br>' + contact.seat , after return contact.name, with no luck. 我尝试在联系人中添加一行,然后在返回contact.name之后添加+ '<br>' + contact.seat ,但没有运气。 Basically when it does the popover (which it pulls from global function for the template and the information from the data field box), I want it to have the CountySeat under the County Name (eg the County Seat for Harris County is Houston, so it would have Houston under Harris County). 基本上,当它执行弹出窗口时(它从模板的全局功能和数据字段框中的信息中提取),我希望它具有“县名”下的“县名”(例如,哈里斯县的“县名”为休斯顿,所以它将在哈里斯县辖下拥有休斯顿)。

Example of Lubbock County without the City name under it 没有城市名称的拉伯克县示例

var data = {
           contacts: [
                     { name: "Name 1", email: "email1@test.com" },
                     { name: "Name 2", email: "email2@test.com" }
                     ]
           }

To add a new property seat you can simply do this- 要添加新的属性席位,您只需执行以下操作-

data["seat"] = "County Seat"

or 要么

data.seat = "County Seat"

JSFiddle JSFiddle

It looks like you want to set the seat assignment on the contact? 您似乎要在联系人上设置座位分配? You'd have to get the contact from the array, looping through each one and figuring out the one you want to use, then do contact.seat = "XYZ", which will add a seat property to the contact object. 您必须从数组中获取联系人,循环遍历每个联系人并找出您要使用的联系人,然后执行contact.seat =“ XYZ”,这将为联系人对象添加一个seat属性。 You can then use contact.seat in the string output but you have to null check it because it may be null. 然后,您可以在字符串输出中使用contact.seat,但必须对其进行null检查,因为它可能为null。 To help with that, I'd recommend defaulting it to an empty string like: 为了解决这个问题,建议您将其默认为一个空字符串,例如:

 data = {
contacts: [
 { name: "Name 1", email: "email1@test.com", seat: "" },
 { name: "Name 2", email: "email2@test.com", seat: "" }
  ]
}

And then change it when it is assigned. 然后在分配它时更改它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM