【问题标题】:Push array of elements inside another array at a index in javascript在javascript中的索引处将元素数组推送到另一个数组中
【发布时间】:2022-01-20 22:55:25
【问题描述】:

我需要在 javascript 中的特定索引处将元素数组推送到另一个数组中,而不使用扩展运算符,也不使用另一个数组。

输入:

let firstArray = [4,5,6];
let secondArray = [1,2,3,7,8,9];

预期输出:

console.log(secondArray); //[1,2,3,4,5,6,7,8,9];

我尝试使用 spliceapply 函数,如下所示。

let firstArray = [4,5,6];
let secondArray = [1,2,3,7,8,9];
firstArray.splice(0, 0, 3, 0); 
secondArray.splice.apply(secondArray, firstArray);

这段代码给出了预期的输出,但也在更新 secondArray 之前更新了 firstArray 中的元素。

有没有更好的方法来实现预期的输出而不更新 firstArray 中的元素?

编辑 1: 我试图在没有新数组的情况下实现这一点,因为 firstArray 和 secondArray 的大小很大。因此感觉它可能会为新数组创建不需要的内存分配,以便将元素推入另一个数组。 避免传播运算符,因为它在 IE 浏览器中不受支持 编辑2: 在不使用循环条件的情况下删除。

【问题讨论】:

  • "...不使用扩展运算符或循环,也不使用另一个数组。" 为什么不呢?这些是显而易见的方法。像这样的任意限制,这个问题真的没有任何意义。
  • 不使用循环,展开语法,如何从数组firstArray获取所有数组值
  • 你的代码和this answer 链接到重复的目标 Andreas 都让你非常接近。完成后,您只需添加一点即可删除通过unshift(代码中为splice)添加到firstArray 的元素。
  • @T.J.Crowder 我不知何故忽略了 “不使用另一个数组” 部分:/
  • @ArunKumar - 感谢您的回复。请编辑问题以说明这一点。

标签: javascript arrays


【解决方案1】:

您已经说过限制的原因是:

...因为firstArraysecondArray 的大小很大。因此感觉它可能会为新数组创建不需要的内存分配以将元素推入另一个数组。

在这种情况下,您要避免在firstArray 上出现.splice(0, 0, 3, 0),因为它需要将firstArray 中的所有内容移到上面,以便为元素腾出空间。

您的内存问题并不能解释禁止使用循环(毕竟splice 使用循环)和copyWithin(也是循环)结合循环来填充新元素将是一个干净的,简单,而且不占用内存的方式来解决问题。所以:

function insert(target, source, index) {
    // Get the count of elements to copy
    let count = source.length;
    if (count) {
        // 1. First, make room by copying elements out of the way
        let targetIndex = index + source.length;
        target.length += count;
        target.copyWithin(targetIndex, index, index + count);
        // 2. Now, fill in the new elements
        while (count-- > 0) {
            target[targetIndex--] = source[count];
        }
    }
    return target;
}

现场示例:

let firstArray = [4,5,6];
let secondArray = [1,2,3,7,8,9];

function insert(target, source, index) {
    // Get the count of elements to copy
    let count = source.length;
    if (count) {
        // 1. First, make room by copying elements out of the way
        let targetIndex = index + source.length;
        target.length += count;
        target.copyWithin(targetIndex, index, index + count);
        // 2. Now, fill in the new elements
        while (count-- > 0) {
            target[targetIndex--] = source[count];
        }
    }
    return target;
}

insert(secondArray, firstArray, 3);
console.log(JSON.stringify(secondArray)); // [1,2,3,4,5,6,7,8,9];
console.log(JSON.stringify(firstArray));  // [4,5,6];

如果while 循环对您来说有点不透明,您也可以这样编写循环部分:

for (let index = 0; index < count; ++index) {
    target[targetIndex + index] = source[index];
}

现场示例:

let firstArray = [4,5,6];
let secondArray = [1,2,3,7,8,9];

function insert(target, source, index) {
    // Get the count of elements to copy
    let count = source.length;
    if (count) {
        // 1. First, make room by copying elements out of the way
        let targetIndex = index + source.length;
        target.length += count;
        target.copyWithin(targetIndex, index, index + count);
        // 2. Now, fill in the new elements
        for (let index = 0; index < count; ++index) {
            target[targetIndex + index] = source[index];
        }
    }
    return target;
}

insert(secondArray, firstArray, 3);
console.log(JSON.stringify(secondArray)); // [1,2,3,4,5,6,7,8,9];
console.log(JSON.stringify(firstArray));  // [4,5,6];

请注意,copyWithin 可能不受 IE11 支持,但很容易填充。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-03
    • 2020-07-30
    • 2021-08-27
    • 1970-01-01
    • 2023-04-07
    相关资源
    最近更新 更多