How to format dates in an array - sanity

hello guys need help does anyone know how to format an array of dates. but just want it returns the year.
an observation the words are in Portugues so inicio and fim translating is start and end
const rowData = dicisionData.map(item => ({
IndicadoresChave: item.indicadores_chave,
Total: item.total,
Femenino: item.genero.femenino,
Masculino: item.genero.masculino,
ICF: item.icf,
Data: [item.data.inicio, item.data.fim],
Fonte: item.fonte
}))
the result

Related

vuejs separate money with commas and periods

hello in vuejs I want to separate the amount of money with a comma and a period, how can I do it with filter?
I want the currency to be like this.
<p>1.000<span>,00</span></p>
I want the part separated by comma to be gray like in the image
Vue.filter('toTL', function (value) {
return new Intl.NumberFormat('tr-TR', { currency: 'TRY', minimumFractionDigits: 2}).format(value);
});
An easy solution would be to let the filter output the HTML:
<p class="amount" v-html="$options.filters.toTL(attributes.gross_total)" />
The filter can be written like so:
Vue.filter('toTL', function (value) {
let formatted = new Intl.NumberFormat('tr-TR', { currency: 'TRY', minimumFractionDigits: 2}).format(value);
let arr = formatted.split(',');
return arr[0] + '<span>,' + arr[1] + '</span>';
});
Links:
String.prototype.split documentation:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split
Also see this StackOverflow question:
VueJS2 v-html with filter

How to sort flatlist in React Native?

I have data stored in a state that is shown in flatlist, I want to sort the data based on ratings. So if I click on sort button they should be sorted in ascending order and when I click again, they should be sorted in descending order.
I have an array of objects stored in state, below is just a piece of data that is important.
show_data_list = [{ ratings : { overall : 4, ...other stuff } } ]
Is there a way I could do it, I tried using the map function which sorts array
list.map((a,b) => a-b)
But how can I use this to sort my array of objects, I cant pass in 2 item.rating.overall as the parameter.
Thanks in advance for the help :)
You can use javascript's built in sort function. You can provide a custom comparer function for it, which should return a negative value if the first item takes precedence, 0 if they are the same and a positive value if the second value should take precedence.
show_data_list.sort((a, b) => { return a.ratings.overall - b.ratings.overall; }). This will sort the data in the ascending order.
show_data_list.sort((a, b) => { return b.ratings.overall - a.ratings.overall; }). This will sort it in the descending order.
This is how I solved it stored the data in a variable and then sorted it based on condition
let rating = this.state.show_data_list;
rating.sort(function(a,b) {
return a.ratings.Overall < b.ratings.Overall
Try This
Sort your List Ascending/ Descending order with name or other string value in list
const [productSortList, setProductSortList] = useState(productarray);
where productarray is your main array
Ascending
productarray.sort((a, b) => a.products_name < b.products_name)
Descending
productarray.sort((a, b) => b.products_name < a.products_name),
You can reset the state of array you have passed as data in Flatlist
ascending ->
setProductSortList(
productarray.sort((a, b) => a.products_name < b.products_name),
);
do same for descending

how to sum from array of object using lodash only?

hey I want to calculate the sum of one property from array of object using lodash
suppose the array of object looks like this ...
salary":[{
"bills":[{"electricity":300,"milk":500},
{"electricity":240,"milk":200},
{"electricity":800,"milk":900}]
}]
I want to calculate the sum of 'milk' from that object using lodash.
Use nested _.sumBy() calls. The internal gets the sum of milk from one salary, and the external sums all salaries:
const data = {"salary":[{"bills":[{"electricity":300,"milk":500},{"electricity":240,"milk":200},{"electricity":800,"milk":900}]}]}
const result = _.sumBy(data.salary, ({ bills }) => _.sumBy(bills, 'milk'))
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>
Something like this
const bills = salary.map((s)=> s.bills)
_(bills).map((objs,key)=>({
'milk': _.sumBy(objs, 'milk')
})).value

Why is my 'WHERE' clause based on a 'DATE' failing?

