首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何解决负索引移位的数组交换问题?(Javascript)

问如何解决负索引移位的数组交换问题?(Javascript)
EN

Stack Overflow用户
提问于 2021-01-08 10:32:21
回答 2查看 129关注 0票数 0

我正在学习JS和做运动。我偶然发现了这个练习,为了清楚起见,我将张贴这篇文章:

你是个魔术师你拿着一副牌。为了正确地执行你的魔术,你需要能够将一张卡片从一个位置移动到另一个位置。也就是说,你需要重新安排甲板。自然地,你想要能够在两个方向移动卡片,并且能够说“从甲板的顶部”或“从甲板的底部”。

创建一个函数排列,该函数需要:

项目数组(长度n)、从位置(整数、从

它返回一个新数组,将项从位置移到位置,移动到:

代码语言:javascript
复制
const before = ['❤ A', '❤ 9', '❤ 3', '❤ 6', '♣ A']
const magics = arrange(before, 1, -2)
magics
// => ['❤ A', '❤ 3', '❤ 6', '❤ 9', '♣ A']
//                          ^--- has moved from position 1 to -2 (from the right side)
Create a second function rearrange that does the same thing, but mutates the original input array:

const before = ['❤ A', '❤ 3', '❤ 6', '❤ 9', '♣ A']
const magics = rearrange(before, 4, 0)
magics
// => ['♣ A', '❤ A', '❤ 3', '❤ 6', '❤ 9']
//      ^--- has moved from position 4 to 0
magics === before
// => true

因此,我对排列函数的解决方案如下所示,使用了最简单的方法,即temp变量:

代码语言:javascript
复制
function arrange(array, from, to) {

         var temp = array[from];
         array[from] =array[to];
array[to] = temp;

 return array
  }

var cards =['♠', '♣', '♥', '♦', '✨']
arrange(cards, 3,0)

我知道这只会改变正指数的位置,例如,排列(卡片,4,-1)将返回未定义的位置,而不是期望的位置。

有办法让这件事成功吗?我尝试过剪接法,但似乎也没有用。非常感谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-01-08 11:04:50

请注意,它是在请您一个新的数组。

​

代码语言:javascript
复制
function arrange(array, from, to) {

  const count = array.length;
  from = (from<0)?count+from:from;
  to = (to<0)?count+to:to;

  const newArr = array.filter((_,i)=> (i!==from));
  newArr.splice(to,0,array[from]);
  return newArr;
  }

var cards =['A', 'B', 'C', 'D', 'E']
const newCards = arrange(cards, -1,0)
console.log("before",cards,"after",newCards);

​

使用筛选器,您将得到一个新数组(当然还有其他几个选项),在同一步骤中,我在原始位置(from)中删除项,然后使用剪接将其添加到其新位置(to)中。

为了能够使用过滤器,我需要将负指数转换为正指数。一个有两个拼接的解决方案可以完成,但是您需要事先复制数组。

票数 1
EN

Stack Overflow用户

发布于 2021-01-08 11:17:42

如果end为负,则索引为the length of the array + end,例如,['❤ A', '❤ 9', '❤ 3', '❤ 6', '♣ A']长度为5。如果end为-2,则该索引为5 + -2、3。

​

代码语言:javascript
复制
function getIndex(array, position)
{
    return position < 0 ? array.length + position : position;
}

const before = ['❤ A', '❤ 9', '❤ 3', '❤ 6', '♣ A'];
console.log(getIndex(before, 1));
console.log(getIndex(before, -2));

​

现在,问题不是交换元素from和to,而是转移它们。

例如,[ '❤ 9', '❤ 3', '❤ 6' ],您删除第一个[ '❤ 3', '❤ 6' ]并将其推到末尾:[ '❤ 3', '❤ 6', '❤ 9' ]

班()方法移除第一个元素并返回它。

​

代码语言:javascript
复制
function shiftArray(array)
{
    const elem = array.shift(); // remove the first element
    array.push(elem); // push it to the end
    
    // changes are made in place, nothing to return
}

const arr = [ '❤ 9', '❤ 3', '❤ 6' ];

shiftArray(arr);
console.log(arr);

​

现在,您需要将该转换应用到数组的一个片段中。

切片(从,到)方法正是这样做的。

它接受负索引,因此甚至不需要前面编写的getIndex()函数:

​

代码语言:javascript
复制
function shiftArray(array)
{
    const elem = array.shift(); // remove the first element
    array.push(elem); // push it to the end

    // changes are made in place, nothing to return
}

function arrange(array, from, to)
{
    // get the first part of the array
    const firstpart = array.slice(0, from);
    // get the part to do the "magic"
    const partToShift = array.slice(from, to + 1);
    // get the last part
    const lastPart = array.slice(to + 1);
    
    shiftArray(partToShift);
    
    // return a new array, combining all parts
    return firstpart.concat(partToShift, lastPart);
}

let before = ['❤ A', '❤ 9', '❤ 3', '❤ 6', '♣ A'];
let magics = arrange(before, 1, -2);

console.log("original");
console.log(before);
console.log("modified");
console.log(magics);

// some edge cases
magics = arrange(before, 0, -2);
console.log(magics);

magics = arrange(before, 1, 4);
console.log(magics);

magics = arrange(before, 1, 1);
console.log(magics);

​

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65627378

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档