English 中文(简体)
如何从Javascript联系阵列中收回价值?
原标题:How to retrieve values from Javascript associative array hash table?

我有一整套术语及其定义:

 var definitions = [
        {label : "abdomen", def: "stomach, stomach area, belly, tummy"},
        {label : "ability", def: "skill"},
        {label : "abolish", def: "end, do away with, get rid of"},
                    etc
                ]

I want to write a f或 loop that basically runs through every single "label" and lists them out. F或 simply listing a single term I ve tried different ways and none have w或ked:

 var definitionDiv = document.getElementById("definitionContainer");
 definitionDiv.appendChild(document.createTextNode(definitions["abdomen"));

 definitionDiv.appendChild(document.createTextNode(definitions.def["abdomen"));

 definitionDiv.appendChild(document.createTextNode(definitions.label[1]));

And none of them have w或ked. My desired end result is using a f或 loop to list all the labels as links which point to the def 或 definition. But first I need to overcome this hurdle.

问题回答

对开端人来说,你真的希望是:

definitionDiv.appendChild(document.createTextNode(definitions[0].label));

查阅<代码>def 财产,definitions [0].def。 住宿:

var i;
for (i = 0; i < definitions.length; i++) {
    var definition = definitions[i];
    // Use definition.label and definition.def however you want here.
}

If i m following correctly you just want to end up with a list of links, label being what the user sees and the "def" being the "href" of the link?

如果是的话,你可以做这样的事情:

var definitions = [
  {
    label: "abdomen",
    def: "stomach, stomach area, belly, tummy"
  },
  {
    label: "ability",
    def: "skill"
  },
  {
    label: "abolish",
    def: "end, do away with, get rid of"
  }

 ];

$.each(definitions, function(index, item) {
    $( #definitionContainer ).append( <a href="  + item.def +  ">  + item.label +  </a> );
});

a 这方面的工作实例:JSFiddle

列出单一术语:

function getDef( which ) {
  for(var i=0,j=definitions.length; i<j; i++){
    if( definitions[i].label == which )
      return definitions[i].def;
  }
}

因此:

var foo = getDef( ability ); // etc

1. 建立一个完整的定义清单:

var finalResult = $( <dl/> );

$( definitions ).each( function(i,e){
    var $dt = $( <dt/> ).html( e.label );
    var $dd = $( <dd/> ).html( e.def );
    finalResult.append( $dt, $dd );
});

$( #answer ).append( finalResult );

这里的例子有:。 http://jsfiddle.net/VyXdG/

现有表格definitions[i].label <>/code>和definitions [i].def是你重新研究的。

如果你希望用自己的标签而不是数字指数来获取定义,那么你也可以尝试以代表身份行事,而你们所期望的只是一阵。

在这种情况下,你可以把你的阵列重新组织成物体,钥匙将是标签。

var definitions = { // objects are hashes 
    abdomen: "stomach, stomach area, belly, tummy",
    ability: "skill",
    abolish: "end, do away with, get rid of",
    // key: value,
    // ...
};

当然,这改变了你通过这些物品的方式,现在成为数字<>。

for (var label in definitions) {
   if (definitions.hasOwnProperty(key)) {
      // label is label
      // definition is definitions[label]
      alert(label + " => " + definitions[label]);
   }
}




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.