I'm using node.js to connect to an SQL database (SQL Server 2016 specifically). My table, called transactionCounts, has the following tables and data types:
staff_id: varchar(50), date: Date, count: int;
The 'date' field is just a date, not a DateTime, for clarity. Records look like this: "2017-08-07"
I'm using the mssql package, const sql = require('mssql');
So basically, I have a function which accepts a start date and an end date and does this with them:
function(start, end) {
let ps = new sql.PreparedStatement(transactionRegisterSqlPool);
ps.input('start', sql.Date);
ps.input('end', sql.Date);
ps.prepare('SELECT staff_id, SUM(Count) TotalCount FROM [TransactionRegister].[dbo].[transactionCounts] ' +
'WHERE date >= #start AND date < #end GROUP BY staff_id', err => {
.execute({start: start, end: end}, (err, result) => {});
});
};
I've simplified the function for illustrations sake (it normally returns a promise), but here's what's going wrong:
I pass in the dates of Aug 20 midnight, and Aug 27 midnight, and what I'm expecting to get back is the sum for the dates 20,21,22,23,24,25 and 26 naturally (7 days, a week).
26th isn't being included though (definitely), and I'm not entirely sure but I'd wager that the 19th is being included. I think it's a daylight-savings issue, because these dates, when i call .toISOString(), look like 2017-08-19T23:00:00.000Z and 2017-08-26T23:00:00.000Z respectively (11pm the night prior).
I've modified my function to use strings instead of dates, and this seems to work and returns the correct Sums:
function(start, end) {
let ps = new sql.PreparedStatement(transactionRegisterSqlPool);
ps.input('start', sql.VarChar);
ps.input('end', sql.VarChar);
start = `${start.getFullYear()}/${start.getMonth() + 1}/${start.getDate()}`;
end = `${end.getFullYear()}/${end.getMonth() + 1}/${end.getDate()}`;
ps.prepare('SELECT staff_id, SUM(Count) TotalCount FROM [TransactionRegister].[dbo].[transactionCounts] ' +
'WHERE date >= #start AND date < #end GROUP BY staff_id', err => {
ps.execute({start: start, end: end}, (err, result) => {});
});
};
But it seems...wrong to take my dates and turn them into strings to work my way around this issue. What is the correct way to deal with dates between Javascript Dates and SQL Dates, so that this apparent Daylight-Savings-caused issue is avoided?
Your problem is that JavaScript does not have a "date" type, only "datetime", yet SQL does have a "date" type. Because of that, you will have to do the conversion.
If you wrap it in a function, it is still readable:
function toDateString(d) {
return `${d.getFullYear()}/${d.getMonth() + 1}/${d.getDate()}`;
}
ps.prepare('SELECT staff_id, SUM(Count) TotalCount FROM [TransactionRegister].[dbo].[transactionCounts] ' +
'WHERE date >= #start AND date < #end GROUP BY staff_id', err => {
ps.execute({start: toDateString(start), end: toDateString(end)}, (err, result) => {});
});

Optimize SQL query that retrieves count of items over a period

Is there a way to better optimize the query in this function? I am wanting it to only make a single SQL query if possible. The code picks up the number of events generated over 2 weeks. Thanks.
def items_chart_data
#current_student = Student.find(current_user.student_id)
(2.weeks.ago.to_date..DateTime.now).map do |date|
{
created_at: date,
item_count: Item.where("date(created_at) = ? AND student_id = ?", date, #current_student.id).count
}
end
end
You can do it in one query, like this:
items = Item.select('date(created_at) as date_created, count(id) as id_count').
where('student_id = ? and created_at >= ?', current_user.student_id, 2.weeks.ago.beginning_of_day).
group('date(created_at)').map do |item|
{ created_at: item.date_created, item_count: item.id_count }
end
To get the missing dates with 0 items into your array you can do this:
(2.weeks.ago.to_date..Date.current).each do |date|
date = date.strftime('%Y-%m-%d')
unless items.any? { |h| h.value?(date) } # Check if date exists already
items << { created_at: date, item_count: 0 } # Add item_count: 0 if not
end
end
items.sort_by! { |h| h[:created_at] } # Put array in correct order
Note that adding the zero item days to the array doesn't perform any queries. The array items contains only 14 elements, so (especially compared to querying the database 14 times) this code should be really fast.