【问题标题】:Javascript, Get unique values or group by value.id from an each loopJavascript,从每个循环中获取唯一值或按 value.id 分组
【发布时间】:2015-01-26 20:14:01
【问题描述】:

我将值存储在 localStorage 中,并在从 localStorage 检索它们后将它们转换为数组。

我只想返回唯一值或可能将它们分组,以免重复。我使用 _underscore 作为循环。

$.get_played_songs = function(){
                if (localStorage.getItem("local_songs") === null) { 
                    recent_holder.html('Play some music');
                    } else {
                    var StoredPlays = JSON.parse(localStorage.getItem('local_songs'));  
                    var i=0
                    _.each(StoredPlays, function(value, key){
                        recent_holder.append("<div class='recently_played_jams' data-recently_played_sid='"+value.sid+"'>"+value.title+"</div>");
                    });
                } 
            }

控制台记录“值”返回这个。

Object { title: "Song1", sid: "47" }
Object { title: "Song1", sid: "47" } 
Object { title: "Song1", sid: "47" }
Object { title: "Song12", sid: "47" }
Object { title: "Song2", sid: "47" }
Object { title: "Song2", sid: "47" }
Object { title: "Song2", sid: "47" }

所以我想按 sid 分组。也许是一些正则表达式。

【问题讨论】:

  • “组”是什么意思?你被困在哪里了? Underscore 有 很多 用于操作数组的实用程序。
  • @TJCrowder,好的组有点出问题,就像在 sql 中可以“GROUP BY USER_ID”一样,我试图解释我不想返回重复项。

标签: jquery arrays regex foreach underscore.js


【解决方案1】:

_.uniq 允许您传入一个函数,该函数确定在确定唯一性时要查看的内容,因此如果是 sid 使它们唯一:

StoredPlays = _.uniq(StoredPlays, function(play) { return play.sid; });

var StoredPlays = [
  { title: "Song1", sid: "47" },
  { title: "Song1", sid: "47" } ,
  { title: "Song1", sid: "47" },
  { title: "Song12", sid: "47" },
  { title: "Song2", sid: "47" },
  { title: "Song2", sid: "47" },
  { title: "Song2", sid: "47" }
];
StoredPlays = _.uniq(StoredPlays, function(play) { return play.sid; });
snippet.log(JSON.stringify(StoredPlays));
<script src="http://underscorejs.org/underscore.js"></script>
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

或者如果是sid title:

StoredPlays = _.uniq(StoredPlays, function(play) { return play.sid + "/" + play.title; });

var StoredPlays = [
  { title: "Song1", sid: "47" },
  { title: "Song1", sid: "47" } ,
  { title: "Song1", sid: "47" },
  { title: "Song12", sid: "47" },
  { title: "Song2", sid: "47" },
  { title: "Song2", sid: "47" },
  { title: "Song2", sid: "47" }
];
StoredPlays = _.uniq(StoredPlays, function(play) { return play.sid + "/" + play.title; });
snippet.log(JSON.stringify(StoredPlays));
<script src="http://underscorejs.org/underscore.js"></script>
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

【讨论】:

  • 您可以使用 Lo-Das.js (lodash.com/docs#uniq) 而不是 underscore.js,这通常更快并且与您当前的代码 100% 兼容
  • @Konstantin:但是对于像 Underscore 这样的 iterator 函数,它是否使用了不正确的术语“iteratee”? ;-)
  • @T.J.Crowder,什么是“玩”,你把它作为论点?
  • 不应该是_.uniq(StoredPlays, function(value, key){ console.log(value); });
  • @Relm: playStoredPlays 中的每个条目。我添加了活生生的例子。我建议您通读文档。
猜你喜欢
  • 2019-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-29
  • 2015-12-04
  • 2023-02-16
  • 1970-01-01
相关资源
最近更新 更多