变量数组中的对象是否可能有2个值
Is it possible for an object inside a variable array to have 2 values?
我希望我的变量数组(在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]);
}
相关文章:
- 边栏按钮和infoWindow是否可能有不同的内容
- 是否可能复选框获胜't检查数据库中的行中是否有值
- 变量数组中的对象是否可能有2个值
- 在Javascript中,从localStorage检索时是否可能有一个等待动画
- 是否有可能有完整的CRUD功能在剑道网格与本地数据
- 是否有可能有一个按钮,可以切换音频的播放状态
- 八字胡js:是否有可能有八字胡渲染HTML是在json对象传入
- 使用GET方法选择表单——是否有可能有一个不向URL添加任何内容的选项?
- 是否可能有一个Javascript函数有两个或更多的函数在里面
- 是否可能有一个Javascript变量有两个或更多的函数在里面
- 在highcharts中是否可能有两个图表,共享相同的x轴,但彼此相邻
- 是否有可能有一个链接不可见/隐藏在网页源,但活跃时,按钮点击
- 是否有可能有JSON.对保存函数进行字符串化
- 是否有可能有一个greasemonkey脚本,是自动加载到一个网页内的所有框架
- 在for循环中是否可能有第三个条件表达式?
- 是否有可能有不同的javascript代码为每个浏览器
- 在iOS的Safari中,是否有可能有一个按钮将文本复制到剪贴板
- 是否有可能有多个谷歌图表在一个页面
- 是否有可能有一个来回按钮
- 是否可能有一个没有内容溢出的onscroll事件?