Remove duplicate values from JSON array based on key (date) in JS

I want to remove duplicate values from array base on key with below conditions.

  1. If End Date is same
  2. If Start Date is small than other then use small start date value.

Below is Input and excepted output.

Input:

[
  {
    "StartDate": "2020-01-15",
    "EndDate": "2020-02-14"
  },
  {
    "StartDate": "2019-12-13",
    "EndDate": "2020-01-15"
  },
  {
    "StartDate": "2019-11-13",
    "EndDate": "2020-01-15"
  },
  {
    "StartDate": "2019-10-11",
    "EndDate": "2019-11-13"
  }
]

Output:

[
  {
    "StartDate": "2020-01-15",
    "EndDate": "2020-02-14"
  },
  {
    "StartDate": "2019-11-13",
    "EndDate": "2020-01-15"
  },
  {
    "StartDate": "2019-10-11",
    "EndDate": "2019-11-13"
  }
]

Start date: 2019-12-13 is bigger than 2019-11-13 with same End date 2020-01-15 so it will be remove.

Please suggest me solution.

I have tired with array.some() function but it only returns is value duplicate or not.

Below is my code.

var array = [{"StartDate":"2020-01-15","EndDate":"2020-02-14"},{"StartDate":"2019-12-13","EndDate":"2020-01-15"},{"StartDate":"2019-11-13","EndDate":"2020-01-15"},{"StartDate":"2019-10-11","EndDate":"2019-11-13"}];

var newArray = [];

for (let data of array) {

    var result = newArray.some(function (o) {

        var d1 = new Date(o["StartDate"]);
        var d2 = new Date(data.StartDate);

        if (o["EndDate"] === data.EndDate && d1 > d2) {
            //console.log(o["StartDate"] +' === '+data.StartDate);
            return true;
        } else {
            return false;
        }

    });

    if (result == false) {

        newArray.push({
            "StartDate": data.StartDate,
            "EndDate": data.EndDate
        });
    }

}

console.log(newArray);

Thanks

