2

I have 2 JSON objects as

obj1 = [
  {
    "start": "2019-11-15T00:00:00",
    "end": "2019-11-15T23:59:59",
    "Room Night": "2019-11-15"
  },
  {
    "start": "2019-11-13T00:00:00",
    "end": "2019-11-13T23:59:59",
    "Room Night": "2019-11-13"}
  {
    "start": "2019-11-14T00:00:00",
    "end": "2019-11-14T23:59:59",
    "Room Night": "2019-11-14"
  }
]

and

obj2 = [
  {
    "start": "2019-11-15T00:00:00", 
    "end": "2019-11-15T23:59:59", 
    "Room Night": "2019-11-15"
  },
  {
    "start": "2019-11-13T00:00:00",
    "end": "2019-11-13T23:59:59",
    "Room Night": "2019-11-13"
  },
  {
    "start": "2019-11-16T00:00:00",
    "end": "2019-11-16T23:59:59",
    "Room Night": "2019-11-16"
  },
  {
    "start": "2019-11-17T00:00:00", 
    "end": "2019-11-17T23:59:59", 
    "Room Night": "2019-11-17"
  }
] 

In the above 2 JSONs, i have 2 duplicate "Room Nights" , dates as 2019-11-13 and 2019-11-15. I want to delete the entire duplicate values and get the output as below in JSON format.

[
  {
    "start": "2019-11-14T00:00:00", 
    "end": "2019-11-14T23:59:59", 
    "Room Night": "2019-11-14"
  },
  {
    "start": "2019-11-16T00:00:00",
    "end": "2019-11-16T23:59:59",
    "Room Night": "2019-11-16"
  },
  {
    "start": "2019-11-17T00:00:00", 
    "end": "2019-11-17T23:59:59",
    "Room Night": "2019-11-17"
  }
] 

Could you please help to sort out this.

Regards, Prets.

Yannick K
  • 4,887
  • 3
  • 11
  • 21
