How to make Javascript array unique by attribute

What is a quick way to create a unique Javascript array by attribute?

I have an array sorted by timestamp like:

[
    {
        ts: 1417048100,
        release: 1.0
    },
    {
        ts: 1417046900,
        release: 1.1
    },
    {
        ts: 1417046712,
        release: 1.0
    }
]

      

And I want to make it unique by issue number keeping only those with the most recent time stamp. In my example, this would mean deleting the last entry, since there is a newer one for this version.

+3


source to share


6 answers


You can iterate over the array with forEach

and store the release numbers in an object to make sure all previously released iterations are collapsed from the array.



var arr = [{
    ts: 1417048100,
    release: 1.0
},
{
    ts: 1417046900,
    release: 1.1
},
{
    ts: 1417046712,
    release: 1.0
}];

var o = {};

arr.forEach(function(x, i) {
    if (x.release in o) arr.splice(i, 1);
    o[x.release] = null; 
});

document.body.innerHTML = '<pre>' + JSON.stringify(arr, null, 4) + '</pre>';
      

Run code


+2


source


Convert it to a mapping using it release

as a key.

var output = {};
for(var i=0; i<input.length; i++) {
  output[input[i].release] = input[i].ts;
}

      



It will automatically overwrite old keys with new release keys.

If you need it to be sorted, you will need to convert it to a list and sort, or filter the original list on only the remaining output keys.

+1


source


You can use Array.prototype.reduce along with Array.prototype.some , for example:

var items = [{ts: 1417048100,release: 1.0},{ts: 1417046900,release: 1.1},{ts: 1417046712,release: 1.0}];

var results = items.reduce(function(res, item){
var exists = res.some(function(t){ return (t.release === item.release );});        
     if (!exists){
        res.push(item);
     }
return res; },[]);
                                                                              document.body.innerHTML = '<pre>' + JSON.stringify(results) + '</pre>';
      

Run code


Array.prototype.some allows you to check if an element exists in an array, and if it does, it is not included in Array.prototype.reduce res.push(item);

when processing the array.

If you are open to use in other libraries, you might be interested in checking out the unique uniqueness:

https://lodash.com/docs#uniq

0


source


if you can use jquery then try with grep

var releases =[
    {
    ts: 1417048100,
    release: 1.0
    },
    {
        ts: 1417046900,
        release: 1.1
    },
    {
        ts: 1417046712,
        release: 1.0
    }];

    var newRelease={
        ts: 1517046712,
        release: 1.0
    };

releases = jQuery.grep(y, function(value) {
  return value.release != newRelease.release;
});

releases.push(newRelease);

      

0


source


Try

var arr = [
  {
    ts: 1417048100,
    release: 1.0
  },
  {
    ts: 1417046900,
    release: 1.1
  },
  {
    ts: 1417046712,
    release: 1.0
  }
]

, arr = arr.sort(function(a, b) {
  return (a.ts > b.ts) ? -1 : 1
}).filter(function(val, key) {
  return $.inArray(val.release, arr.map(function(v) {return v.release})) === key
});

      

    var arr = [
      {
        ts: 1417048100,
        release: 1.0
      },
      {
        ts: 1417046900,
        release: 1.1
      },
      {
        ts: 1417046712,
        release: 1.0
      }
    ]

    , arr = arr.sort(function(a, b) {
      return (a.ts > b.ts) ? -1 : 1
    }).filter(function(val, key) {
      return $.inArray(val.release, arr.map(function(v) {return v.release})) === key
    });

    $.each(arr, function(k, v) {
      $("<span>").html("release:" + v.release + ", timestamp:" + v.ts + "<br />")
      .appendTo("body")
    })
      

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
      

Run code


0


source


Converting an array to an associative array. The keys in an associative array are unique. Sample code shown in node.

→ node
> a ={ "1.0": 1417048100, "1.1": 1417046900}
{ '1.0': 1417048100,
  '1.1': 1417046900 }
> a["1.0"]
1417048100
> a["1.0"]=99999
99999
> a
{ '1.0': 99999,
  '1.1': 1417046900 }

      

For a better understanding, reading the hashes and the blog post below might also be helpful.

http://www.javascriptkata.com/2007/03/29/how-to-use-javascript-hashes/

-1


source







All Articles