259 thoughts on “Remove duplicate values from JSON array based on key (date) in JS”

  1. When someone writes an piece of writing he/she maintains the plan of a user in his/her brain that how a user can understand it.
    Thus that’s why this article is perfect. Thanks!

    Reply
  2. I know this if off topic but I’m looking into starting
    my own weblog and was curious what all is required to get setup?
    I’m assuming having a blog like yours would cost a pretty penny?
    I’m not very web smart so I’m not 100% positive.
    Any tips or advice would be greatly appreciated. Kudos

    Reply
  3. I have been exploring for a little bit for any high-quality
    articles or blog posts on this kind of area . Exploring in Yahoo I ultimately stumbled upon this website.
    Studying this info So i’m satisfied to express
    that I have an incredibly excellent uncanny feeling I discovered exactly what I needed.
    I such a lot surely will make certain to do not disregard this site and give it
    a glance regularly.

    my web page … Oracle Leaf CBD Review

    Reply
  4. Excellent weblog right here! Additionally your site loads up very fast!
    What host are you the use of? Can I get your
    affiliate link for your host? I wish my site loaded
    up as quickly as yours lol

    Reply
  5. Hello There. I found your blog using msn. This is a really well written article.
    I will make sure to bookmark it and come back to read more of
    your useful info. Thanks for the post. I’ll definitely comeback.

    Also visit my web blog ecuamir.com

    Reply
  6. Thank you for your own labor on this site. Debby really loves participating in internet research and it is easy to understand
    why. My partner and i notice all regarding the dynamic method you offer both interesting and useful tips on your blog
    and increase participation from website visitors on that content while our child is now becoming educated so much.

    Take pleasure in the rest of the year. You’re the one performing a fantastic job.[X-N-E-W-L-I-N-S-P-I-N-X]I am really inspired
    together with your writing talents and also with the layout to your blog.
    Is this a paid topic or did you modify it yourself?
    Anyway keep up the excellent quality writing, it’s uncommon to look a great weblog like this one nowadays.

    Here is my blog post … Slimy Vita Review

    Reply
  7. Hey! This is my first visit to your blog!
    We are a collection of volunteers and starting a new initiative in a community in the same niche.
    Your blog provided us useful information to work on. You have
    done a wonderful job!

    Also visit my web-site – Keto BHB Plus

    Reply
  8. Thanks for your own efforts on this web site. Kate really loves
    making time for investigation and it’s obvious why.
    A number of us know all regarding the lively medium you produce
    advantageous steps through this web site and as well as foster participation from website visitors on the situation while our
    daughter is actually understanding a great deal. Have fun with the remaining portion of the new year.

    You have been conducting a really good job.[X-N-E-W-L-I-N-S-P-I-N-X]I’m really
    impressed with your writing talents as well as with the structure to your weblog.

    Is that this a paid topic or did you modify it yourself?
    Anyway stay up the nice high quality writing, it
    is rare to look a nice weblog like this one nowadays.

    Also visit my website: Xoth Keto BHB

    Reply
  9. With havin so much content and articles do you ever run into any problems of plagorism
    or copyright infringement? My site has a lot of exclusive content I’ve either
    authored myself or outsourced but it seems a lot of it is popping it up all over the internet without my agreement.
    Do you know any methods to help protect against
    content from being stolen? I’d definitely appreciate it.

    Here is my website SuperCharged IQ

    Reply
  10. Pingback: keto popsicles
  11. A lot of thanks for all of the effort on this site.
    My aunt takes pleasure in setting aside time for research and it’s really easy to
    understand why. Most people learn all concerning the powerful
    tactic you create sensible techniques via the web blog and even encourage contribution from others about this subject matter and my daughter is understanding a lot of things.
    Take pleasure in the remaining portion of the new year.
    You have been conducting a stunning job.

    Review my website – Flow Zone Male Enhancement Reviews

    Reply
  12. Good day! I could have sworn I’ve been to this website
    before but after checking through some of the post I realized it’s new to me.
    Anyways, I’m definitely glad I found it and I’ll be book-marking and checking back often!

    Here is my website ecs300.net

    Reply
  13. Hi there! I understand this is sort of off-topic however
    I needed to ask. Does building a well-established blog like yours take a large amount of work?

    I’m brand new to writing a blog however I do write in my journal everyday.
    I’d like to start a blog so I can share my personal experience and feelings online.
    Please let me know if you have any kind of recommendations or tips for new aspiring bloggers.
    Appreciate it!

    Also visit my web site: Greener Earth CBD Gummies

    Reply
  14. Can I simply say what a relief to discover somebody who genuinely knows what they are talking about over the internet.
    You definitely realize how to bring an issue to light and
    make it important. More people ought to read this and understand this side of the story.
    I can’t believe you’re not more popular since you most certainly have the
    gift.

    My website :: Brilliance Keto Ingredients

    Reply
  15. Thanks , I’ve just been searching for information approximately this subject for a
    while and yours is the best I’ve found out so far.
    However, what about the bottom line? Are you positive about the source?

    Reply
  16. Hey! I just wanted to ask if you ever have any problems with hackers?

    My last blog (wordpress) was hacked and I ended up losing
    a few months of hard work due to no backup. Do you
    have any methods to prevent hackers?

    Have a look at my web site :: MosQiller S

    Reply
  17. Fantastic beat ! I wish to apprentice while you amend your site,
    how can i subscribe for a blog website? The account helped me a acceptable deal.
    I had been a little bit acquainted of this your broadcast offered bright clear
    concept

    my web blog – kanmoulue.com

    Reply
  18. I am no longer sure where you are getting your information, however great topic.
    I needs to spend a while learning much more or figuring out more.
    Thanks for excellent info I used to be looking for this
    info for my mission.

    Reply
  19. Can I just say what a comfort to find someone who actually knows what they are discussing over the internet.
    You certainly know how to bring an issue to light and make it important.
    More and more people must look at this and
    understand this side of your story. I was surprised you aren’t more popular because you certainly possess
    the gift.

    My homepage Green Flame CBD

    Reply
  20. Unquestionably consider that that you stated. Your favourite justification seemed to be at the internet the easiest thing to take into account of.
    I say to you, I certainly get annoyed even as other people
    consider concerns that they just don’t understand about.
    You controlled to hit the nail upon the highest and
    also defined out the entire thing without having side-effects , other people can take a
    signal. Will probably be back to get more. Thanks!

    My web blog; Imarais Beauty

    Reply
  21. Simply desire to say your article is as astounding.

    The clarity in your post is simply spectacular and i could assume you
    are an expert on this subject. Well with your permission allow me to grab your feed to keep up to date with
    forthcoming post. Thanks a million and please carry on the rewarding work.

    Reply
  22. Hmm it appears like your site ate my first comment (it was extremely long) so I guess I’ll just sum it up what I submitted and say, I’m thoroughly enjoying your blog.
    I too am an aspiring blog blogger but I’m still new to the whole thing.

    Do you have any points for newbie blog writers? I’d genuinely
    appreciate it.

    Reply
  23. Howdy! Do you know if they make any plugins to help with Search
    Engine Optimization? I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very good gains.

    If you know of any please share. Thank you!

    Reply
  24. That is really attention-grabbing, You’re a very skilled blogger.
    I have joined your rss feed and look forward to seeking extra of your magnificent post.
    Also, I have shared your site in my social networks

    Reply
  25. Thanks on your marvelous posting! I seriously enjoyed reading it, you’re a great
    author. I will ensure that I bookmark your blog and may come back very soon. I want to encourage one to
    continue your great writing, have a nice morning!

    Reply
  26. Greetings from Idaho! I’m bored to death at work so I decided to browse your blog on my iphone during lunch break.
    I enjoy the info you provide here and can’t wait to take a look when I
    get home. I’m surprised at how quick your blog loaded on my cell phone
    .. I’m not even using WIFI, just 3G .. Anyways,
    excellent blog!

    Also visit my site – Keto Incinerate

    Reply
  27. Write more, thats all I have to say. Literally, it seems as
    though you relied on the video to make your point.
    You definitely know what youre talking about, why throw away your intelligence
    on just posting videos to your weblog when you could be giving us
    something enlightening to read? quest bars http://bit.ly/3C2tkMR quest bars

    Reply
  28. Thank you for your website post. Jones and I have already been saving for a new guide
    on this matter and your article has made us
    all to save the money. Your opinions really responded to all our questions.
    In fact, greater than what we had thought of before
    we came upon your fantastic blog. I actually no longer have doubts along with a troubled
    mind because you have clearly attended to the
    needs right here. Thanks

    My homepage – Reneaux Skin Care

    Reply
  29. I do accept as true with all the ideas you’ve introduced
    in your post. They’re very convincing and can definitely work.

    Still, the posts are very short for beginners.
    May just you please lengthen them a little from subsequent time?
    Thanks for the post.

    Also visit my webpage Freya

    Reply
  30. It is the best time to make a few plans for the long run and it is
    time to be happy. I’ve read this post and if I could I desire to recommend you some fascinating things or advice.
    Perhaps you can write subsequent articles referring to this article.
    I want to learn more things about it!

    Also visit my website :: quanfff.com

    Reply
  31. Hmm it appears like your site ate my first comment (it was extremely
    long) so I guess I’ll just sum it up what I submitted and say,
    I’m thoroughly enjoying your blog. I as well am an aspiring blog writer but I’m still new to everything.

    Do you have any suggestions for newbie blog writers? I’d definitely appreciate it.

    My blog post: everythingarcades.com

    Reply
  32. Right here is the perfect blog for anybody who hopes to understand
    this topic. You know so much its almost hard to argue with you
    (not that I personally will treatments need to?HaHa).
    You certainly put a brand new spin on a topic which has been written about for years.
    Wonderful stuff, just wonderful!

    Reply
  33. I’m also commenting to make you be aware of what a impressive
    discovery our child enjoyed checking your blog.
    She came to understand so many issues, which include how it is like to possess a wonderful giving heart to have
    the others easily have an understanding of selected tricky issues.
    You truly exceeded her desires. I appreciate you for giving the beneficial, safe, edifying and as well as
    unique guidance on this topic to Ethel.

    my blog :: libido cures

    Reply
  34. It’s the best time to make some plans for the future and it’s time
    to be happy. I have read this post and if I could I want to suggest you few
    interesting things or tips. Maybe you could write next
    articles referring to this article. I wish to read more things about it!

    Here is my web site … children smoking

    Reply

Leave a Comment