设置初始值,然后将下一个值作为初始值。

huangapple go评论113阅读模式
英文:

Set initial value and then make next value as the initial one

问题

  1. 我从一个函数将作为字符串的数组值传递给另一个函数
  2. const func1 = () => {
  3. let myArray = ["Harry", "Hermione", "Ron", "Ginny", "Luna"];
  4. myArray.map((arr, i) => func2(arr, i));
  5. }
  6. const func2 = (arrString, index) => {
  7. let before, after;
  8. if (index === 0) {
  9. before = arrString;
  10. after = null;
  11. }
  12. }
  13. func1();
  14. 在第一次迭代(index === 0)之后,我希望after等于before的值,当before等于arrStringindex = 1)时。我不确定如何循环这个。
  15. 期望结果:
  16. before = "Harry"after = null
  17. before = "Hermione"after = "Harry"
  18. before = "Ron"after = "Hermione"
  19. before = "Ginny"after = "Ron"
  20. before = "Luna"after = "Ginny"
  21. before = nullafter = "Luna"
英文:

I have values coming from an array as a string from one function to another

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. const func1 = () =&gt; {
  2. let myArray = [&quot;Harry&quot;, &quot;Hermione&quot;, &quot;Ron&quot;, &quot;Ginny&quot;, &quot;Luna&quot;];
  3. myArray.map((arr, i) =&gt; func2(arr, i));
  4. }
  5. const func2 = (arrString, index) =&gt; {
  6. let before, after;
  7. if (index === 0) {
  8. before = arrString;
  9. after = null;
  10. }
  11. }
  12. func1();

<!-- end snippet -->

After the first iteration(index===0) I want after = before value a when and before = arrString(index=1). I am not sure how to loop that.

Expected results:

  1. before = &quot;Harry&quot;, after = null
  2. before = &quot;Hermione&quot;, after = &quot;Harry&quot;
  3. before = &quot;Ron&quot;, after = &quot;Hermione&quot;
  4. before = &quot;Ginny&quot;, after = &quot;Ron&quot;
  5. before = &quot;Luna&quot;, after = &quot;Ginny&quot;
  6. before = null, after = &quot;Luna&quot;

答案1

得分: 1

  1. `map()` `forEach()` 也将正在迭代的数组作为第三个参数传递给回调函数您可以使用这个参数来访问数组的其他索引
  2. const func1 = () => {
  3. let myArray = ["Harry", "Hermione", "Ron", "Ginny", "Luna"];
  4. myArray.forEach((elt, i, arr) => func2(elt, i, arr));
  5. }
  6. const func2 = (arrString, index, array) => {
  7. let before = arrString, after;
  8. if (index === 0) {
  9. after = null;
  10. } else {
  11. after = array[index - 1];
  12. }
  13. console.log(`before = ${before} after = ${after}`);
  14. }
  15. func1();
英文:

map() and forEach() also pass the array being iterated over as a third argument to the callback function. You can use this to access other indexes of the array.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. const func1 = () =&gt; {
  2. let myArray = [&quot;Harry&quot;, &quot;Hermione&quot;, &quot;Ron&quot;, &quot;Ginny&quot;, &quot;Luna&quot;];
  3. myArray.forEach((elt, i, arr) =&gt; func2(elt, i, arr));
  4. }
  5. const func2 = (arrString, index, array) =&gt; {
  6. let before = arrString, after;
  7. if (index === 0) {
  8. after = null;
  9. } else {
  10. after = array[index - 1];
  11. }
  12. console.log(`before = ${before} after = ${after}`);
  13. }
  14. func1();

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年8月10日 23:28:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76877211.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定