如何从一个索引开始,但不遗漏数组中的任何项目?

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

How to start at a index but without missing any items in the array?

问题

我正在学习JS,有一个相当令人困惑的任务要完成。任务是从一个索引开始,包括数组中的剩余部分或缺失的项,并在Angular中显示它们。这是我的代码:

const arr = ["a", "b", "c", "d"];

const startIndex = 2;

let arr2 = arr.slice(startIndex, arr.length);

输出

["c", "d"]

// 忽略了a和b,不知道如何相应地显示它们

任务是从索引2开始,一直到最后一项,并包括a和b这两个在使用slice方法时被忽略的项。

英文:

I am learning JS and have a task to complete which is quite confusing. The task is to giving a index to start from and include the remainders or the the items missing in the array, as well as to display them in angular. This is what i have

const arr = ["a", "b" ,"c" ,"d"]

const startIndex = 2:

let arr2 = arr.slice(startIndex, arr.length)

Output

["c","d"]

// a and b are ignore and don't see a way to display them accordingly

The tasks is to start let's say at index 2 through the last item and include items a and b which are the items missing when using slice method

答案1

得分: 2

这是你提供的代码示例:

JS

const arr = ["a", "b", "c", "d"];
const startIndex = 2;
let arr2 = [...arr.slice(startIndex), ...arr.slice(0, startIndex)];
console.log(arr2);

Angular示例

<div *ngFor="let item of arr2">{{item}}</div>

注意:上面的翻译已经去掉了HTML编码,以便更容易阅读。

英文:

Heres an example with your code above:

JS

const arr = [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;];
const startIndex = 2;
let arr2 = [...arr.slice(startIndex), ...arr.slice(0, startIndex)];
console.log(arr2);

The ... is the spread operator, it allows an iterable (like an array expression) to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected.

Angular Example:

&lt;div *ngFor=&quot;let item of arr2&quot;&gt;{{item}}&lt;/div&gt;

答案2

得分: 0

你可以使用原始数组复制两次:

const arr = ["a", "b", "c", "d"]
const startIndex = 2;

const result = [...arr, ...arr]
  .slice(startIndex, startIndex + arr.length);

console.log(result);
英文:

You could use the original array copied twice:

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

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

const arr = [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;]
const startIndex = 2;

const result = [...arr, ...arr]
  .slice(startIndex, startIndex + arr.length);

console.log(result);

<!-- end snippet -->

答案3

得分: 0

以下是您要翻译的内容:

由于我觉得这是为了教育目的,所以想要提供一个没有内置运算符的解决方案。这里是在JavaScript中不使用任何内置运算符的替代方法。

function customSliceAndMoveItems(array, startIndex) {
  var result = [];

  for (var i = startIndex; i < array.length; i++) {
    result.push(array[i]);
  }

  for (var j = 0; j < startIndex; j++) {
    result.push(array[j]);
  }

  return result;
}

console.log(customSliceAndMoveItems(["a", "b", "c", "d"], 2))

如果考虑性能方面,您可以优化上面的代码,同时使用Array slice() 运算符和Array concat() 运算符。

function customSliceAndMoveItems(array, startIndex) {
  return array.slice(startIndex).concat(array.slice(0, startIndex));
}

console.log(customSliceAndMoveItems(["a", "b", "c", "d"], 2));

当涉及到Angular时,您可以直接在模板上进行迭代,如下所示:

<h1 *ngFor="let item of getArray1(['a', 'b', 'c', 'd'], 2)">
  {{ item }}
</h1>

请在此处找到工作的StackBlitz 链接

英文:

Since I feel this is for educational purposes thought it would be nice to have something without any inbuilt operators. Here is an alternative approach to your case without any inbuilt operators in javascript.

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

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

function customSliceAndMoveItems(array, startIndex) {
  var result = [];
  
  
  for (var i = startIndex; i &lt; array.length; i++) {
    result.push(array[i]);
  }
  
  
  for (var j = 0; j &lt; startIndex; j++) {
    result.push(array[j]);
  }
  
  return result;
}

console.log(customSliceAndMoveItems([&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;],2))

<!-- end snippet -->

In case considering the performance aspect you can optimize the above code while using the Array slice() operator and Array concat() operator.

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

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

function customSliceAndMoveItems(array, startIndex) {
  return array.slice(startIndex).concat(array.slice(0, startIndex));
}

console.log(customSliceAndMoveItems([&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;], 2));

<!-- end snippet -->

When it comes to angular you can iterate it over the template directly as follows :

&lt;h1 *ngFor=&quot;let item of getArray1([&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;], 2)&quot;&gt;
  {{ item }}
&lt;/h1&gt;

Please find the working stackblitz here.

huangapple
  • 本文由 发表于 2023年7月13日 20:14:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76679258.html
匿名

发表评论

匿名网友

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

确定