-1

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','2021/07/07']

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'},
{date: '2021/07/01', company: 'DEF', sales: '32460'},
{date: '2021/07/02', company: 'DEF', sales: '28165'},
{date: '2021/07/03', company: 'DEF', sales: '31546'},
{date: '2021/07/04', company: 'DEF', sales: '12654'},
{date: '2021/07/05', company: 'DEF', sales: '26457'},
{date: '2021/07/06', company: 'DEF', sales: '20351'},
{date: '2021/07/07', company: 'DEF', sales: '56404'},
]

What I need to do is only extract the sales in the jsonSalesData array for each company ONLY if the date matches the salesDates list.

Additionally, I need to set the sales to equal to 0 if it isn't found in that array.

For example, I'd like to have {date: '2021/07/06', company: 'ABC', sales: '20351'} returned, but not

{date: '2021/07/01', company: 'ABC', sales: '32460'} because 2021/07/01 does not exist in salesDates

I've tried nesting for loops and such but cannot figure this out.

Spectric
  • 30,714
  • 6
  • 20
  • 43
Joey
  • 613
  • 1
  • 6
  • 17
  • thanks @HereticMonkey it looks like you were the first to find this. I failed to mention in my original post that if the date doesn't match in the salesDates array, I need to set the `sales` to 0. – Joey Jul 15 '21 at 19:12
  • Sounds like a different question, but you'd use similar logic; `var salesData = jsonSalesData.map(datum => salesDates.includes(datum.date) ? datum : { ...datum, sales: '0' });` – Heretic Monkey Jul 15 '21 at 19:17
  • BTW, it's not good manners to change a question once it's been answered. – Heretic Monkey Jul 15 '21 at 19:19

2 Answers2

-1

Please try this:

jsonSalesData.map(data => {
    (!salesDates.includes(data.date))&&(data.sales = '0')
    return data;
})
camper
  • 51
  • 8
  • 1
    Answers which explain code rather than simply plopping the code down with "try this" are more often upvoted that those that do. Also, please try to use professional English, not txtspk like "plz". Finally, if you see a comment that starts with "Does this answer your question?" and a link to another question, take a moment to follow that link and see if any of the answers there answer this question. If so, you do not need to answer this question -- it's already been answered. – Heretic Monkey Jul 15 '21 at 19:07
  • HI @camper thanks. I added an if statement to account for each company -- is there a way I can set the sales to 0 if the date doesn't match? – Joey Jul 15 '21 at 19:07
  • @Heretic Monkey, Thanks for your advice. – camper Jul 15 '21 at 19:10
  • @Joey I've updated my code. Hope you kindly check. – camper Jul 15 '21 at 19:16
  • @camper -- this set all the sales to 0 – Joey Jul 15 '21 at 19:53
-1

Loop through salesDates and inside the loop loop through jsonSalesData. Inside the loop, check whether the date property is equal to the current salesDates item, and if so, add to the array.

Store whether a sales item with the same date was found in a variable and set this to true if one is found. If one is not found, we construct a new JSON with the sales property set to 0, and add it to the array

const salesDates = ['2021/07/04', '2021/07/05', '2021/07/05', '2021/07/06', '2021/07/07']

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"},{date:"2021/07/01",company:"DEF",sales:"32460"},{date:"2021/07/02",company:"DEF",sales:"28165"},{date:"2021/07/03",company:"DEF",sales:"31546"},{date:"2021/07/04",company:"DEF",sales:"12654"},{date:"2021/07/05",company:"DEF",sales:"26457"},{date:"2021/07/06",company:"DEF",sales:"20351"},{date:"2021/07/07",company:"DEF",sales:"56404"}];

const n = [];
salesDates.forEach((e)=>{
  var found = false;
  jsonSalesData.forEach(f => f.date == e? n.push(f) : '');
  if(!found) n.push({date: e, company:'ABC', sales:0})
})

console.log(n);
Spectric
  • 30,714
  • 6
  • 20
  • 43
  • The OP has admitted this was a duplicate. – Heretic Monkey Jul 15 '21 at 19:18
  • hi @spectric -- I ran that code but it's returning objects with dates that do not exist in the salesDates array e.g. ({ "date": "2021/07/02", "company": "ABC", "sales": "28165" },) – Joey Jul 15 '21 at 19:48
  • thanks @spectric it's returning all the objects that match the dates in `salesDates`, however if there's a date in `salesDates` but not in `jsonSalesData`, i need the object returned with the date not matched and a sales of 0. For example if '2021/06/29' exists in `salesDates` but does not exist in the jsonSalesData, i need to return the object with a sales of 0 -- {date: '2021/06/29', company: 'ABC', sales: 0} – Joey Jul 15 '21 at 20:27