[英]Is there a way to replace individual array elements with one repeated element?
I'm trying to replace the elements of an array with 1 element repeated for each index. 我正在尝试用对每个索引重复1个元素来替换数组的元素。
Eg ['12','a','drink','cookie']
becomes ['0','0','0','0']
例如
['12','a','drink','cookie']
变为['0','0','0','0']
I'm learning basic JavaScript
and trying to figure out how to do this. 我正在学习基本的
JavaScript
并试图弄清楚如何做到这一点。 I've tried forEach()
and splice()
but I keep ending up with just one index (eg ['0']
). 我已经尝试过
forEach()
和splice()
但是我一直以一个索引(例如['0']
)结束。 I feel like I can solve this with a for
loop but what to put in? 我觉得我可以用
for
循环解决这个问题,但是要输入什么呢? The code below is my latest effort but it didn't run. 下面的代码是我最近的工作,但是没有运行。
function replace()
{
var Word = 'test';
var newWordArray = Array.from(Word);
for (var i = 0; i < newWordArray.length; i++); {
i = '0';
}
}
I expect ['0','0','0','0']
as output, but I get undefined when trying to run the function. 我希望将
['0','0','0','0']
作为输出,但是在尝试运行该函数时却未定义。
You have a couple of issues with your code: 您的代码有几个问题:
i = '0'
isn't doing what you think it is doing. i = '0'
并没有按照您认为的去做。 Here i
is just a number, representing an index in your array. 在这里,
i
只是一个数字,代表数组中的索引。 Changing i
will do nothing as you're not doing anything with the value of i
itself. 改变
i
不会做任何事情,因为您不会以i
本身的价值做任何事情。 Instead your need to change the element at index i
. 相反,您需要更改索引
i
处的元素。 You can do this by doing newWordArray[i] = 0
. 您可以通过执行
newWordArray[i] = 0
。 This way you are saying change the element at index i
in your array newWordArray
. 这样,您说的是更改数组
newWordArray
中索引i
处的元素。
You are not returning anything from your function. 您没有从函数中返回任何东西。 In order for your function to get assigned a value once called, you need to
return
said value. 为了使您的函数在被调用后被分配一个值,您需要
return
所述值。 Thus, as you want your function call to output (return) a new array, you need to return newWordArray
at the end of your function. 因此,当您希望函数调用输出(返回)新数组时,需要在函数末尾
return newWordArray
。
Although it isn't necessary, I suggest that you add a parameter to your function called word
. 尽管没有必要,但我建议您在函数中添加一个名为
word
的参数。 This way the function can accept a string (or array) and give you the required output. 这样,函数可以接受字符串(或数组)并提供所需的输出。
See working example below: 请参见下面的工作示例:
function replace(word) { // accept a word as an input to use in your function var newWordArray = Array.from(word); // create an array of characters from your inputted word for (var i = 0; i < newWordArray.length; i++) { // remove ';' colon here newWordArray[i] = 0; // Set every element (by accessing it using its index 'i') to 0 } return newWordArray; // output (return) the new array } console.log(replace("test")); // input "test" as the word variable console.log(replace(['12','a','drink','cookie']));
Also note Array.from([1, 2, 3])
will give an array in return, so [1, 2, 3]
. 还要注意
Array.from([1, 2, 3])
将返回一个数组,因此[1, 2, 3]
。 Thus the above works on both strings and arrays. 因此,以上内容适用于字符串和数组。
Alternatively, a different approach to achieving the same thing as above would be to use Array.prototype.fill()
. 另外,实现上述相同目的的另一种方法是使用
Array.prototype.fill()
。 Here I have used ES6 arrow functions with destructing assignment : 在这里,我使用了具有破坏性分配 功能的ES6箭头功能 :
const replace = ({length}) => Array(length).fill(0); console.log(replace("testing")); console.log(replace(['12','a','drink','cookie']));
Nick Parsons , already explained so good how to change your code to approach your goal, however, and since you are already using Array::from() , you can use the mapFn
argument and reduce your code to be more simple, like this: 尼克·帕森斯 ( Nick Parsons )已经很好地解释了如何更改代码以实现目标,但是,由于您已经在使用Array :: from() ,因此可以使用
mapFn
参数并将代码简化为如下所示:
const replace = (word, replaceVal) => Array.from(word, x => replaceVal); console.log(replace("word", 0)); console.log(replace("Hello World", 5));
You just have to assign it to each element in the array: 您只需将其分配给数组中的每个元素:
for (var i = 0; i < newWordArray.length; i++); {
newWordArray[i] = '0';
}
You were assigning the value to the iterator. 您正在将值分配给迭代器。
Here array map shortens your code 在这里数组映射可以缩短您的代码
var Word = 'test'; //var newWordArray = Array.from(Word); let j=Array.from(Word).map(a=>a='0'); console.log(j)
I prefer splice and forEach to achieve this 我更喜欢splice和forEach实现这一目标
var contents = ['a','b','c','d'];
document.getElementById('LW').value = contents
contents.forEach(function(element, index) {
contents.splice(index, 1, "0");
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.