移动数组中的元素,但基于另一个数组的数据。

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

Javascript: move elements in an array but based on another array data

问题

这是我试图做的事情。我有一个名为"arr"的数组,严格包含4个元素,不能多也不能少。我还有另一个名为"shiftAmounts"的数组。

所以这是我拥有的以及我正在寻找的结果...

arr[A, B, C, D]
shiftAmounts[0, 1, 1, 3]
结果: arr[A,"Empty", B, C]

所以从右边(第四个元素)开始,我必须将D向右移动3个位置。由于不能超过4个元素,所以它被删除了。
接下来,C向右移动一个位置。
接下来,B向右移动一个位置。
接下来,A不移动,因为它的移动量为0。
元素之间永远不会交叉。
更改必须在原地进行。感谢您的帮助。
Seby

英文:

Here's what i'm trying to do. I have an Array arr with strickly 4 elements. can't have more and can't have less. I have another Array called shiftAmounts.
So here's what I have and the result i'm looking for...

arr[A, B, C, D]
shiftAmounts[0, 1, 1, 3]
result: arr[A,"Empty", B, C]

So starting from the right (the fouth elements) it<s important, I have to move the D to the right by 3 space. Since I can't have more than 4 elements, then it's deleted
Next the C move one space to the right
Next the B move one space to the right
Next the A don't move cause it has 0.
Elements never cross each others.
The change has to be in place.
tks for your help
Seby

答案1

得分: 1

我已经为您创建了一个函数,可以实现您期望的结果。

function shiftArray(arr, shiftAmounts) {
  for (let i = 3; i >= 0; i--) {
    const shift = shiftAmounts[i];
    if (shift === 0) continue;

    const newIndex = i + shift;
    if (newIndex >= 4) {
      arr.splice(i, 1);
      continue;
    }

    arr[newIndex] = arr[i];
    arr[i] = "Empty";
  }
  return arr;
}

const arr = ["A", "B", "C", "D"];
const shiftAmounts = [0, 1, 1, 3];
const result = shiftArray(arr, shiftAmounts);
console.log(result);

请注意,这段代码的功能是将数组中的元素按照给定的偏移量向右移动,并在移动后用"Empty"填充空位。

英文:

i have made a function for you which can achieve you desired outcome

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

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

function shiftArray(arr, shiftAmounts) {
  for (let i = 3; i &gt;= 0; i--) {
    const shift = shiftAmounts[i];
    if (shift === 0) continue;

    const newIndex = i + shift;
    if (newIndex &gt;= 4) {
      arr.splice(i, 1);
      continue;
    }

    arr[newIndex] = arr[i];
    arr[i] = &quot;Empty&quot;;
  }
  return arr;
}

const arr = [&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;];
const shiftAmounts = [0, 1, 1, 3];
const result = shiftArray(arr, shiftAmounts);
console.log(result);

<!-- end snippet -->

答案2

得分: 1

将数组的每个元素映射到一个包含元素和新位置的列表。

然后,使用 Object.fromEntries 将该数组视为键值对的列表,并从中创建一个对象,然后使用 Array.from 将其转换为数组。请注意,Array.from 将自动排除位置大于指定数组长度的元素。

Array.from 的第二个参数是一个箭头函数,用于替换缺失的数组元素为字符串 "Empty"。

const arr = ['A', 'B', 'C', 'D']
const shiftAmounts = [0, 1, 1, 3]

const r = Array.from({
  length: arr.length,
  ...Object.fromEntries(arr.map((e,i) => [i+shiftAmounts[i], e]))
}, e => e ?? 'Empty')

console.log(r)
英文:

Map each element of the array to a list of elements and new positions.

Then, use Object.fromEntries to treat that array as a list of key-value pairs and create an object from them, and use Array.from to convert that into an array. Note that Array.from will automatically exclude elements in positions greater than the specified array length.

The second parameter to Array.from is an arrow function that replaces missing array elements with the string 'Empty'.

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

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

const arr = [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;]
const shiftAmounts = [0, 1, 1, 3]

const r = Array.from({
  length: arr.length,
  ...Object.fromEntries(arr.map((e,i) =&gt; [i+shiftAmounts[i], e]))
}, e =&gt; e ?? &#39;Empty&#39;)

console.log(r)

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月20日 05:58:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76059110.html
匿名

发表评论

匿名网友

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

确定