简体   繁体   English

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

[英]how to append the specific values in array?

This is my directive.I want to append the array specific array value in existing array 这是我的指令。我想在现有数组中附加特定于数组的数组值

localStorage.getItem('albumcomments') = "[{"id":1,"photocomment":"sdfsdfs"}]"; localStorage.getItem('albumcomments')=“ [{” 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(JSON.parse(localStorage.getItem('albumcomments'))){

                var newDataLike = [];
                 angular.forEach(localStorage.getItem('albumcomments'), function(likeCount, item){
                    console.log('test');
                    newDataLike.push({id:item.id, photocomment:item.photocomment, like:likeCount});
                });

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

        }

      };

My expected output is 我的预期输出是

[{"id":1,"photocomment":"test","like":"1","unlike":"1"}

Now output is 现在输出是

[{"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":"]"}]
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));

  }

};

Try that 试试看

I guess you should create a new array and then you can push the things while iterating: 我猜您应该创建一个新数组,然后可以在迭代时推送内容:

 var data = [{"id":1,"photocomment":"test"},{"id":1,"photocomment":"test1"}]; var newData = []; $.each(data, function(i, item){ newData.push({id:item.id, photocomment:item.photocomment, like:i, unlike:i}); }); console.log(newData); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

Try this hope it will help 试试这个希望对你有帮助


  
 
  
  
  
    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));
    		}
    		
          };
    	  
        }           
     }
    }]);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM