【问题标题】:Is it possible for an object inside a variable array to have 2 values?变量数组中的对象是否有可能具有 2 个值?
【发布时间】:2012-03-01 12:24:51
【问题描述】:

我希望我的变量数组(在 JavaScript 中)有 2 个值:一个引号和一个真值或假值。 这是代码中最好放入的部分:

var q = new Array()



q[0]='There are some people who live in a dream world, and there are some who face reality; and then there are those who turn one into the other. <i>-By Douglas Everett</i>'

q[1]='Whether you think you can or whether you think you can\'t, you\'re right! <i>-Henry Ford</i>'

q[2]='I know of no more encouraging fact than the unquestionable ability of man to elevate his life by conscious endeavor. <i>-Henry David Thoreau</i>'

q[3]='Do not let what you cannot do interfere with what you can do. <i>-John Wooden</i>'

这是我拥有的众多引用之一(很快就会成为琐事,我从另一个站点借用了一些代码来随机生成其中一个。) 例如,我希望 q[3] 成为引号真值或假值。

这可能吗?关于我应该如何做的任何建议?

我是初学者,如果这是一个明显的问题,我很抱歉。

【问题讨论】:

    标签: javascript arrays variables object


    【解决方案1】:

    方法很多,这里有三种:

    var q = [];
    q.push("Quote#1");
    q.push(true);
    q.push("Quote#2");
    q.push(false);
    
    for(var i = 0; i < q.length-1; i++) {
        console.log(q[i], q[i+1]);
    }
    

    var q = [];
    q.push({quote: "Quote#1", flag: true});
    q.push({quote: "Quote#2", flag: false});
    for (var i = 0; i < q.length; i++) {
        console.log(q[i].quote, q[i].flag);
    }
    

    var q = [];
    q.push(["Quote#1", true]);
    q.push(["Quote#2", false]);
    for (var i = 0; i < q.length; i++) {
        console.log(q[i][0], q[i][1]);
    }
    

    【讨论】:

      【解决方案2】:

      我可能会为此使用对象文字。比如:

      var q = [];
      
      q[0]= {Question: 'There are some people who live in a dream world, and there are some who face reality; and then there are those who turn one into the other. <i>-By Douglas Everett</i>', Answer: true};
      q[1]= {Question: 'Whether you think you can or whether you think you can\'t, you\'re right! <i>-Henry Ford</i>', Answer: true};
      q[2]= {Question: 'I know of no more encouraging fact than the unquestionable ability of man to elevate his life by conscious endeavor. <i>-Henry David Thoreau</i>', Answer: false};
      q[3]= {Question: 'Do not let what you cannot do interfere with what you can do. <i>-John Wooden</i>', Answer: false};
      
      window.alert(q[1].Question);
      window.alert(q[1].Answer);
      

      【讨论】:

        【解决方案3】:
        var q = new Array()
        
        
        
        q[0]= ['There are some people who live in a dream world, and there are some who face reality; and then there are those who turn one into the other. <i>-By Douglas Everett</i>', true]
        
        q[1]=['Whether you think you can or whether you think you can\'t, you\'re right! <i>-Henry Ford</i>', false]
        
        q[2]=['I know of no more encouraging fact than the unquestionable ability of man to elevate his life by conscious endeavor. <i>-Henry David Thoreau</i>', true]
        
        q[3]=['Do not let what you cannot do interfere with what you can do. <i>-John Wooden</i>', false]
        
        if (q[3][1]) {
            print q[3][0]
        }
        

        【讨论】:

          【解决方案4】:

          使用嵌套数组。

          q[0] = ['Some quote',true];
          

          那么q[0][0] 是引用,q[0][1] 是真/假值。

          【讨论】:

            【解决方案5】:

            好吧,如果我跟着你,你想要的是一个对象数组:

            [{flag: true, text: "If you choose the red pill..."},...]
            

            这有意义吗?

            关键是你要在数组的每个元素上都有一个 JS 对象。

            【讨论】:

              【解决方案6】:

              您可以使用带有属性的对象字面量来保存引号,而另一个属性来保存布尔值。所以,例如:

              var q = []; // NEVER use new Array() and ALWAYS put a semicolon at the end of lines.
              
              q[0] = {
                  quote: 'There are some people who live in a dream world, and there are some who face reality; and then there are those who turn one into the other. <i>-By Douglas Everett</i>',
                  someValue: true
              };
              
              // ...
              
              alert(q[0].quote); // There are some people...
              alert(q[0].someValue); // true
              

              【讨论】:

              • 您的所有答案都非常有用,而且速度非常快。我发现这个是彻底和整洁的,谢谢,minitech!
              • 我不会说“永远不要使用 new Array()”,它实际上与样式有很多关系。 [] 更简洁易读,但使用 new Array() 并没有错。
              • 只是好奇.. 使用 new Array() 与 [] 的缺点是什么?
              • @arturnt:是的,实际上,使用new Array 有一些问题。 new Array(1, 2, 3, 4, 5)[1, 2, 3, 4, 5]new Array(5)[,,,,]。另外,Array 可以重新定义为任何函数; [] 总会给你一个数组。
              • @arturnt:这可能令人难以置信的混乱。没有充分的理由使用new Array(element1, element2, elementN...) 而不是[element1, element2, elementN...],如果您使用的是new Array(nElements),那么它可能是1)预优化或2)重复字符串的一种hacky方式。以这种方式重复一个字符串可能没问题,但预优化通常是不必要的,只会混淆未来的代码编辑器。
              【解决方案7】:

              使其成为一个独立的数组。

              q[3] = ['My string....', true];
              

              然后使用q[3][0] 访问“我的字符串....”并使用q[3][1] 访问布尔值。


              附带说明,在创建数组时,应使用简写 [] 而不是 new Array()

              var q = [];
              

              【讨论】:

              • 每次看到这个我都会畏缩......它会导致魔法数组索引。
              猜你喜欢
              • 1970-01-01
              • 2012-08-22
              • 1970-01-01
              • 2020-10-31
              • 1970-01-01
              • 2016-08-16
              • 2016-12-04
              • 2016-07-02
              • 1970-01-01
              相关资源
              最近更新 更多