数组是否包含?

时间:2013-10-09 08:31:59

标签: javascript arrays var

我在JS中有以下数组

 var wordselection = [
{ 'word': "Pomme", "gender": "m", },
{ "word": "Banane", "gender": "f", },
{ "word": "Ananas", "gender": "m", },
{ "word": "Chat", "gender": "f", },
{ "word": "Chien", "gender": "m", },
{ "word": "Poisson", "gender": "f", },

];   

function randomword(){
var chosen = wordselection[Math.floor(Math.random()*wordselection.length)].word
document.getElementById("wordinput").innerHTML=chosen;
}

我目前有随机字函数,它从数组中生成一个随机“单词”并将其放入div(例如pomme,chat)

我需要创建另一个函数来检查div中的随机单词究竟是什么,然后计算出数组中该单词的性别是“m”还是“f”。

例如,如果随机词生成为“泊松”,我需要一个实现“泊松”的函数,并在数组中查看它是性别'f'

如何实现这一目标?

5 个答案:

答案 0 :(得分:2)

这是一个简单的JS解决方案,可以满足您的需求:

<强> HTML

<div id="test">Chat</div>

<强> JavaScript的:

var wordselection = [
{ 'word': "Pomme", "gender": "m" },
{ "word": "Banane", "gender": "f"},
{ "word": "Ananas", "gender": "m" },
{ "word": "Chat", "gender": "f" },
{ "word": "Chien", "gender": "m" },
{ "word": "Poisson", "gender": "f" }

];   

var word = document.getElementById('test').innerHTML;

var item = wordselection.filter(function(el){
  return el.word == word
})[0];

console.log(item && item.gender);

如果div中的单词不在列表中,则会记录未定义。

DEMO:http://jsbin.com/AlOtINE/1/edit

编辑:如果您需要检查许多此类字词,可以将它们存储在地图中(也称为JavaScript中的对象),以获得更好的性能:

var wordselection = {
  'Pomme' : 'm',
  'Banane' : 'f',
  'Ananas' : 'm',
  'Chat' : 'f',
  'Chien' : 'm',
  'Poisson' : 'f'
}

然后过滤部分就像:

一样简单
console.log(wordselection[word]); // gender or undefined

如果您从外部来源获得了字选择但仍希望将其转换为上述格式,则可以使用此代码段:

var optimizedwordselection;
for (var i=0; i<wordselection.length; i++) {
  optimizedwordselection[wordselection.word] = wordselection.gender;
}

答案 1 :(得分:0)

我不知道您的完整代码结构或实际实现。但是,我会采用两种方式中的任何一种。可能还有很多其他方法可以实现解决方案。

  1. 使用http://api.jquery.com/html/复制DIV元素的HTML 在变量中。 HTML将存储为字符串。搜索 字符串并查看“单词”是否存在。如果它有或没有,请采取 适当的行动。

  2. 创建临时数组。将键值对存储在该Array中。例如。 { “泊松”, “F”}。稍后当您想要查找“单词”是否存在时,只需在数组中搜索它们是否存在键。如果KEY存在,那么您可以随时检查其性别值。

  3. var holder = new Array();
    
    function randomword(){
        var chosen = wordselection[Math.floor(Math.random()*wordselection.length)].word
    
        if( checkPost(chosen) ){
            document.getElementById("wordinput").innerHTML=chosen;
            holder['chosen'] = gender_variable;
        }
    }
    
    function checkPost(word){
        if( holder[word] === undefined ) {
            return null;
        }
        return holder[word];
    }
    

答案 2 :(得分:0)

以下是您想要使用JQuery的内容:

JavaScript代码:

 var wordselection = [
{ 'word': "Pomme", "gender": "m", },
{ "word": "Banane", "gender": "f", },
{ "word": "Ananas", "gender": "m", },
{ "word": "Chat", "gender": "f", },
{ "word": "Chien", "gender": "m", },
{ "word": "Poisson", "gender": "f", },

];  


     $.each( wordselection, function(key, value) {

         if ( value.word == $('#result').html() )
         {
             alert(value.gender);
         }
     })

http://jsfiddle.net/GWUxc/

或使用随机功能:

 var wordselection = [
{ 'word': "Pomme", "gender": "m", },
{ "word": "Banane", "gender": "f", },
{ "word": "Ananas", "gender": "m", },
{ "word": "Chat", "gender": "f", },
{ "word": "Chien", "gender": "m", },
{ "word": "Poisson", "gender": "f", },

];  

function randomword(){
    var chosen = wordselection[Math.floor(Math.random()*wordselection.length)].word
document.getElementById("wordinput").innerHTML=chosen;
}

     randomword()
     $.each( wordselection, function(key, value) {

         if ( value.word == $('#wordinput').html() )
         {
             alert(value.gender);
         }
     })

http://jsfiddle.net/GWUxc/1/

无论如何,我更喜欢使用filter方法的解决方案:Does array contain?

答案 3 :(得分:0)

您是否可以使用任何其他库?如果我是你,我会使用lo dash,它可以为你处理这类事情而无需你自己迭代收集。例如,您需要做的就是:

var wordselection = [
    { 'word': "Pomme", "gender": "m", },
    { "word": "Banane", "gender": "f", },
    { "word": "Ananas", "gender": "m", },
    { "word": "Chat", "gender": "f", },
    { "word": "Chien", "gender": "m", },
    { "word": "Poisson", "gender": "f", },
];

var gender = _.find(wordSelection, { 'word': "Banane" }).gender;

答案 4 :(得分:0)

您可以遍历JSON对象数组:

var wordselection = [
  { 'word': "Pomme", "gender": "m", },
  { "word": "Banane", "gender": "f", },
  { "word": "Ananas", "gender": "m", },
  { "word": "Chat", "gender": "f", },
  { "word": "Chien", "gender": "m", },
  { "word": "Poisson", "gender": "f", },
]; 

var selectedWord = "Chien";
var selectedWordGender;
for(var i = 0; i < wordselection.length; i++) {
  if(wordselection[i].word === selectedWord)
  {
      selectedWordGender = wordselection[i].gender;
  }
}     

console.log(selectedWordGender);