Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8112489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:31:05+00:00 2026-06-06T02:31:05+00:00

I have data like the following: var data = [{ id: 1, date: new

  • 0

I have data like the following:

var data = [{
   id: 1,
   date: new Date("2010-01-01"),
   value: 10
}, {
   id: 2,
   date: new Date("2010-01-01"),
   value: 11
}, {
   id: 3,
   date: new Date("2010-01-01"),
   value: 12
}, {
   id: 4,
   date: new Date("2010-01-02"),
   value: 10
}, {
   id: 5,
   date: new Date("2010-01-03"),
   value: 10
}, {
   id: 6,
   date: new Date("2010-01-03"),
   value: 21
}, {
   id: 7,
   date: new Date("2010-01-03"),
   value: 22
}, {
   id: 8,
   date: new Date("2010-01-03"),
   value: 23
}];

I am trying to apply two kinds of filters:

  • Filter1 should give me only those points that differ by 1 in their value field but having the same date field. Therefore, this should return a new dataset containing data records with ids 1,2,3, 6,7,8 (the first three because values are 10,11,12 and last three because values are 21,22,23)
  • Filter2 should give me only those points that differ by 1 day in their date field but having the same value field. Therefore, this should return a new dataset containing data records with ids 1,4,5

I am currently doing this in C# on the server-side but am looking to see if there is an efficient way to do this in Javascript. Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T02:31:08+00:00Added an answer on June 6, 2026 at 2:31 am

    Why, custom sort functions, of course!

    function filter1(arr) {
        // Clone the array:
        arr = arr.slice();
    
        // Sort the array by value:
        arr.sort(function(a, b) {
            return a.date - b.date || a.value - b.value;
        });
    
        // Look for consecutive elements with the same date:
        var r = [];
    
        for(var i = 1; i < arr.length; i++) {
            if(+arr[i - 1].date === +arr[i].date && arr[i - 1].value === arr[i].value - 1) {
                if(r.indexOf(arr[i - 1]) === -1) r.push(arr[i - 1]);
                r.push(arr[i]);
            }
        }
    
        // Return the result
        return r;
    }
    
    function filter2(arr) {
        // Clone the array:
        arr = arr.slice();
    
        // Sort the array by date:
        arr.sort(function(a, b) {
            return a.value - b.value || a.date - b.date;
        });
    
        // Look for consecutive elements with the same value:
        var r = [];
    
        for(var i = 1; i < arr.length; i++) {
            if(arr[i - 1].value === arr[i].value && arr[i].date - arr[i - 1].date <= 24 * 60 * 60 * 1000) {
                if(r.indexOf(arr[i - 1]) === -1) r.push(arr[i - 1]);
                r.push(arr[i]);
            }
        }
    
        // Return the result
        return r;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following data test<-data.frame(group=1:10, var.a=rnorm(n=10,mean=500,sd=20), var.b=runif(10)) I would like a barplot with
I have some data formated like the following 2009.07.02 02:20:14 40.3727 28.2330 6.4 2.6
I have a data file that looks like the following example. I've added '%'
I have a data source with about 2000 lines that look like the following:
I have some data in a table that looks roughly like the following: table
I have something like the following data structure: Category StartDateTime EndDateTime =============================================== 1 12/1/2009
I have an xml like the following <DataCollection> <Data> <Name>John</Name> <Age>30</Age> </Data> ... more
I have a jQuery post call like the following: $.post('/Controller/_PartialAction', { 'param1': data },
I have the following C# code: public JsonResult Graph() { var result = new
I have an array of javascript objects like the following: var food = [

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.