1

Suppose i have this array in javascript

ar = ['C241C12A-45FD-40FF-B26E-B879107E584D||20',
      'AB1BB99D-4396-415A-A5D8-1BEF3E0149CE||33',
      'ADE54113-53D1-4F26-A01C-48EBE37E3CE8||85',
      'DB500FD1-5988-4202-A4AA-727AC29AC67A||85',
      '8A8EAB5A-E0D2-4AA8-96A0-42686FF5EB0C||10',
      '50C29E8A-16C7-4FC2-A186-C3D65F23C627||200',
      '70C79BBB-14C7-4F34-346B-CSSSDFVFZZZ7||200'
    ];

is there any way to get arrays like this

newarray1 = [
      'ADE54113-53D1-4F26-A01C-48EBE37E3CE8||85',
      'DB500FD1-5988-4202-A4AA-727AC29AC67A||85'
      ];
 newarray2 = [
      '50C29E8A-16C7-4FC2-A186-C3D65F23C627||200',
      '70C79BBB-14C7-4F34-346B-CSSSDFVFZZZ7||200'
      ];

Duplicate values that comes after the pipe symbol,grab that value and form a new array based on that

3
  • Looping through the entries and creating your new arrays is one way to go. Not sure if there can be a more efficient way of doing this. Commented Jan 17, 2013 at 5:43
  • Your example does not seem to be sorted, 10 coming between 85 and 200? Commented Jan 17, 2013 at 5:44
  • @Bergi ..its string thats why..removed that line Commented Jan 17, 2013 at 5:47

3 Answers 3

2
var arraysByKey = {};
for (var i=0; i<ar.length; i++) {
    var key = ar[i].split("||")[1];
    if (key in arraysByKey)
        arraysByKey[key].push( ar[i] );
    else
        arraysByKey[key] = [ ar[i] ];
}

The result (arraysByKey) will look like this then:

{
    "10": [
        "8A8EAB5A-E0D2-4AA8-96A0-42686FF5EB0C||10"
    ],
    "20": [
        "C241C12A-45FD-40FF-B26E-B879107E584D||20"
    ],
    "33": [
        "AB1BB99D-4396-415A-A5D8-1BEF3E0149CE||33"
    ],
    "85": [
        "ADE54113-53D1-4F26-A01C-48EBE37E3CE8||85",
        "DB500FD1-5988-4202-A4AA-727AC29AC67A||85"
    ],
    "200": [
        "50C29E8A-16C7-4FC2-A186-C3D65F23C627||200",
        "70C79BBB-14C7-4F34-346B-CSSSDFVFZZZ7||200"
    ]
}

If you only want arrays where "duplicate values" exists, just filter the result:

for (var key in arraysByKey)
    if (arraysByKey[key].length < 2)
        delete arraysByKey[key];
Sign up to request clarification or add additional context in comments.

2 Comments

@Bergi one doubt how to check if duplicate exists i means ` if(arraysByKey.length > 0) {` but it gives me undefined error ..although it has some
@Bergi ..i think this will do api.jquery.com/jQuery.isEmptyObject Thanks for the time..will mark it as answer
1

This is not the most elegant solution, but it will give you an object of your desired arrays.

dupecheck = {};
dupes = {};
$.each(ar, function () {
   var num = this.split('||')[1];

   if (typeof dupecheck[num] !== 'undefined') {
      if (typeof dupes[num] === 'undefined') {
         dupes[num] = [];
         dupes[num].push(dupecheck[num]);
      }
      dupes[num].push(this);
   }

   dupecheck[num] = this;
});

http://jsfiddle.net/38wme/

Comments

1
var keyArray = {};

for ( var counter = 0; counter < ar.length; counter++)
{
  var arValue = ar[ counter ];
  var intVal = ar.split("||")[1];

  if ( keyArray[ intVal ] == null || keyArray[ intVal ] == undefined )
  {
    keyArray[ intVal ] = new Array();
  }
  keyArray[ intVal ].push( arValue  );
}
filterDuplicates();

function filterDuplicates( keyArray )
{
  for ( key in keyArray )
  {

  keyArray[key] = keyArray[key].filter(function(elem, pos, self) {
    return self.indexOf(elem) == pos;
  }
  }
return keyArray();

)

}

Now you have got an array in each index value of 'keyArray' object

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.