Javascript:如何将数组值复制到变量中
Javascript: How to copy an arrays value into a variable
所以我正在努力解决这个问题这是一个简单的问题,但它一直说我没有正确处理。问题是要我获取数组的索引值。(在本例中为 "batman")并将其值复制到数组中。所以我的第一个想法是使用 .slice()
来完成这项工作。但是当我 console.log()
将它切片成一个变量 (var thirdHero
) 后,我看到放入变量的是 ["batman"]
而不是 "batman"
。任何有关如何解决此问题的帮助将不胜感激!
//#7 Create an array of strings that are the 7 primary colors in the rainbow - red, orange, yellow, green, blue, indigo, violet (lower-case). Call your array rainbowColors
var rainbowColors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'];
// #8 Using this array do the following
var heroes = ['superman', 'batman', 'flash'];
// add 'wonderwoman' to the end
heroes.push('wonderwoman');
console.log(heroes);
// remove 'superman' and store him in a variable called firstHero
var firstHero = heroes.shift();
// add 'spongebob' to the start of the array
heroes.unshift('spongebob');
// remove 'flash' from the array and store him in a variable called secondHero
var secondHero = heroes.splice(2, 1);
console.log(heroes);
// leave batman in the array but put a copy of him on a variable called thirdHero
var thirdHero = heroes.slice(1, 2);
console.log(thirdHero);
您无需对数组进行切片即可从中获取对单个值的引用。
'slice' 函数returns 你自己一个数组
就这样
var hero = heroes[2];
所以我正在努力解决这个问题这是一个简单的问题,但它一直说我没有正确处理。问题是要我获取数组的索引值。(在本例中为 "batman")并将其值复制到数组中。所以我的第一个想法是使用 .slice()
来完成这项工作。但是当我 console.log()
将它切片成一个变量 (var thirdHero
) 后,我看到放入变量的是 ["batman"]
而不是 "batman"
。任何有关如何解决此问题的帮助将不胜感激!
//#7 Create an array of strings that are the 7 primary colors in the rainbow - red, orange, yellow, green, blue, indigo, violet (lower-case). Call your array rainbowColors
var rainbowColors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'];
// #8 Using this array do the following
var heroes = ['superman', 'batman', 'flash'];
// add 'wonderwoman' to the end
heroes.push('wonderwoman');
console.log(heroes);
// remove 'superman' and store him in a variable called firstHero
var firstHero = heroes.shift();
// add 'spongebob' to the start of the array
heroes.unshift('spongebob');
// remove 'flash' from the array and store him in a variable called secondHero
var secondHero = heroes.splice(2, 1);
console.log(heroes);
// leave batman in the array but put a copy of him on a variable called thirdHero
var thirdHero = heroes.slice(1, 2);
console.log(thirdHero);
您无需对数组进行切片即可从中获取对单个值的引用。
'slice' 函数returns 你自己一个数组
就这样
var hero = heroes[2];