0

Had my earlier question closed because I left out an important detail.

Say I have the following two Arrays

const salesDates = ['2021/06/30','2021/07/01','2021/07/02','2021/07/03','2021/07/04','2021/07/05','2021/07/06']

and

const jsonSalesData = [
         {date: '2021/07/01', company: 'ABC', sales: '32460'},
         {date: '2021/07/02', company: 'ABC', sales: '28165'},
         {date: '2021/07/03', company: 'ABC', sales: '31546'},
         {date: '2021/07/04', company: 'ABC', sales: '12654'},
         {date: '2021/07/05', company: 'ABC', sales: '26457'},
         {date: '2021/07/06', company: 'ABC', sales: '20351'},
         {date: '2021/07/07', company: 'ABC', sales: '56404'},
]

What I need to do is

  1. Return the objects from jsonSalesData if the date in jsonSalesData matches the salesDates array
  2. If there is a date in salesDates that is not in jsonSalesData add an object to the jsonSalesData with that date and set the sales to 0.

Desired Output

[
{date: '2021/06/30', company: 'ABC', sales: '0'},
{date: '2021/07/01', company: 'ABC', sales: '32460'},
{date: '2021/07/02', company: 'ABC', sales: '28165'},
{date: '2021/07/03', company: 'ABC', sales: '31546'},
{date: '2021/07/04', company: 'ABC', sales: '12654'},
{date: '2021/07/05', company: 'ABC', sales: '26457'},
{date: '2021/07/06', company: 'ABC', sales: '20351'},
]

notice the addition of the 1st object and the exclusion of the last object

I've tried to use forEach but it only accomplishes #1 (returns the dates in jsonSalesData which match to salesDates). It doesn't add the object if there is no match.

const n = [];
jsonSalesData.forEach(e => {
    if (salesDates.includes(e.date)){ 
      n.push(e);
  }
})
console.log(n);

I've also tried nested loops, but I can't seem to figure it out. Here's what I did for those:

    for (let i = 0; i < jsonSalesData.length; i++){
        for (let j = 0; j < salesData.length; j++){     
        if (jsonSalesData[i].date === salesData[j]){
console.log(jsonSalesData[i].company,jsonSalesData[i].date,jsonSalesData[i].sales)
        }
    
      }
    }

Same issues with map and filter -- I can't seem to look back at salesData and add an object back to jsonSalesData if something in salesData doesn't exist in jsonSalesData

7
  • 1
    How do you know the company for 6/30? Commented Jul 15, 2021 at 21:48
  • Because there is a 2021/06/30 date in salesDates but not a 2021/06/30 in jsonSalesData. Therefore, it was added with a 0 sales. Commented Jul 15, 2021 at 21:50
  • 1
    salesDates.map(date => jsonSalesData.find(row=> row.date===date) || { date, sales: 0 }); Commented Jul 15, 2021 at 21:51
  • 1
    How do you know that the company is ABC for 6/30? Commented Jul 15, 2021 at 21:52
  • 1
    Show us what you have tried, or where you are stuck. SO is not a coding service. You can visit the help center, and take the tour to see what and How to Ask. Commented Jul 15, 2021 at 21:55

1 Answer 1

2

if you don't care about adding the company name arbitrarily, you could do something like this

const salesDates = ['2021/06/30','2021/07/01','2021/07/02','2021/07/03','2021/07/04','2021/07/05','2021/07/06']
 
const jsonSalesData = [
         {date: '2021/07/01', company: 'ABC', sales: '32460'},
         {date: '2021/07/02', company: 'ABC', sales: '28165'},
         {date: '2021/07/03', company: 'ABC', sales: '31546'},
         {date: '2021/07/04', company: 'ABC', sales: '12654'},
         {date: '2021/07/05', company: 'ABC', sales: '26457'},
         {date: '2021/07/06', company: 'ABC', sales: '20351'},
         {date: '2021/07/07', company: 'ABC', sales: '56404'},
]

let updatedSalesData = salesDates.map(date => jsonSalesData.find(item=> item.date==date) || { date, company: 'ABC', sales: '0' }); 

console.log(updatedSalesData)

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

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.