1

I have an array of objects containing a "year" key and a "date" key like so:

    var fullDates = [
{ 'year': '2018', date: '2018-01'},
{ 'year': '2017', date: '2017-02'},
{ 'year': '2016', date: '2016-03'},
{ 'year': '2018', date: '2018-04'},
{ 'year': '2017', date: '2017-05' },
{ 'year': '2016', date: '2016-06'}
];

I want to make a new array of objects containing the year and the dates but where the "date" key is an array like shown below. How can i do this?

var final = [
{ 'year': '2018', date: ['2018-01', '2018-02']},
{ 'year': '2017', date: ['2017-03', '2017-04']},
{ 'year': '2016', date: ['2016-04', '2016-06']}
]
2

3 Answers 3

1

You can use Array.reduce() operation to get that output:

var fullDates = [
  { 'year': '2018', date: '2018-01'},
  { 'year': '2017', date: '2017-02'},
  { 'year': '2016', date: '2016-03'},
  { 'year': '2018', date: '2018-04'},
  { 'year': '2017', date: '2017-05' },
  { 'year': '2016', date: '2016-06'}
];
var final = fullDates.reduce((acc, obj)=>{
  var existingObj = acc.find(item => item.year === obj.year);
  if(existingObj){
    existingObj.date.push(obj.date);
    return acc;
  }
  obj.date = [obj.date];
  acc.push(obj);
  return acc;
}, []);
console.log(final);

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

2 Comments

hehe, same approach. beaten by 2min :). The only difference is that i don't change current object.
@fredd5229 glad to help.
1

You can use array#reduce.

For each year, create an object containing year and dates properties and for each repeating year add date in the array. Then get all the values from this object accumulator.

const fullDates = [ { 'year': '2018', date: '2018-01'}, { 'year': '2017', date: '2017-02'}, { 'year': '2016', date: '2016-03'}, { 'year': '2018', date: '2018-04'}, { 'year': '2017', date: '2017-05' }, { 'year': '2016', date: '2016-06'} ],
    result = Object.values(fullDates.reduce((r, {year, date}) => {
      r[year] = r[year] || {year, dates : []};
      r[year].dates.push(date);
      return r;
    },{}));
console.log(result);

Comments

0

Hope this helps:

    var fullDates = [
{ 'year': '2018', date: '2018-01'},
{ 'year': '2017', date: '2017-02'},
{ 'year': '2016', date: '2016-03'},
{ 'year': '2018', date: '2018-04'},
{ 'year': '2017', date: '2017-05' },
{ 'year': '2016', date: '2016-06'}
];



const reducer = (accumulator, currentValue) => {

    let yearData = accumulator.find(x => x.year == currentValue.year);
    if(yearData !== undefined){
        yearData.date.push(currentValue.date);
    }
    else{
        accumulator.push({ 'year': currentValue.year, date: [currentValue.date]});
    }


    return accumulator;
}


var final = fullDates.reduce(reducer, []);

2 Comments

Your approach will need to know all the keys of the object in fullDates as you are creating a new object so that will add complexity if there are many properties in object.
That is absolutely true.

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.