变量数组中的对象是否可能有2个值

Is it possible for an object inside a variable array to have 2 values?

本文关键字:是否 可能有 2个值 对象 数组 变量      更新时间:2023-09-26

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

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]是一个引号是一个true或false值。

这可能吗?有什么建议我应该怎么做吗?

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

您可以使用对象文字,其中一个属性用于保存引号,另一个属性则用于保存布尔值。例如:

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

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

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

这有道理吗?

关键是您希望在数组的每个元素上都有一个JS对象。

使其本身成为一个数组。

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

然后使用q[3][0]访问"My string….",使用q[3][1]访问布尔值。


附带说明一下,创建数组时,应该使用缩写[]表示法,而不是new Array():

var q = [];

使用嵌套数组。

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

q[0][0]是引号,q[0][1]是真/假值。

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]
}

我可能会为此使用对象文字。类似于:

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);

有很多方法,这里有三种:

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]);
}