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 - lodashunderscore; compare two objects and remove duplicates - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - lodashunderscore; compare two objects and remove duplicates - Stack Overflow

programmeradmin4浏览0评论

As you can see in the image below, I have some returned json data with three objects; each contains a clients id => data.

exact_match : {104}
match_4 :  {104, 103}
match_2 :  {104, 103, 68}

How can I "trim" or remove the duplicate objects based on previous ones? something like:

exact_match : {104}
match_4 :  {103}
match_2 :  {68}

I tried _.difference but did not work (Maybe because it is for arrays not objects?):

var exact_match = data.exact_match,
    match_four_digits = _.difference(data.match_4, data.exact_match),
    match_two_digits = _.difference(data.match_2, data.exact_match, data.match_4),

Any help would be appreciated :)

Update

I need that the returned value has the same object data instead of a new array :)

As you can see in the image below, I have some returned json data with three objects; each contains a clients id => data.

exact_match : {104}
match_4 :  {104, 103}
match_2 :  {104, 103, 68}

How can I "trim" or remove the duplicate objects based on previous ones? something like:

exact_match : {104}
match_4 :  {103}
match_2 :  {68}

I tried _.difference but did not work (Maybe because it is for arrays not objects?):

var exact_match = data.exact_match,
    match_four_digits = _.difference(data.match_4, data.exact_match),
    match_two_digits = _.difference(data.match_2, data.exact_match, data.match_4),

Any help would be appreciated :)

Update

I need that the returned value has the same object data instead of a new array :)

Share Improve this question edited Mar 4, 2015 at 20:20 numediaweb asked Mar 4, 2015 at 15:52 numediawebnumediaweb 17k12 gold badges78 silver badges116 bronze badges 3
  • Have you tried with _.keys? – Jonathan Allard Commented Mar 4, 2015 at 15:57
  • Same object reference, or new object with the same keys? – Adam Boduch Commented Mar 4, 2015 at 19:24
  • @AdamBoduch Thanks, I already found the solution.. – numediaweb Commented Mar 4, 2015 at 20:21
Add a ment  | 

3 Answers 3

Reset to default 9

It looks like you want to diff keys (or rather, it'd be efficient to — _.keys)

_.difference(
  _.keys({104: 1, 102: 3, 101: 0}), // ["104", "102", "101"]
  _.keys({104: 1, 102: 3}) // ["104", "102"]
)
// [ "101" ]

Or, you could always convert your object to an array of pairs if you want to pare within the objects too (_.pairs):

_.difference(
  _.pairs({104: 1, 102: 3, 101: 0}), // [["104",1], ["102",3], ["101",0]]
  _.pairs({104: 1, 102: 2}) // [["104",1], ["102",2]]
)
// [["102", 3], ["101", 0]]

I would create a map called unique, e.g. var unique = {}; and then iterate over each key in your data and check if it's in unique. If it is in unique, delete the entry associated with that key, thus removing duplicates.

You could pull this check out as an alreadyFound method:

var alreadyFound = function (key) {
  if (!(key in unique)) {
    unique[key] = true;
    return false;
  }
  return true;
};

Then iterate over your data and check alreadyFound(key) for key in your data, and delete the key if alreadyFound(key) returns true.

You could go messing with lodash/underscore methods but those might be inefficient depending on how you use them (and how they're implemented) and this should operate in linear time.

It looks like for your specific use case the full solution would be something like:

var unique = {};
// Assume I copy and pasted alreadyFound here
var alreadyFound = ...;
for (var object in data) {
  // Iterate through ids in each object in data
  for (var id in object) {
    // Remove this entry if it's already found
    if (alreadyFound(id)) {
      delete object[id];
    }
  }
}

Thanks guys for the answers, I really appreciate your time.

I searched further and found this post by Lodash developer that helped me came up with this snippet;

var data = {
  exact_match: {
    104: {
      supplier_id: 104
    }
  },
  match_four_digits: {
    104: {
      supplier_id: 104
    },
    68: {
      supplier_id: 68
    }
  },
  match_two_digits: {
    104: {
      supplier_id: 104
    },
    68: {
      supplier_id: 68
    },
    103: {
      supplier_id: 103
    },
    999: {
      supplier_id: 999
    }
  }
};

var arr_match_four_digits = _.difference(_.keys(data.match_four_digits), _.keys(data.exact_match));
var arr_match_two_digits = _.difference(_.keys(data.match_two_digits), _.keys(data.match_four_digits), _.keys(data.exact_match));



$('#output1').html(JSON.stringify(data));
$('#output2').html(JSON.stringify(_.pick(data.match_four_digits, arr_match_four_digits)));
$('#output3').html(JSON.stringify(_.pick(data.match_two_digits, arr_match_two_digits)));
<script src="https://cdn.rawgit./lodash/lodash/3.3.1/lodash.min.js"></script>
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>

data
<pre><code><div id="output1"></div></code></pre>
arr_match_four_digits
<pre><code><div id="output2"></div></code></pre>
match_two_digits
<pre><code><div id="output3"></div></code></pre>

发布评论

评论列表(0)

  1. 暂无评论