# Compare values and date

I have an array like:

[
'48.02625820568998,11/10/2020,0',
'58.12225223768978,11/11/2020,0' ,
'55.237285833568328,11/12/2020,0' ,
'121.634689831538,11/10/2020,1' ,
'175.489472549372,11/11/2020,1'
]

and I’m trying to compare values from the day before if it’s in the same category to see if the value went up or down and by how much.

The first point is the value, the second the data and the third the category.

So 48.02625820568998 on November 10,2020 in category 0, would be compared to 58.12225223768978 on November 11, 2020 in category 0, to see what the difference is between them.

However I’m not sure how I can extract the values and compare them.

Any ideas would be great, thanks!

@yjay hey,

Try this out it, is this the results your looking for?

const arr = [
'48.02625820568998,11/10/2020,0',
'58.12225223768978,11/11/2020,0',
'55.237285833568328,11/12/2020,0',
'121.634689831538,11/10/2020,1',
'175.489472549372,11/11/2020,1'
]

const difference = (a, b) => Math.abs(a - b)

const results = []
let yesterdayItem

arr.forEach(item => {

const [value, date, category] = item.split(',')

if (yesterdayItem) {

const [yesterdayValue, yesterdayDate, yesterdayCategory] = yesterdayItem.split(',')

if (yesterdayCategory === category) {
results.push({ category: category, compare: `\${yesterdayDate} - \${date}`, difference: difference(yesterdayValue, value) })
}
}
yesterdayItem = item

})

console.log(results)
1 Like

@biscuitmanz this is fantastic, thanks so much!!

1 Like