te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - How can I "filter" JSON for unique key namevalue pairs? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I "filter" JSON for unique key namevalue pairs? - Stack Overflow

programmeradmin4浏览0评论

I've got some JSON data that is giving me a list of languages with info like lat/lng, etc. It also contains a group value that I'm using for icons--and I want to build a legend with it. The JSON looks something like this:

{"markers":[
  {"language":"Hungarian","group":"a", "value":"yes"},
  {"language":"English", "group":"a", "value":"yes"},
  {"language":"Ewe", "group":"b", "value":"no"},
  {"language":"French", "group":"c", "value":"NA"}
]}

And I want to "filter" it to end up like this:

{"markers":[
 {"group":"a", "value":"yes"},
 {"group":"b", "value":"no"},
 {"group":"c", "value":"NA"}
]}

Right now I've got this, using jQuery to create my legend..but of course it's pulling in all values:

$.getJSON("http://127.0.0.1:8000/dbMap/map.json", function(json){
    $.each(json.markers, function(i, language){
        $('<p>').html('<img src="http://mysite/group' + language.group + '.png\" />' + language.value).appendTo('#legend-contents');
    });
});

How can I only grab the unique name/value pairs in the entire JSON object, for a given pair?

I've got some JSON data that is giving me a list of languages with info like lat/lng, etc. It also contains a group value that I'm using for icons--and I want to build a legend with it. The JSON looks something like this:

{"markers":[
  {"language":"Hungarian","group":"a", "value":"yes"},
  {"language":"English", "group":"a", "value":"yes"},
  {"language":"Ewe", "group":"b", "value":"no"},
  {"language":"French", "group":"c", "value":"NA"}
]}

And I want to "filter" it to end up like this:

{"markers":[
 {"group":"a", "value":"yes"},
 {"group":"b", "value":"no"},
 {"group":"c", "value":"NA"}
]}

Right now I've got this, using jQuery to create my legend..but of course it's pulling in all values:

$.getJSON("http://127.0.0.1:8000/dbMap/map.json", function(json){
    $.each(json.markers, function(i, language){
        $('<p>').html('<img src="http://mysite/group' + language.group + '.png\" />' + language.value).appendTo('#legend-contents');
    });
});

How can I only grab the unique name/value pairs in the entire JSON object, for a given pair?

Share Improve this question edited Oct 12, 2015 at 19:40 Patrick Murphy 2,32915 silver badges17 bronze badges asked Mar 3, 2009 at 2:56 miketaylrmiketaylr 1,9763 gold badges15 silver badges14 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 8

I'd transform the array of markers to a key value pair and then loop that objects properties.

var markers = [{"language":"Hungarian","group":"a", "value":"yes"}, 
  {"language":"English", "group":"a", "value":"yes"}, 
  {"language":"Ewe", "group":"b", "value":"no"},
  {"language":"French", "group":"c", "value":"NA"}];

var uniqueGroups = {};
$.each(markers, function() {
  uniqueGroups[this.group] = this.value;
});

then

$.each(uniqueGroups, function(g) {
  $('<p>').html('<img src="http://mysite/group' + g + '.png\" />' + this).appendTo('#legend-contents');
});

or

for(var g in uniqueGroups)
{
  $('<p>').html('<img src="http://mysite/group' + g + '.png\" />' + uniqueGroups[g]).appendTo('#legend-contents');
}

This code sample overwrites the unique value with the last value in the loop. If you want to use the first value instead you will have to perform some conditional check to see if the key exists.

How about something more generic?

function getDistinct(o, attr)
{
 var answer = {};
 $.each(o, function(index, record) {
   answer[index[attr]] = answer[index[attr]] || [];
   answer[index[attr]].push(record);
 });

 return answer;    //return an object that has an entry for each unique value of attr in o as key, values will be an array of all the records that had this particular attr.
}

Not only such a function would return all the distinct values you specify but it will also group them if you need to access them.

In your sample you would use:

$.each(getDistinct(markers, "group"), function(groupName, recordArray)
{ var firstRecord = recordArray[0];
        $('<p>').html('<img src="http://mysite/group' + groupName+ '.png\" />' + firstRecord.value).appendTo('#legend-contents');

}

See this- Best way to query back unique attribute values in a javascript array of objects?

You just need a variation that checks for 2 values rather than 1.

var markers  = _.uniq( _.collect( markers , function( x ){
                        return JSON.stringify( x ); 
                    })); 

reference

发布评论

评论列表(0)

  1. 暂无评论