【问题标题】:how to append the specific values in array?如何在数组中附加特定值?
【发布时间】:2015-04-04 13:00:58
【问题描述】:

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

localStorage.getItem('albumcmets') = "[{"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));

        }

      };

我的预期输出是

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

【问题讨论】:

  • 迭代数组并相应地改变对象?您不必向数组推送任何内容...
  • 检查我更新的问题@FelixKling

标签: javascript jquery arrays angularjs


【解决方案1】:

试试这个希望它会有所帮助

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

【讨论】:

    【解决方案2】:

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

    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>

    【讨论】:

    • 很有趣,但是什么不修改旧数组[Object]?
    • 这种情况下使用.map会更优雅一点。
    • 它运行多次@jai 现在我的输出是[{"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"}]
    • 我该如何解决这个问题?
    【解决方案3】:
    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));
    
      }
    
    };
    

    试试看

    【讨论】:

    • 更好。有什么理由不能写“你”而不是“你”?这不是 Twitter,实际上没有字符限制。
    • 其实是错的。为什么要将对象传递给 jquery?
    • @user2598136 如果你不想使用 jQuery - 我重写为纯 js(但在我的 exp - jQuery rly 更容易)
    • 我不是这个意思。我的意思是$(this) 是错误的。如果你运行代码,你会注意到原始数组没有改变,因为你只是在改变 jQuery 对象。
    • 我是大多数 ruby​​ 和 scala 开发人员,有时我对 js 关闭等感到困惑)再次@FelixKling
    猜你喜欢
    • 2012-03-14
    • 2019-05-24
    • 2021-06-07
    • 2015-06-17
    • 2017-10-13
    • 2011-11-15
    • 2015-12-20
    • 2013-10-25
    • 2021-05-03
    相关资源
    最近更新 更多