0

I have any array that looks like this:

[
    { "tempValue": "Sunday  CLOSE to CLOSE" }, 
    { "tempValue": "Sunday  CLOSE to 08:30" }, 
    { "tempValue": "Sunday  CLOSE to 08:30" }, 
    { "tempValue": "Tuesday  CLOSE to 08:30" }, 
    { "tempValue": "Thursday  CLOSE to 08:30" }
]

I need to remove all the dupliactes from this array. In the above example, the duplicates are Sunday...

So I tried something like this:

function unique(list) {
    var result = [];
    $.each(list, function(i, e) {
        if ($.inArray(e, result) == -1) result.push(e);
    });
    return result;
}


unique(hours);

The variable hours is my array.

But my code above doesn't work!

Could someone please advice on this issue?

EDIT:

The desired output is this:

[
    { "tempValue": "Tuesday  CLOSE to 08:30" }, 
    { "tempValue": "Thursday  CLOSE to 08:30" }
]
Muowee
  • 54
  • 8
James Juanjie
  • 219
  • 3
  • 18
  • What's your desired output? The array minus the 3rd item (duplicate of the 2nd item), = 4 items total, is that it? – CertainPerformance Jul 11 '19 at 09:45
  • @CertainPerformance, please view my edit. – James Juanjie Jul 11 '19 at 09:47
  • 1
    Possible duplicate of [Get all unique values in a JavaScript array (remove duplicates)](https://stackoverflow.com/questions/1960473/get-all-unique-values-in-a-javascript-array-remove-duplicates) – jhujhul Jul 11 '19 at 09:48
  • 1
    @jhujhul OP is looking to *remove* all items which have duplicates (not just the duplicates), given a particular logic for the string - his case is pretty different. – CertainPerformance Jul 11 '19 at 09:54

4 Answers4

2

I'd first count up the number of occurrences of each day, and create an array containing only the days that occur more than once. Then, you can .filter by whether the day in an array item is included in that array:

const input = [{
    "tempValue": "Sunday  CLOSE to CLOSE"
}, {
    "tempValue": "Sunday  CLOSE to 08:30"
}, {
    "tempValue": "Sunday  CLOSE to 08:30"
}, {
    "tempValue": "Tuesday  CLOSE to 08:30"
}, {
    "tempValue": "Thursday  CLOSE to 08:30"
}];

const dayCountObj = input.reduce((a, { tempValue }) => {
  const day = tempValue.match(/^\S+/)[0];
  a[day] = (a[day] || 0) + 1;
  return a;
}, {});
const daysToRemove = Object.entries(dayCountObj)
  .filter(([, count]) => count > 1)
  .map(([day]) => day);
  
const output = input.filter(({ tempValue }) => {
  const day = tempValue.match(/^\S+/)[0];
  return !daysToRemove.includes(day);
});
console.log(output);

If you want to reduce the computational complexity to O(N) instead of O(N^2), use a Set instead of an array for the daysToRemove:

const input = [{
    "tempValue": "Sunday  CLOSE to CLOSE"
}, {
    "tempValue": "Sunday  CLOSE to 08:30"
}, {
    "tempValue": "Sunday  CLOSE to 08:30"
}, {
    "tempValue": "Tuesday  CLOSE to 08:30"
}, {
    "tempValue": "Thursday  CLOSE to 08:30"
}];

const dayCountObj = input.reduce((a, { tempValue }) => {
  const day = tempValue.match(/^\S+/)[0];
  a[day] = (a[day] || 0) + 1;
  return a;
}, {});
const daysToRemove = new Set(
  Object.entries(dayCountObj)
    .filter(([, count]) => count > 1)
    .map(([day]) => day)
);
  
const output = input.filter(({ tempValue }) => {
  const day = tempValue.match(/^\S+/)[0];
  return !daysToRemove.has(day);
});
console.log(output);
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
1

You can try this way also:

var list  = [{"tempValue": "Sunday  CLOSE to CLOSE"}, {"tempValue": "Sunday  CLOSE to 08:30"}, {"tempValue": "Sunday  CLOSE to 08:30"}, {"tempValue": "Tuesday  CLOSE to 08:30"}, {"tempValue": "Thursday  CLOSE to 08:30"}];    
    function removeDuplicate(json){
    var uniques=[];
    var stringify={};
    for(var i=0;i<json.length;i++){
       var keys=Object.keys(json[i]);
       keys.sort(function(a,b) {return a-b});
       var str='';
        for(var j=0;j<keys.length;j++){
           str+= JSON.stringify(keys[j]);
           str+= JSON.stringify(json[i][keys[j]]);
        }
        if(!stringify.hasOwnProperty(str)){
            uniques.push(json[i]);
            stringify[str]=true;
        }
    }
    return uniques;
}

var list = removeDuplicate(list);

console.log(list);
Pankaj Chauhan
  • 1,623
  • 14
  • 12
  • Hi james, Its working..actually result display in console, now i display on browser please check on this : https://jsfiddle.net/Lpxh6q8b/3/ – Pankaj Chauhan Jul 11 '19 at 14:35
1

It's just your code with only 2 more commands:

function unique(list) {

  var result = [];
  var resultJson = [];

  $.each(list, function(i, e) {
    if ($.inArray(e.tempValue, result) == -1) {
     result.push(e.tempValue);
     resultJson.push(e);
    }
  });
  return resultJson;
}

 unique(hours);
pedro
  • 23
  • 5
0

One line solution could be implemented as follows:

const unique = (hours) => [... new Set(hours)];

unique(hours);

bonev93
  • 13
  • 3
  • @JamesJuanjie, `...` is the spread operator in js. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax – bonev93 Jul 12 '19 at 11:03