短的方式来代替数组含量 [英] Short way to replace content of an array
问题描述
有没有更换一个数组的内容,并保持对它的引用任何方便的方式?我不想更换阵列是这样的:
VAR ARR1 = [1,2,3];
VAR referenceToArr1 = ARR1;
VAR ARR2 = [4,5,6];ARR1 = ARR2;
//日志:[4,5,6]假
的console.log(ARR1,ARR1 === referenceToArr1);
//日志[1,2,3]
的console.log(referenceToArr1);
本办法 ARR1
的 ARR2
的内容,但我失去的参考referenceToArr1
,因为它仍然指向的原始的 ARR1
。
通过这种方式,我不松参考:
VAR ARR1 = [1,2,3];
VAR referenceToArr1 = ARR1;
VAR ARR2 = [4,5,6];arr1.length = 0;
对于(VAR I = 0; I< arr2.length;我++){
arr1.push(ARR2 [I]);
}
//日志:[4,5,6]真
的console.log(ARR1,ARR1 === referenceToArr1);
//日志:[4,5,6]
的console.log(referenceToArr1)
在这里的缺点是,我得空 arr1.length = 0
, ARR2
并用手把它推到 ARR1
。
我的问题是:
- 当然,我可以写这样的帮手,但什么是最有效的方法是什么?
- 是否有短,香草JavaScript的方式,这样做(也许是单行?)
- 我还使用underscore.js,但还没有找到这种问题的方法。有没有办法用underscore.js做到这一点?
背景:
我有一个服务的AngularJS应用。这里面的服务,我有一个数组,以保持从服务器加载的所有元素。从服务中的数据得到的绑定到一个控制器和视图中使用。当从服务中获得的变化数据(例如,重新读取发生),我想自动更新我的控制器VAR和它绑定视图。
这里是一个Plunker: http://plnkr.co/edit / 8yYahwDO6pAuwl6lcpAS?p = preVIEW
VAR应用= angular.module('plunker',[]);app.controller('MainCtrl',函数($范围,为myService){
$ scope.name ='世界';
myService.fetchData()。然后(功能(数据){
的console.log(数据)
$ scope.data =数据;
})
});app.service('为myService',函数($超时,$ Q){
VAR ARR;
VAR推迟; VAR loadData =功能(){
//这里从服务器附带的一些数据
变种serverData = [1,2,3];
ARR = serverData;
deferred.resolve(ARR); //模拟数据的重取
$超时(函数(){
VAR newServerData = [4,5,6];
//这是行不通的,因为MainCtrl失去它的参考
// ARR = newServerData;
},1000); $超时(函数(){
VAR newServerData = [7,8,9];
arr.length = 0;
[] .push.apply(ARR,newServerData);
},2000); $超时(函数(){
VAR newServerData = [10,11,12]。
[] .splice.apply(ARR,[0,arr.length] .concat(newServerData));
},3000);
} 返回{
fetchData:功能(){
递延= $ q.defer(); loadData(); 返回deferred.promise;
}
}
})
和视图:
<!DOCTYPE HTML>
< HTML NG-应用=plunker> < HEAD>
<间的charset =UTF-8/>
<标题> AngularJS Plunker< /标题>
<脚本>的document.write('<基本href =+ document.location +'/>');< / SCRIPT>
<链接rel =stylesheet属性HREF =style.css文件/>
&所述;脚本数据需要=angular.js@1.2.xSRC =的https://$c$c.angularjs.org/1.2.16/angular.js数据semver =1.2.16&GT ;< / SCRIPT>
&所述; SCRIPT SRC =app.js>&下; /脚本>
< /头> <机身NG控制器=MainCtrl>
< UL>
<李NG重复=D数据> {{D}}< /李>
< / UL>
3每隔重新提取第二
< /身体GT;< / HTML>
这个是什么:
// 1.重置阵列,同时保持其基准
arr1.length = 0;
// 2.填充与来自第二项的第一阵列
[] .push.apply(ARR1,ARR2);
请参阅:
Is there any convenient way to replace the content of an array, AND keep a reference to it? I don't want to replace the array like this:
var arr1 = [1,2,3];
var referenceToArr1 = arr1;
var arr2 = [4,5,6];
arr1 = arr2;
// logs: [4,5,6] false
console.log(arr1, arr1===referenceToArr1);
// logs [1,2,3]
console.log(referenceToArr1);
This way arr1
has the content of arr2
, but I loose the reference in referenceToArr1
, because it still points to the original arr1
.
With this way, I don't loose the reference:
var arr1 = [1,2,3];
var referenceToArr1 = arr1;
var arr2 = [4,5,6];
arr1.length = 0;
for (var i = 0; i < arr2.length; i++) {
arr1.push(arr2[i]);
}
// logs: [4,5,6] true
console.log(arr1, arr1===referenceToArr1);
// logs: [4,5,6]
console.log(referenceToArr1)
The drawback here is, that I have to empty arr1.length = 0
, iterate over every element of arr2
and push it to arr1
by hand.
My questions are:
- Sure, I could write a helper for this, but what is the most efficient way?
- Is there a short, vanilla javascript way, to do this (maybe a one liner?)
- I'm also using underscore.js but haven't found a method for this kind of problem. Is there a way to do this with underscore.js?
Background:
I have an AngularJS app with a service. Inside this service, I have an array to keep all the elements loaded from the server. The data from the service get's bind to a controller and is used in a view. When the data from the service get's change (e.g. a refetch happens), I want to auto update my controllers vars and the view it is bind to.
Here is a Plunker: http://plnkr.co/edit/8yYahwDO6pAuwl6lcpAS?p=preview
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope, myService) {
$scope.name = 'World';
myService.fetchData().then(function(data) {
console.log(data)
$scope.data = data;
})
});
app.service('myService', function($timeout, $q) {
var arr;
var deferred;
var loadData = function() {
// here comes some data from the server
var serverData = [1,2,3];
arr = serverData;
deferred.resolve(arr);
// simulate a refetch of the data
$timeout(function() {
var newServerData = [4,5,6];
// this won't work, because MainCtrl looses it's reference
// arr = newServerData;
}, 1000);
$timeout(function() {
var newServerData = [7,8,9];
arr.length = 0;
[].push.apply(arr, newServerData);
}, 2000);
$timeout(function() {
var newServerData = [10,11,12];
[].splice.apply(arr, [0, arr.length].concat(newServerData));
}, 3000);
}
return {
fetchData: function() {
deferred = $q.defer();
loadData();
return deferred.promise;
}
}
})
And the view:
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<script data-require="angular.js@1.2.x" src="https://code.angularjs.org/1.2.16/angular.js" data-semver="1.2.16"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
<ul>
<li ng-repeat="d in data">{{d}}</li>
</ul>
3 refetches every second
</body>
</html>
What about this:
// 1. reset the array while keeping its reference
arr1.length = 0;
// 2. fill the first array with items from the second
[].push.apply(arr1, arr2);
See:
- How to empty an array in JavaScript?
- .push() multiple objects into JavaScript array returns 'undefined'
这篇关于短的方式来代替数组含量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!