1

I have two arrays of objects

First, one obtained from the database

const data = [{
    count: 156,
    monthCount: 1,
    year: 2018
  },
  {
    count: 165,
    monthCount: 2,
    year: 2018
  },
  {
    count: 153,
    monthCount: 3,
    year: 2018
  },
  {
    count: 63,
    monthCount: 6,
    year: 2018
  },
  {
    count: 4,
    monthCount: 9,
    year: 2018
  },
  {
    count: 116,
    monthCount: 10,
    year: 2018
  }
]

And second is my dummy data

const dummyData = [{
    count: 0,
    monthCount: 1,
    year: 2018
  },
  {
    count: 0,
    monthCount: 2,
    year: 2018
  },
  {
    count: 0,
    monthCount: 3,
    year: 2018
  },
  {
    count: 0,
    monthCount: 4,
    year: 2018
  },
  {
    count: 0,
    monthCount: 5,
    year: 2018
  },
  {
    count: 0,
    monthCount: 6,
    year: 2018
  },
  {
    count: 0,
    monthCount: 7,
    year: 2018
  },
  {
    count: 0,
    monthCount: 8,
    year: 2018
  },
  {
    count: 0,
    monthCount: 9,
    year: 2018
  },
  {
    count: 0,
    monthCount: 10,
    year: 2018
  },
  {
    count: 0,
    monthCount: 11,
    year: 2018
  },
  {
    count: 0,
    monthCount: 12,
    year: 2018
  }
]

I need to add missing values inside my data array comparing with the month of dummy array.

I have tried using lodash but could not get what I needed.

var diffArr =  _.differenceWith(obj.data, 'monthCount', numberArr, 'monthCount', _.isEqual)

Thank you in advance.

1
  • 1
    What is missing data here ? Is it based on monthCount ? Commented Oct 19, 2019 at 8:31

5 Answers 5

2

You can map your dummyData items and replace them if a corresponding item exists for the same monthCount in the real data array:

const data = [{
    count: 156,
    monthCount: 1,
    year: 2018
  },
  {
    count: 165,
    monthCount: 2,
    year: 2018
  },
  {
    count: 153,
    monthCount: 3,
    year: 2018
  },
  {
    count: 63,
    monthCount: 6,
    year: 2018
  },
  {
    count: 4,
    monthCount: 9,
    year: 2018
  },
  {
    count: 116,
    monthCount: 10,
    year: 2018
  }
]

const dummyData = [{
    count: 0,
    monthCount: 1,
    year: 2018
  },
  {
    count: 0,
    monthCount: 2,
    year: 2018
  },
  {
    count: 0,
    monthCount: 3,
    year: 2018
  },
  {
    count: 0,
    monthCount: 4,
    year: 2018
  },
  {
    count: 0,
    monthCount: 5,
    year: 2018
  },
  {
    count: 0,
    monthCount: 6,
    year: 2018
  },
  {
    count: 0,
    monthCount: 7,
    year: 2018
  },
  {
    count: 0,
    monthCount: 8,
    year: 2018
  },
  {
    count: 0,
    monthCount: 9,
    year: 2018
  },
  {
    count: 0,
    monthCount: 10,
    year: 2018
  },
  {
    count: 0,
    monthCount: 11,
    year: 2018
  },
  {
    count: 0,
    monthCount: 12,
    year: 2018
  }
]

const allData = dummyData.map(dummyItem => (
  data.find(item => item.monthCount === dummyItem.monthCount) || dummyItem
))

console.log(allData);

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

1 Comment

@AshishBhangade if you see a answer solved your question you should mark as correct answer, What should I do when someone answers my question?
0

You could create a new array and check if the actual mont exist or take a single dummy object with an adjusted monthCount.

var data = [{ count: 156, monthCount: 1, year: 2018 }, { count: 165, monthCount: 2, year: 2018 }, { count: 153, monthCount: 3, year: 2018 }, { count: 63, monthCount: 6, year: 2018 }, { count: 4, monthCount: 9, year: 2018 }, { count: 116, monthCount: 10, year: 2018 }],
    dummy = { count: 0, monthCount: 0, year: 2018 },
    result = Array.from(
        { length: 12 },
        (i => (_, j) => data[i] && data[i].monthCount === j + 1
            ? data[i++]
            : { ...dummy, monthCount: j + 1 }
        )(0)
    );

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Comments

0

Merge Object with same month count key.

Please check out this simple solution:

const data = [{
    count: 156,
    monthCount: 1,
    year: 2018
  },
  {
    count: 165,
    monthCount: 2,
    year: 2018
  },
  {
    count: 153,
    monthCount: 3,
    year: 2018
  },
  {
    count: 63,
    monthCount: 6,
    year: 2018
  },
  {
    count: 4,
    monthCount: 9,
    year: 2018
  },
  {
    count: 116,
    monthCount: 10,
    year: 2018
  }
]

const dummyData = [{
    count: 0,
    monthCount: 1,
    year: 2018
  },
  {
    count: 0,
    monthCount: 2,
    year: 2018
  },
  {
    count: 0,
    monthCount: 3,
    year: 2018
  },
  {
    count: 0,
    monthCount: 4,
    year: 2018
  },
  {
    count: 0,
    monthCount: 5,
    year: 2018
  },
  {
    count: 0,
    monthCount: 6,
    year: 2018
  },
  {
    count: 0,
    monthCount: 7,
    year: 2018
  },
  {
    count: 0,
    monthCount: 8,
    year: 2018
  },
  {
    count: 0,
    monthCount: 9,
    year: 2018
  },
  {
    count: 0,
    monthCount: 10,
    year: 2018
  },
  {
    count: 0,
    monthCount: 11,
    year: 2018
  },
  {
    count: 0,
    monthCount: 12,
    year: 2018
  }
]

const resultData = dummyData.map((d, index)=>({...d, ...data.find(k=>k.monthCount===d.monthCount)}));
    console.log(resultData);

Thanks

2 Comments

I do not think the indexes are the same. dummyData's purpose is more to fill in the holes in the real data, as I understood it.
Got your point. I didn't understood monthCount relevance at first instance. Thanks
0

Easy and simple way to do that would be to map the dummyData array, check if the object exists in the data array from your database.

Ifi it exists, we return the found object, else we return the dummy oject :)

const data=[{count:156,monthCount:1,year:2018},{count:165,monthCount:2,year:2018},{count:153,monthCount:3,year:2018},{count:63,monthCount:6,year:2018},{count:4,monthCount:9,year:2018},{count:116,monthCount:10,year:2018}];
const dummyData=[{count:0,monthCount:1,year:2018},{count:0,monthCount:2,year:2018},{count:0,monthCount:3,year:2018},{count:0,monthCount:4,year:2018},{count:0,monthCount:5,year:2018},{count:0,monthCount:6,year:2018},{count:0,monthCount:7,year:2018},{count:0,monthCount:8,year:2018},{count:0,monthCount:9,year:2018},{count:0,monthCount:10,year:2018},{count:0,monthCount:11,year:2018},{count:0,monthCount:12,year:2018}];

const myMergedData = dummyData.map(dummyDataItem=>{
    var found = false;
    for(var i = 0; i < data.length; i++) 
    {
        if(data[i].monthCount === dummyDataItem.monthCount)
        {
            found = data[i];
            break;
        }
    }
    
    //if already in original, return the original
    //else return the dummy one ;)
    if(found) return found; 
    else return dummyDataItem;
});

console.log('Your new array =>', myMergedData);

Comments

0

Finally, I got this simplest method using lodash:-

var diffArr = _.unionBy(data, dummyData, 'monthCount')
console.log(diffArr)

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.