Prets
  • 33
  • 3
  • 1
    Possible duplicate of [Remove duplicates from an array of objects in JavaScript](https://stackoverflow.com/questions/2218999/remove-duplicates-from-an-array-of-objects-in-javascript) – Harun Yilmaz Nov 01 '19 at 11:11
  • Welcome to stackoverflow. What have you tried to solve this yourself? We're not a code-writing service, and we expect people to research the subject, give it a go... and only then, if they're still struggling, then show the code in a [mcve]. Please have a read of the [help] and the [ask] section in particular – freefaller Nov 01 '19 at 11:12
  • @HarunYilmaz He has multiple arrays, and is merging and removing duplicates. – Kobe Nov 01 '19 at 11:12
  • @Kobe It doesn't matter if you have one array or two. The logic is the same. – Harun Yilmaz Nov 01 '19 at 11:14
  • @HarunYilmaz True, but he wants to remove the duplicate **as well as the** the original – Kobe Nov 01 '19 at 11:17

3 Answers3

1

It can be done through filter and some methods:

let obj1= [
{"start": "2019-11-15T00:00:00", "end": "2019-11-15T23:59:59", "Room Night": "2019-11-15"},
{"start": "2019-11-13T00:00:00", "end": "2019-11-13T23:59:59", "Room Night": "2019-11-13"},
{"start": "2019-11-14T00:00:00", "end": "2019-11-14T23:59:59", "Room Night": "2019-11-14"}
];

let obj2= [
{"start": "2019-11-15T00:00:00", "end": "2019-11-15T23:59:59", "Room Night": "2019-11-15"},
{"start": "2019-11-13T00:00:00", "end": "2019-11-13T23:59:59", "Room Night": "2019-11-13"},
{"start": "2019-11-16T00:00:00", "end": "2019-11-16T23:59:59", "Room Night": "2019-11-16"},
{"start": "2019-11-17T00:00:00", "end": "2019-11-17T23:59:59", "Room Night": "2019-11-17"}
]

const merged = obj1.concat(obj2);
const duplicates = obj2.filter(o=> obj1.some(s=> s["Room Night"]==o["Room Night"]));
const result = merged.filter(f=> !duplicates.some(s=> 
    s["Room Night"] == f["Room Night"] 
));
console.log(result);

Without arrow function:

let obj1= [
{"start": "2019-11-15T00:00:00", "end": "2019-11-15T23:59:59", "Room Night": "2019-11-15"},
{"start": "2019-11-13T00:00:00", "end": "2019-11-13T23:59:59", "Room Night": "2019-11-13"},
{"start": "2019-11-14T00:00:00", "end": "2019-11-14T23:59:59", "Room Night": "2019-11-14"}
];

let obj2= [
{"start": "2019-11-15T00:00:00", "end": "2019-11-15T23:59:59", "Room Night": "2019-11-15"},
{"start": "2019-11-13T00:00:00", "end": "2019-11-13T23:59:59", "Room Night": "2019-11-13"},
{"start": "2019-11-16T00:00:00", "end": "2019-11-16T23:59:59", "Room Night": "2019-11-16"},
{"start": "2019-11-17T00:00:00", "end": "2019-11-17T23:59:59", "Room Night": "2019-11-17"}
]

const merged = obj1.concat(obj2);
const duplicates = obj2.filter( function(o) {
return obj1.some(function(s) {
    return s["Room Night"] ==o ["Room Night"];
});
});

const result = merged.filter(function(f) {
return !duplicates.some(function(s) {
    return s["Room Night"] == f["Room Night"] }
    );
});
console.log(result);
StepUp
  • 36,391
  • 15
  • 88
  • 148
1

This solution isn't great, but it gets the job done. I wouldn't recommend using JSON.stringify to use objects as keys, but that's what I've done here:

const obj1 = [
  { start: '2019-11-15T00:00:00', end: '2019-11-15T23:59:59', 'Room Night': '2019-11-15' },
  { start: '2019-11-13T00:00:00', end: '2019-11-13T23:59:59', 'Room Night': '2019-11-13' },
  { start: '2019-11-14T00:00:00', end: '2019-11-14T23:59:59', 'Room Night': '2019-11-14' }
]

const obj2 = [
  { start: '2019-11-15T00:00:00', end: '2019-11-15T23:59:59', 'Room Night': '2019-11-15' },
  { start: '2019-11-13T00:00:00', end: '2019-11-13T23:59:59', 'Room Night': '2019-11-13' },
  { start: '2019-11-16T00:00:00', end: '2019-11-16T23:59:59', 'Room Night': '2019-11-16' },
  { start: '2019-11-17T00:00:00', end: '2019-11-17T23:59:59', 'Room Night': '2019-11-17' }
]

const s = s => JSON.stringify(s)
const p = s => JSON.parse(s)

const combined = [...obj1, ...obj2]
const mapEntries = [...combined.reduce((a, v) => a.set(s(v), (a.get(s(v)) || 0) + 1), new Map()).entries()]
const out = mapEntries.reduce((a, v) => (v[1] === 1 && a.push(p(v[0])), a), [])


console.log(out)
Kobe
  • 6,226
  • 1
  • 14
  • 35
-2

 let obj1= [{"start": "2019-11-15T00:00:00", "end": "2019-11-15T23:59:59", "Room Night": "2019-11-15"},{"start": "2019-11-13T00:00:00", "end": "2019-11-13T23:59:59", "Room Night": "2019-11-13"},{"start": "2019-11-14T00:00:00", "end": "2019-11-14T23:59:59", "Room Night": "2019-11-14"}];

let obj2=[{"start": "2019-11-15T00:00:00", "end": "2019-11-15T23:59:59", "Room Night": "2019-11-15"},{"start": "2019-11-13T00:00:00", "end": "2019-11-13T23:59:59", "Room Night": "2019-11-13"},{"start": "2019-11-16T00:00:00", "end": "2019-11-16T23:59:59", "Room Night": "2019-11-16"},{"start": "2019-11-17T00:00:00", "end": "2019-11-17T23:59:59", "Room Night": "2019-11-17"}]

obj1 = obj1.concat(obj2);
let temp=[];
let result=[];
obj1.forEach(x=>{
if(temp.indexOf(x['Room Night']) == -1){
temp.push(x['Room Night']);
result.push(x);
}
})

console.log(result);
sara
  • 11
  • 1
  • Code-only answers are generally frowned upon on this site. Could you please edit your answer to include some comments or explanation of your code? Explanations should answer questions like: What does it do? How does it do it? Where does it go? How does it solve OP's problem? See: [How to anwser](https://stackoverflow.com/help/how-to-answer). Thanks! – Eduardo Baitello Nov 01 '19 at 23:36