[英]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.