在Javascript中,我试图创建一个新的div.这是我为了制作div而添加的内容.
html+= "<div>" + concerts + "</div>";
Run Code Online (Sandbox Code Playgroud)
但在我的浏览器中显示[object HTMLCollection] div标签应该在哪里.
音乐会是预定义的变体.
任何人都可以向我解释我可能缺少什么来完成这项工作.提前致谢.
我的JSON在下面.它包含两个对象,每个对象都有几个键值对.如何搜索整个JSON数组并将包含特定字符串的对象作为值?
在这种情况下,我需要使用coupon_code:COUPON1拉取对象,以便我可以拉出该优惠券的ID.
简而言之,我只需要使用coupon_code:COUPON1获取优惠券的ID
[Object, Object]
0: Object
coupon_code: "COUPON1"
created_at: "2013-06-04T13:50:20Z"
deal_program_id: 1
id: 7
updated_at: "2013-06-04T13:50:20Z"
__proto__: Object
1: Object
coupon_code: "COUPON3"
created_at: "2013-06-04T15:47:14Z"
deal_program_id: 1
id: 8
updated_at: "2013-06-04T15:47:14Z"
Run Code Online (Sandbox Code Playgroud)
谢谢 :)
我在Code School上练习,我不明白循环是如何工作的.
有一个i,但我没有看到它使用.通常你在某个地方使用它.否则,for循环是什么?
var puzzlers = [
function(a) { return 8 * a - 10; },
function(a) { return (a - 3) * (a - 3) * (a - 3); },
function(a) { return a * a + 4; },
function(a) { return a % 5; }
];
var start = 2;
var applyAndEmpty = function(input, queue) {
var length = queue.length;
for (var i = 0; i < length; i++) {
input = queue.shift()(input);
}
return input; …Run Code Online (Sandbox Code Playgroud) 我有以下json数组:
var champions = [{
"1":{
"name":"Aatrox",
"role1":"Top",
"role2":"Jungle"},
"2":{
"name":"Ahri",
"role1":"Middle"},
"3":{"
name":"Akali",
"role1":"Middle",
"role2":"Top"
}
}];
Run Code Online (Sandbox Code Playgroud)
我试图用我发现的代码片段遍历这个数组:
$(document).ready(function(){
var x = 1;
for(var i = 0; i < champions.length; i++) {
console.log(champions[i][x].name);
x++;
}
});
Run Code Online (Sandbox Code Playgroud)
但它只给了我"Aatrox"并按原样停止champions.length1.
我怎样才能遍历所有名称的数组?
我正在做一个新的个人练习,我正在开始JS OOP.
我的目标是:创建一个小机器人军队,每个机器人自我介绍.
我的代码很棒而且很有效,但我想改进它.我想将我的机器人添加到一个数组中,并为每个机器人的引入创建一个循环.
这并不难,但我不能在OOP Javascript中创建一个数组.我不明白我怎么能用机器人的所有功能创建一个.
这是我的代码:
// Objet Robot
function Robot(nick, pv, maxSpeed, position) {
this.nick = nick;
this.pv = pv;
this.maxSpeed = maxSpeed;
this.position = position;
};
//Méthode présentation des robots
Robot.prototype.sePresenter = function() {
console.log("Bonjour je m'appelle " + this.nick + ". J'ai " + this.pv + " points de vie." + " Je me déplace à " + this.maxSpeed + " cases par seconde. Je suis à la case de coordonnées " + this.position);
};
// …Run Code Online (Sandbox Code Playgroud) 我有一个数组中的电子邮件列表:
["gomand@gmail.com", "terry@yahoo.com", "123Yu@gmail.com"]
Run Code Online (Sandbox Code Playgroud)
我如何遍历数组并将每封电子邮件作为自己的属性推送到对象中:对象如下所示:
{
email1: "gomand@gmail.com",
email2: "terry@yahoo.com",
email3: "123Yu@gmail.com"
}
Run Code Online (Sandbox Code Playgroud) 我试图理解/理解.forEachJavascript中的循环.
现在,我明白了
let numberArray = [1, 2, 3, 4]
numberArray.forEach(x => {
console.log(x)
})Run Code Online (Sandbox Code Playgroud)
正如预期的那样它将返回输出
1
2
3
4
Run Code Online (Sandbox Code Playgroud)
现在,因为forEach是一个函数,我决定返回一些东西并存储它的值
let numberArray = [1, 2, 3, 4]
let newNum = numberArray.forEach(x => x)
console.log(newNum)Run Code Online (Sandbox Code Playgroud)
这是在返回undefined时,我清楚地回东西,它再次它是一个功能.
有人能告诉我,我在这里失踪了吗?我的意思是我是forEach循环的背景
[更新:]我知道很多其他方法来实现我的目标,我更感兴趣了解更多有关此行为的信息forEach
我有一个关于 map 的问题。如您所知,通过使用 map,我们可以迭代数组并能够更改数组的元素。为此,有两种方法,我在下面列出了它们。哪种方法具有较低的复杂性和较高的性能?(在示例数组并不大,但想想现实世界,比如数组中有 500 个元素)
var numbers = [4, 9, 16, 25];
numbers=numbers.map(e=>{
return e=e*2;
})
Run Code Online (Sandbox Code Playgroud)
在这种方法中,将每个返回值分配给当前数组。
numbers.map((e,a)=>{
numbers[a]=e*2;
})
Run Code Online (Sandbox Code Playgroud)
在每次迭代中,我们需要按索引访问数组以查找元素,我认为这比上面的更糟糕,但我不确定。
我正在制作网站,在那里我创建了许多在输出中分配的标签,如下所示
使用帖子末尾的小提琴链接
<!-- lets say that I want to make a kind of board to show some game clans or... whatever -->
<label class='team' name='ally'>Madcowz</label><BR>
<label class='team' name='ally'>Fluffy Unicorns</label><BR>
<label class='team' name='enemy'>Blue bastards</label><BR><BR>
<b>JS stuff:</b>
<div id='printSomeOutputHere'></div>
<!-- The problem is that the NAME tag does not exist for label in this case: -->
<!-- I can't use ID because ID should be unique values -->
<script>
var teams = $(".team");
for(i=0; i<teams.length; i++)
{
document.getElementById('printSomeOutputHere').innerHTML += teams[i].name + ": …Run Code Online (Sandbox Code Playgroud)我有多个span元素,我尝试删除该类activeLang。我怎样才能做到这一点?
<div class="header_content_lang">
<span class="langNav">
...
</span>
<span class="langNav">
...
</span>
<span class="langNav activeLang">
...
</span>
</div>
Run Code Online (Sandbox Code Playgroud)
我这样尝试过,就像这里建议的那样:
var x = document.getElementsByClassName(".activeLang");
[].forEach.call(x, function(el) {
el.classList.remove(".activeLang");
});
console.log(x[0]);
console.log(x[1]);
console.log(x[2]);
Run Code Online (Sandbox Code Playgroud)
但所有日志的输出都是“未定义”的。