简体   繁体   English

在ES5 Javascript中,如何在不使用concat的情况下将项目添加到数组并立即返回新数组?

[英]In ES5 Javascript, how do I add an item to an array and return the new array immediately, without using concat?

I often find myself in the situation where I want to, in a single (atomic) operation, add an item to an array and return that new array. 我经常遇到一种情况,我想通过一个(原子)操作将一个项目添加到数组中并返回该新数组。

['a', 'b'].push('c');

won't work as it returns the new length. 将无法使用,因为它会返回新的长度。

I know the following code works 我知道以下代码有效

['a', 'b'].concat(['c']);

But I find it ugly code (combining two arrays just to add a single item to the end of the first array). 但是我发现它很丑陋(将两个数组组合在一起,只是将一个项目添加到第一个数组的末尾)。

I can't use Array.splice() as it modifies the original array (and returns the removed items). 我不能使用Array.splice()因为它会修改原始数组(并返回删除的项)。 Array.slice() does return a shallow copy but you can't add new items. Array.slice()确实返回浅表副本,但是您不能添加新项目。

ES6 ES6

I'm aware that in es6 you can use 我知道您可以在es6中使用

[...['a', 'b'], 'c']

But I'm looking for an es5 solution 但我正在寻找es5解决方案

Lodash 洛达

I'm okay in using lodash 我可以使用lodash

Just to be clear 只是要清楚

I'm aware that this can be achieved in several different ways (like the Array.concat() method above), but I'm looking for an intuitive simple piece of code, which doesn't "misuses" other operators 我知道这可以通过几种不同的方式来实现(例如上面的Array.concat()方法),但是我正在寻找一种直观的简单代码,不会“滥用”其他运算符

I know the following code works ['a', 'b'].concat(['c']); 我知道以下代码有效['a', 'b'].concat(['c']); But I find it ugly code ( combining two arrays just to add a single item to the end of the first array). 但是我发现它很丑陋( 将两个数组组合在一起只是在第一个数组的末尾添加一个项目)。

The concat() method can be given a single (or multiple) values without the need of encapsulating the value(s) in an array first, for example: 可以给concat()方法一个(或多个)值,而无需先将值封装在数组中,例如:

['a', 'b'].concat('c');   // instead of .concat(['c']);

From MDN (my emphasis): 来自MDN (我的重点):

Arrays and/or values to concatenate into a new array. 数组和/或以连接成一个新数组。

Besides from that there are limited options without using extension and existing methods. 除此之外,没有使用扩展名和现有方法的选项有限。

Example on how to extend the Array (this will return current array though): 有关如何扩展数组的示例(尽管这将返回当前数组):

 Array.prototype.append = function(item) { this.push(item); return this }; var a = [1, 2, 3]; console.log(a.append(4)) 

Optionally create a simple function as @torazaburo suggests, which can take array and item as argument: (可选)按照@torazaburo建议创建一个简单函数,该函数可以将array和item作为参数:

function append(arr, item) {
  arr.push(item);
  return arr;
}

or using concat() : 或使用concat()

function append(arr, item) {
  return arr.concat(item)
}

I can offer two methods for Array.prototype.insert() which will allow you insert single or multiple elements starting from any index within the array. 我可以为Array.prototype.insert()提供两种方法,这些方法使您可以从数组中的任何索引开始插入单个或多个元素。

1) mutates the array it's called upon and returns it 1)修改其调用的数组并返回它

 Array.prototype.insert = function(i,...rest){ this.splice(i,0,...rest) return this } var a = [3,4,8,9]; console.log(JSON.stringify(a.insert(2,5,6,7))); 

ES5 compliant version of the above snippet. 以上代码段的ES5兼容版本。

Array.prototype.insert = function(i){
  this.splice.apply(this,[i,0].concat(Array.prototype.slice.call(arguments,1)));
  return this;
};

2) Not mutates the array it's called upon and returns a new one 2)不改变其调用的数组并返回一个新的数组

 Array.prototype.insert = function(i,...rest){ return this.slice(0,i).concat(rest,this.slice(i)); } var a = [3,4,8,9], b = a.insert(2,5,6,7); console.log(JSON.stringify(a)); console.log(JSON.stringify(b)); 

ES5 compliant version of the above snippet. 以上代码段的ES5兼容版本。

Array.prototype.insert = function(i){
  return this.slice(0,i).concat(Array.prototype.slice.call(arguments,1),this.slice(i));
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 ES5 解构数组? - How can I destructure an array using ES5? JavaScript(ES5)中的参数数组 - Arguments Array in JavaScript (ES5) 如何使用 ES6 在 javascript 中删除或添加项目到锯齿状数组? - How to remove or add an item to a jagged array in javascript using ES6? Javascript ES5:如何在不使用 Lodash 或 Underscore 的情况下按键分组并从键值对象的平面数组创建子对象 - Javascript ES5: How to Group By a key and create Sub objects from a flat array of key-value objects without using Lodash or Underscore 使用 ES5/Lodash,如何在不比较整个对象的情况下查找/删除一个数组中不在另一个数组中的对象? - Using ES5/Lodash, how can I find/remove objects in one array that aren't in another without comparing the whole object? 选择使用javascript es5更新或推送到数组 - Choose to update or push to array using javascript es5 如何使用 javascript 中的提示向数组添加新元素? - How do I add new elements to an array using a promt in javascript? 如何在ES5中使用多个值在数组中查找对象的索引? - How to find index of object in array using multiple values with ES5? 如何使用 JSON.stringify 输出一个 javascript 数组,其中每个数组项都是一个新行 - How do I output a javascript array where each array item is a new line using JSON.stringify 我将如何遍历 Javascript 中的数组,添加一个单词,然后返回一个数组(不使用 map)? - How would I loop through an array in Javascript, add a word, then return an array (without using map)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM