根据javascript中的索引将数组分成两部分

发布于 11-26 17:42 字数 101 浏览 1 评论 0原文

我有一个包含对象列表的数组。我想在一个特定的索引处分割这个数组,比如 4(这实际上是一个变量)。我想将分割数组的第二部分存储到另一个数组中。可能很简单,但我想不出一个好的方法来做到这一点。

I have an array with a list of objects. I want to split this array at one particular index, say 4 (this in real is a variable). I want to store the second part of the split array into another array. Might be simple, but I am unable to think of a nice way to do this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

微暖i2024-12-03 17:42:10

使用 slice,如下所示:

var ar = [1,2,3,4,5,6];
    
var p1 = ar.slice(0,4);
console.log({p1});

var p2 = ar.slice(4);
console.log({p2});

Use slice, as such:

var ar = [1,2,3,4,5,6];
    
var p1 = ar.slice(0,4);
console.log({p1});

var p2 = ar.slice(4);
console.log({p2});

秋千易2024-12-03 17:42:10

您可以使用 Array@splice将指定索引后面的所有元素从数组末尾删除并返回它们:

x = ["a", "b", "c", "d", "e", "f", "g"];
y = x.splice(3);
console.log(x); // ["a", "b", "c"]
console.log(y); // ["d", "e", "f", "g"]

You can use Array@splice to chop all elements after a specified index off the end of the array and return them:

x = ["a", "b", "c", "d", "e", "f", "g"];
y = x.splice(3);
console.log(x); // ["a", "b", "c"]
console.log(y); // ["d", "e", "f", "g"]
哭泣的笑容2024-12-03 17:42:10

我建议使用 slice() 如下

ar.slice(startIndex,length);
或者
ar.slice(startIndex);

var ar = ["a","b","c","d","e","f","g"];

var p1 = ar.slice(0,3);
var p2 = ar.slice(3);

console.log(p1);
console.log(p2);

I would recommend to use slice() like below

ar.slice(startIndex,length);
or
ar.slice(startIndex);

var ar = ["a","b","c","d","e","f","g"];

var p1 = ar.slice(0,3);
var p2 = ar.slice(3);

console.log(p1);
console.log(p2);

忘羡2024-12-03 17:42:10

使用 切片

var bigOne = [0,1,2,3,4,5,6];
var splittedOne = bigOne.slice(3 /*your Index*/);

use slice:

var bigOne = [0,1,2,3,4,5,6];
var splittedOne = bigOne.slice(3 /*your Index*/);
酒废2024-12-03 17:42:10
const splitAt = (i, arr) => {
  const clonedArray = [...arr];
  return [clonedArray.splice(0, i), clonedArray];
}

const [left, right] = splitAt(1, [1,2,3,4])

console.log(left) // [1]
console.log(right) // [2,3,4]


const [left1, right1] = splitAt(-1, [1,2,3,4])

console.log(left1) // []
console.log(right1) // [1,2,3,4]


const [left2, right2] = splitAt(5, [1,2,3,4])

console.log(left1) // [1,2,3,4]
console.log(right1) // []

与其他解决方案相比的一些好处:

  1. 您可以用一个衬垫获得结果
  2. 当分割索引下溢或溢出时,结果仍然是正确的。 slice 将无法正确运行。
  3. 它不会改变原始数组。一些基于拼接的解决方案做到了。
  4. 只有 1 个splice 操作,而不是 2 个slice 操作。但您需要进行基准测试以查看是否存在实际性能差异。

const splitAt = (i, arr) => {
  const clonedArray = [...arr];
  return [clonedArray.splice(0, i), clonedArray];
}

const [left, right] = splitAt(1, [1,2,3,4])

console.log(left) // [1]
console.log(right) // [2,3,4]


const [left1, right1] = splitAt(-1, [1,2,3,4])

console.log(left1) // []
console.log(right1) // [1,2,3,4]


const [left2, right2] = splitAt(5, [1,2,3,4])

console.log(left1) // [1,2,3,4]
console.log(right1) // []

Some benefits compared to other solutions:

  1. You can get the result with a one liner
  2. When split index is underflow or overflow, the result is still correct. slice will not behave correctly.
  3. It does not mutate the original array. Some splice based solutions did.
  4. There is only 1 splice operation, rather than 2 slice operations. But you need to benchmark to see if there is actual performance difference.
执手闯天涯2024-12-03 17:42:10

您还可以使用下划线/lodash 包装器:

var ar = [1,2,3,4,5,6];
var p1 = _.first(ar, 4);
var p2 = _.rest(ar, 4);

You can also use underscore/lodash wrapper:

var ar = [1,2,3,4,5,6];
var p1 = _.first(ar, 4);
var p2 = _.rest(ar, 4);
彩扇题诗2024-12-03 17:42:10

lodash 的一个简单函数:
<代码>
const mainArr = [1,2,3,4,5,6,7]
const [arr1, arr2] = _.chunk(mainArr, _.round(mainArr.length / 2));

Simple one function from lodash:

const mainArr = [1,2,3,4,5,6,7]
const [arr1, arr2] = _.chunk(mainArr, _.round(mainArr.length / 2));

莫多说2024-12-03 17:42:10
const splitArrayByIndex = (arr, index) => {
  if (index > 0 && index < arr.length) {
    return [arr.slice(0, index), arr.slice(-1 * (arr.length - index))]
  }
}

const input = ['a', 'x', 'c', 'r']
const output = splitArrayByIndex(input, 2)

console.log({ input, output })

const splitArrayByIndex = (arr, index) => {
  if (index > 0 && index < arr.length) {
    return [arr.slice(0, index), arr.slice(-1 * (arr.length - index))]
  }
}

const input = ['a', 'x', 'c', 'r']
const output = splitArrayByIndex(input, 2)

console.log({ input, output })

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文