0

How can i sort the data by date and find maximum value count between consecutive dates ? Example data:

const data = {
    'addias':{
        '2022-1-10': ['a', 'b', 'c', 'd', 'e', 'f', 'g'],
        '2022-1-09': ['a', 'b', 'c', 'd', 'e', 'f', 'g'],
        '2021-12-29': ['a'],
        '2021-12-28': ['y', 'u', 'v', 'd', 'f', 'f', 'g'],
        '2021-1-08': ['x', 't', 's', 'd', 'e', 'f', 'g'],
    },
    'nike':{
        '2022-1-20': ['a', 'b', 'c', 'd', 'e', 'f', 'g'],
        '2022-1-15': ['a', 'b', 'c'],
        '2022-1-14': ['a', 'b'],
        '2022-1-10': ['y', 'b', 'x', 'z', 'e', 'f', 'g'],
    }
}

result be like

const result = {
     'addias':{
        'firstDate': '2022-1-09',
        'secondDate':'2022-1-10',
        'total':14 ,
    },
    'nike':{
        'firstDate': '2022-1-14',
        'secondDate':'2022-1-15',
        'total':5,
    }
}

Attempt:

Object.entries(data).forEach((item) => {
   const [ brand, dates ] = item; /** {'2022-1-10': array } */
   let sorted = Object.entries(dates).sort((a,b) => new Date(a[0]) - new Date(b[0]))
   /** find value count for each sorted date */
   
   /** find if the dates are consecutive */

   /** build new object with result property */ 

})

1
  • How does your attempt relate to what you want done? Commented Jan 24, 2022 at 20:19

2 Answers 2

1

I think finding out the date pairs before counting would be more efficient, I think this should solve the problem:

const data = {
  'addias': {
    '2022-1-10': ['a', 'b', 'c', 'd', 'e', 'f', 'g'],
    '2022-1-09': ['a', 'b', 'c', 'd', 'e', 'f', 'g'],
    '2021-12-29': ['a'],
    '2021-12-28': ['y', 'u', 'v', 'd', 'f', 'f', 'g'],
    '2021-1-08': ['x', 't', 's', 'd', 'e', 'f', 'g'],
  },
  'nike': {
    '2022-1-20': ['a', 'b', 'c', 'd', 'e', 'f', 'g'],
    '2022-1-15': ['a', 'b', 'c'],
    '2022-1-14': ['a', 'b'],
    '2022-1-10': ['y', 'b', 'x', 'z', 'e', 'f', 'g'],
  }
};

function maxValueBetweenConsecutiveDates(input) {
  const result = {};
  for (const brand in input) {
    const dates = Object
      .keys(input[brand])
      .map(e => ({
        date: new Date(e),
        original: e
      }))
      .sort((a, b) => a.date - b.date);

    const pairs = [];

    for (let i = 1; i < dates.length; i++) {
      if (dates[i].date - dates[i - 1].date === 86400000) {
        pairs.push([dates[i - 1], dates[i]]);
      }
    }

    const quantities = pairs.map(pair => ({
      firstDate: pair[0].original,
      secondDate: pair[1].original,
      total: input[brand][pair[0].original].length + input[brand][pair[1].original].length
    }));
    const max = Math.max(...quantities.map(e => e.total));
    const quantity = quantities.find(e => e.total === max);

    result[brand] = quantity;
  }

  return result;
}

console.log(maxValueBetweenConsecutiveDates(data));

Sign up to request clarification or add additional context in comments.

Comments

1

You could do it like this, altough you might want to write a custom sorting function

const data = {
    'addias':{
        '2022-1-10': ['a', 'b', 'c', 'd', 'e', 'f', 'g'],
        '2022-1-09': ['a', 'b', 'c', 'd', 'e', 'f', 'g'],
        '2021-12-29': ['a'],
        '2021-12-28': ['y', 'u', 'v', 'd', 'f', 'f', 'g'],
        '2021-1-08': ['x', 't', 's', 'd', 'e', 'f', 'g'],
    },
    'nike':{
        '2022-1-20': ['a', 'b', 'c', 'd', 'e', 'f', 'g'],
        '2022-1-15': ['a', 'b', 'c'],
        '2022-1-14': ['a', 'b'],
        '2022-1-10': ['y', 'b', 'x', 'z', 'e', 'f', 'g'],
    }
}

const result = {};

Object.keys(data).forEach(sneaker => {
  const dates = Object.keys(data[sneaker]);
  dates.sort();
  result[sneaker] = {
    firstDate: dates[0],
    secondDate: dates[1],
    total: dates.length
  };
});

console.log(result);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.