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

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

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

问题

我从一个函数将作为字符串的数组值传递给另一个函数

const func1 = () => {
  let myArray = ["Harry", "Hermione", "Ron", "Ginny", "Luna"];
  myArray.map((arr, i) => func2(arr, i));
}

const func2 = (arrString, index) => {
  let before, after;
  if (index === 0) {
    before = arrString;
    after = null;
  }
}

func1();

在第一次迭代(index === 0)之后,我希望after等于before的值,当before等于arrString(index = 1)时。我不确定如何循环这个。

期望结果:

before = "Harry",after = null
before = "Hermione",after = "Harry"
before = "Ron",after = "Hermione"
before = "Ginny",after = "Ron"
before = "Luna",after = "Ginny"
before = null,after = "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 -->

const func1 = () =&gt; {
  let myArray = [&quot;Harry&quot;, &quot;Hermione&quot;, &quot;Ron&quot;, &quot;Ginny&quot;, &quot;Luna&quot;];
  myArray.map((arr, i) =&gt; func2(arr, i));
}

const func2 = (arrString, index) =&gt; {
  let before, after;
  if (index === 0) {
    before = arrString;
    after = null;
  }
}

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:

before = &quot;Harry&quot;, after = null
before = &quot;Hermione&quot;, after = &quot;Harry&quot;
before = &quot;Ron&quot;, after = &quot;Hermione&quot;
before = &quot;Ginny&quot;, after = &quot;Ron&quot;
before = &quot;Luna&quot;, after = &quot;Ginny&quot;
before = null, after = &quot;Luna&quot;

答案1

得分: 1

`map()`  `forEach()` 也将正在迭代的数组作为第三个参数传递给回调函数您可以使用这个参数来访问数组的其他索引

const func1 = () => {
  let myArray = ["Harry", "Hermione", "Ron", "Ginny", "Luna"];
  myArray.forEach((elt, i, arr) => func2(elt, i, arr));
}

const func2 = (arrString, index, array) => {
  let before = arrString, after;
  if (index === 0) {
    after = null;
  } else {
    after = array[index - 1];
  }
  console.log(`before = ${before} after = ${after}`);
}

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 -->

const func1 = () =&gt; {
  let myArray = [&quot;Harry&quot;, &quot;Hermione&quot;, &quot;Ron&quot;, &quot;Ginny&quot;, &quot;Luna&quot;];
  myArray.forEach((elt, i, arr) =&gt; func2(elt, i, arr));
}

const func2 = (arrString, index, array) =&gt; {
  let before = arrString, after;
  if (index === 0) {
    after = null;
  } else {
    after = array[index - 1];
  }
  console.log(`before = ${before} after = ${after}`);
}

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:

确定