0

Has someone an idea how the function have to look like if I want the following results:

In:

{
  3: [ "1-2", "4-5" ],
  4: [ "1-2", "6-9" ]
}

Out:

{
  3: [ "4-5" ],
  4: [ "1-2", "6-9" ]
}

I want to remove all duplicate values and leave the last occurrence in the object. If there are no elements left in a property, it should be removed. I tried the suggestions from https://stackoverflow.com/a/9229821/3852382 but it does not solve my problem.

iPaat
  • 792
  • 4
  • 16
  • 1
    Tag your question with the language you are using. – Gordon Linoff Aug 12 '17 at 14:49
  • Excuse me. I have added the language I use. – iPaat Aug 12 '17 at 14:52
  • 1
    "last" has no meaning in an object: object properties are unordered. – trincot Aug 12 '17 at 14:53
  • How would you want to measure the performance? – Mμ. Aug 12 '17 at 14:54
  • @trincot I mean the last occurrence. I know object properties don't have an order. – iPaat Aug 12 '17 at 14:55
  • OK, so you would be fine if the output was `{ 3: ["1-2", "4-5"], 4: ["6-9"] }` then? – trincot Aug 12 '17 at 14:57
  • @D-reaper: I would do some iterations with the algorithm with big objects. – iPaat Aug 12 '17 at 14:59
  • @trincot If the input was `{ 4: ["1-2", "6-9"], 3: ["1-2", "4-5"] }`. After the object is cleaned, I want to iterate over it to do some replacements within a string. If it would not an object would it be easier then? – iPaat Aug 12 '17 at 15:01
  • Your object is not "organised" in any sense of the word so "If the input was `{ 4: ["1-2", "6-9"], 3: ["1-2", "4-5"] }`" makes no sense. @trincot's question still stands. – Andy Aug 12 '17 at 15:08
  • Okay. If I have an object and iterate over it, something comes first and something comes last. I don't know how JavaScript decides which comes first and which comes last. But I would have the one, that JavaScript decides to be the last, the occurrence which remains. Or is that what u want to say, that if I iterate two times over an object, it is not clear which comes first and which comes last? In this case, what should I do to "organize" it? – iPaat Aug 12 '17 at 15:12
  • It may differ from one JS engine to another. You shouldn't base your code on that because you may get the wrong result. – Andy Aug 12 '17 at 15:19
  • @Andy I got a solution which works and solve my problem. But I have to think about what u said. So what do you suggest to do? – iPaat Aug 12 '17 at 15:21
  • you can always manipulate the object by adding to an array, sorting it like you expect it to behave, and then perform operation on the orignal object using this sorted array as a reference – marvel308 Aug 12 '17 at 15:24
  • @marvel308 Would you please add an example to your answer as "best practice"? Let's say that the higher property number is the one which should remain? – iPaat Aug 12 '17 at 15:32
  • check the first answer in https://stackoverflow.com/questions/5467129/sort-javascript-object-by-key, I'll add it in a while – marvel308 Aug 12 '17 at 15:40
  • In mainstream JS engines, "integer" properties mainly get listed in a sorted fashion regardless of in what order they are added. – Redu Aug 12 '17 at 22:31

2 Answers2

0

I think you can do this in the following way

let obj = {
  3: [ "1-2", "4-5" ],
  4: [ "1-2", "6-9" ]
};

let reverseMap = {};
for(property in obj){
 for(let element of obj[property]){
  if(reverseMap[element] == undefined || reverseMap[element] == null){
   ;
  }
  else {
   obj[reverseMap[element]].splice(obj[reverseMap[element]].indexOf(element), 1);
  }
  reverseMap[element] = property;
 }
}

console.log(obj);
marvel308
  • 10,288
  • 1
  • 21
  • 32
0

You may do as follows;

var obj = {3: [ "1-2", "4-5" ], 4: [ "1-2", "6-9" ]},
    res = Object.keys(obj)
                .reduceRight((m,k) => obj[k].reduce((t,e) => t[e] ? t 
                                                                  : (t[e] = true, (t.r[k] = t.r[k] ? t.r[k].concat(e)
                                                                                                   : [e]), t), m), {r:{}}).r;
console.log(res);
Redu
  • 25,060
  • 6
  • 56
  • 76