Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/420.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在数组中附加特定值?_Javascript_Jquery_Arrays_Angularjs - Fatal编程技术网

Javascript 如何在数组中附加特定值?

Javascript 如何在数组中附加特定值?,javascript,jquery,arrays,angularjs,Javascript,Jquery,Arrays,Angularjs,这是我的指令。我想在现有数组中附加特定于数组的数组值 localStorage.getItem('albumcomments')=“[{“id”:1,“photocomment”:“sdfsdfs”}]” 我的预期产出是 [{"id":1,"photocomment":"test","like":"1","unlike":"1"} 现在输出是 [{"like":"["},{"like":"{"},{"like":"\""},{"like":"i"},{"like":"d"},{"like":"

这是我的指令。我想在现有数组中附加特定于数组的数组值

localStorage.getItem('albumcomments')=“[{“id”:1,“photocomment”:“sdfsdfs”}]”

我的预期产出是

[{"id":1,"photocomment":"test","like":"1","unlike":"1"}
现在输出是

[{"like":"["},{"like":"{"},{"like":"\""},{"like":"i"},{"like":"d"},{"like":"\""},{"like":":"},{"like":"1"},{"like":","},{"like":"\""},{"like":"p"},{"like":"h"},{"like":"o"},{"like":"t"},{"like":"o"},{"like":"c"},{"like":"o"},{"like":"m"},{"like":"m"},{"like":"e"},{"like":"n"},{"like":"t"},{"like":"\""},{"like":":"},{"like":"\""},{"like":"s"},{"like":"d"},{"like":"f"},{"like":"s"},{"like":"d"},{"like":"f"},{"like":"s"},{"like":"\""},{"like":"}"},{"like":"]"}]

试试看

我想你应该创建一个新的数组,然后你可以在迭代时推送这些东西:

var data=[{“id”:1,“光通信”:“test”},{“id”:1,“光通信”:“test1”}];
var newData=[];
$。每个(数据、功能(i、项){
push({id:item.id,photocomment:item.photocomment,like:i,inspect:i});
});
console.log(newData)

试试这个,希望能有所帮助

    angular.module('albumlikeFeature',[]).directive('albumlikeFeature', ['$filter', '$route', function ($filter, $route) {

    return {
        restrict: 'E',
        scope: {},
        templateUrl: 'app/components/album/albumlikefeature.html',
        link : function(scope, element, attrs) {

    		scope.likeCount = 0;
    		scope.unlikeCount = 0;
    		
    		
            scope.likeClick = function (dataid) {
    		scope.likeCount += 1;          
    		
    		if(JSON.parse(localStorage.getItem('albumcomments'))){
    				
    				var newDataLike = [];
    				 angular.forEach(JSON.parse(localStorage.getItem('albumcomments')), function(likeCount, item){				
    					newDataLike.push({id:likeCount.id, photocomment:likeCount.photocomment, like:scope.likeCount,unlike:scope.unlikeCount});
    				});
    				
    				localStorage.setItem('albumcomments',JSON.stringify(newDataLike));
    		}
    		
          };
    	  
    	   scope.unlikeClick = function (dataid) {
    	   scope.unlikeCount += 1;          
    		
    		if(JSON.parse(localStorage.getItem('albumcomments'))){
    				
    				var newDataUnlike = [];
    				 angular.forEach(JSON.parse(localStorage.getItem('albumcomments')), function(i, item){
    					newDataUnlike.push({id:i.id, photocomment:i.photocomment,like:scope.likeCount,unlike:scope.unlikeCount});
    				});

    				console.log(newDataUnlike);
    				localStorage.setItem('albumcomments',JSON.stringify(newDataUnlike));
    		}
    		
          };
    	  
        }           
     }
    }]);

迭代数组并相应地改变对象?你不必把任何东西推到数组中…检查我更新的问题@FelixKlingBetter。为什么不能用“你”来代替“你”?这不是推特,没有真正的字符限制。事实上,这是错误的。为什么要将对象传递给jquery?@user2598136如果你不想使用jquery-我会重新编写纯js(但在我的exp-jquery中更容易),那不是我的意思。我的意思是,
$(这个)
是错误的。如果你运行代码,你会注意到原来的数组没有改变,因为你只是在修改jQuery object.im大多数ruby和scala开发人员,有时我会再次混淆js闭包等@FelixKlinginteresting,但是什么不能修改旧数组[object]在这种情况下,使用
.map
会更优雅一点。它运行了多次@jai现在我的输出是
[{like],{like],{like],{like],{like],{like],{like],{like],{like],{like],{like],{like],{like],{like],{like],{like],{like],{like],{like]:{like],{like},{like},{like},like},like},like},{like},{like},{“:“o”}]
如何修复此问题?
localStorage.getItem('albumcomments') = JSON.parse('[{"id":1,"photocomment":"sdfsdfs"}]');

angular.module('albumlikeFeature',[]).directive('albumlikeFeature', ['$filter', '$route', function ($filter, $route) {

return {
  restrict: 'E',
  scope: {},
  templateUrl: 'app/components/album/albumlikefeature.html',
  link : function(scope, element, attrs) {

  scope.likeCount = 0;
  scope.unlikeCount = 0;


  scope.likeClick = function (dataid) {
  scope.likeCount += 1;          

  if(localStorage.getItem('albumcomments').length > 0){

    var newDataLike = [];

    $.each(localStorage.getItem('albumcomments'), function(value, key){
      $(this) = $this;

      console.log('test');

      newDataLike.push( {id: $this.id, photocomment: $this.photocomment, like: scope.likeCount} );
    });

    console.log(newDataLike);
    localStorage.setItem('albumcomments',JSON.stringify(newDataLike));

  }

};
    angular.module('albumlikeFeature',[]).directive('albumlikeFeature', ['$filter', '$route', function ($filter, $route) {

    return {
        restrict: 'E',
        scope: {},
        templateUrl: 'app/components/album/albumlikefeature.html',
        link : function(scope, element, attrs) {

    		scope.likeCount = 0;
    		scope.unlikeCount = 0;
    		
    		
            scope.likeClick = function (dataid) {
    		scope.likeCount += 1;          
    		
    		if(JSON.parse(localStorage.getItem('albumcomments'))){
    				
    				var newDataLike = [];
    				 angular.forEach(JSON.parse(localStorage.getItem('albumcomments')), function(likeCount, item){				
    					newDataLike.push({id:likeCount.id, photocomment:likeCount.photocomment, like:scope.likeCount,unlike:scope.unlikeCount});
    				});
    				
    				localStorage.setItem('albumcomments',JSON.stringify(newDataLike));
    		}
    		
          };
    	  
    	   scope.unlikeClick = function (dataid) {
    	   scope.unlikeCount += 1;          
    		
    		if(JSON.parse(localStorage.getItem('albumcomments'))){
    				
    				var newDataUnlike = [];
    				 angular.forEach(JSON.parse(localStorage.getItem('albumcomments')), function(i, item){
    					newDataUnlike.push({id:i.id, photocomment:i.photocomment,like:scope.likeCount,unlike:scope.unlikeCount});
    				});

    				console.log(newDataUnlike);
    				localStorage.setItem('albumcomments',JSON.stringify(newDataUnlike));
    		}
    		
          };
    	  
        }           
     }
    }]);