JS How can I convert a serialized object to an Array? - javascript

Using jquery.nestable.min.js, I get a the following output:
[{"id":32},{"id":29},{"id":30}]
This current output is coming from the following code:
const myList = JSON.stringify(list.nestable('serialize'));
I need it to simply be:
32, 29, 30
End result is that I would like to read a normal array in PHP. I'm looking to either convert myList to an array in Javascript and POST that array, or convert the current version of the object to an array inside PHP, whichever is most effective.
I've tried to use json_decode in PHP, but I get empty values. So I figured if I can just convert to a normal Array before sending it off to PHP, then it would be less of a hassle.
Thank you.
This may be duplicated, in which case, please point me to the best answer

Depending on whether you want an array (per your title), or literally 32, 29, 30 per your post:
console.log([{"id":32},{"id":29},{"id":30}].map(i => i.id))
console.log([{"id":32},{"id":29},{"id":30}].map(i => i.id).join(', '))
Which, with your example, is probably going to be:
const myList = JSON.stringify(list.nestable('serialize').map(i => i.id));

If you just want an array of numbers, convert that output array of objects to array of numbers
var list = [{"id":32}, {"id":29}, {"id":30}];
var required = list.map(item => item.id);
console.log(required);

Related

In a JSON object, how do I retrieve values of objects within a JSON object without knowing the key?

In the following API response, I have an object of time_entries, each having an ID as a key.
{"count":5,"results":[{"key":"time_entries","id":"1522040735"},{"key":"time_entries","id":"1522040395"},{"key":"time_entries","id":"1517855265"},{"key":"time_entries","id":"1517854385"},{"key":"time_entries","id":"1517853315"}],"time_entries":{"1522040735":{"created_at":"2020-06-22T13:54:54-07:00","updated_at":"2020-06-22T13:54:54-07:00","date_performed":"2020-06-22","time_in_minutes":75,"billable":true,"notes":"","rate_in_cents":25000,"cost_rate_in_cents":null,"currency":"USD","currency_symbol":"$","currency_base_unit":100,"user_can_edit":false,"approved":false,"taxable":false,"is_invoiced":true,"story_id":"569297425","workspace_id":"31689165","user_id":"13403745","active_submission_id":null,"recent_submission_id":null,"id":"1522040735"},"1522040395":{"created_at":"2020-06-22T13:54:42-07:00","updated_at":"2020-06-22T13:54:42-07:00","date_performed":"2020-06-22","time_in_minutes":90,"billable":true,"notes":"","rate_in_cents":25000,"cost_rate_in_cents":null,"currency":"USD","currency_symbol":"$","currency_base_unit":100,"user_can_edit":false,"approved":false,"taxable":false,"is_invoiced":true,"story_id":"569297345","workspace_id":"31689165","user_id":"13403745","active_submission_id":null,"recent_submission_id":null,"id":"1522040395"},"1517855265":{"created_at":"2020-06-19T10:32:50-07:00","updated_at":"2020-06-19T10:32:50-07:00","date_performed":"2020-06-19","time_in_minutes":60,"billable":true,"notes":"","rate_in_cents":25000,"cost_rate_in_cents":null,"currency":"USD","currency_symbol":"$","currency_base_unit":100,"user_can_edit":false,"approved":false,"taxable":false,"is_invoiced":true,"story_id":"568676695","workspace_id":"31689165","user_id":"13403745","active_submission_id":null,"recent_submission_id":null,"id":"1517855265"},"1517854385":{"created_at":"2020-06-19T10:32:12-07:00","updated_at":"2020-06-19T10:32:12-07:00","date_performed":"2020-06-19","time_in_minutes":120,"billable":true,"notes":"","rate_in_cents":25000,"cost_rate_in_cents":null,"currency":"USD","currency_symbol":"$","currency_base_unit":100,"user_can_edit":false,"approved":false,"taxable":false,"is_invoiced":true,"story_id":"568676755","workspace_id":"31689165","user_id":"13403745","active_submission_id":null,"recent_submission_id":null,"id":"1517854385"},"1517853315":{"created_at":"2020-06-19T10:31:40-07:00","updated_at":"2020-06-19T10:31:40-07:00","date_performed":"2020-06-19","time_in_minutes":180,"billable":true,"notes":"","rate_in_cents":25000,"cost_rate_in_cents":null,"currency":"USD","currency_symbol":"$","currency_base_unit":100,"user_can_edit":false,"approved":false,"taxable":false,"is_invoiced":true,"story_id":"568676765","workspace_id":"31689165","user_id":"13403745","active_submission_id":null,"recent_submission_id":null,"id":"1517853315"}},"meta":{"count":5,"page_count":1,"page_number":1,"page_size":20},"stories":{"569297425":{"title":"task5","description":"task5desc","updated_at":"2020-06-22T13:56:41-07:00","assignment_timestamped_at":"2020-06-22T13:56:41-07:00","created_at":"2020-06-22T13:54:08-07:00","due_date":null,"start_date":null,"story_type":"task","state":"started","position":99960000,"archived":false,"deleted_at":null,"sub_story_count":0,"percentage_complete":0,"priority":"normal","has_proofing_access":false,"ancestor_ids":[],"subtree_depth":0,"ancestry_depth":0,"time_trackable":true,"time_estimate_in_minutes":null,"logged_billable_time_in_minutes":75,"logged_nonbillable_time_in_minutes":0,"sub_stories_time_estimate_in_minutes":null,"sub_stories_billable_time_in_minutes":null,"weight":null,"budget_estimate_in_cents":null,"budget_used_in_cents":31250,"uninvoiced_balance_in_cents":0,"invoiced_balance_in_cents":31250,"sub_stories_budget_estimate_in_cents":null,"sub_stories_budget_used_in_cents":null,"fixed_fee":false,"billable":true,"workspace_id":"31689165","creator_id":"13403745","parent_id":null,"root_id":null,"id":"569297425"},"569297345":{"title":"task4","description":"task4desc","updated_at":"2020-06-22T13:56:41-07:00","assignment_timestamped_at":"2020-06-22T13:56:41-07:00","created_at":"2020-06-22T13:53:55-07:00","due_date":null,"start_date":null,"story_type":"task","state":"started","position":99970000,"archived":false,"deleted_at":null,"sub_story_count":0,"percentage_complete":0,"priority":"normal","has_proofing_access":false,"ancestor_ids":[],"subtree_depth":0,"ancestry_depth":0,"time_trackable":true,"time_estimate_in_minutes":null,"logged_billable_time_in_minutes":90,"logged_nonbillable_time_in_minutes":0,"sub_stories_time_estimate_in_minutes":null,"sub_stories_billable_time_in_minutes":null,"weight":null,"budget_estimate_in_cents":null,"budget_used_in_cents":37500,"uninvoiced_balance_in_cents":0,"invoiced_balance_in_cents":37500,"sub_stories_budget_estimate_in_cents":null,"sub_stories_budget_used_in_cents":null,"fixed_fee":false,"billable":true,"workspace_id":"31689165","creator_id":"13403745","parent_id":null,"root_id":null,"id":"569297345"},"568676695":{"title":"task1","description":"desc1","updated_at":"2020-06-22T13:56:40-07:00","assignment_timestamped_at":"2020-06-22T13:56:40-07:00","created_at":"2020-06-19T10:26:09-07:00","due_date":null,"start_date":"2020-06-19","story_type":"task","state":"completed","position":100000000,"archived":false,"deleted_at":null,"sub_story_count":0,"percentage_complete":100,"priority":"normal","has_proofing_access":false,"ancestor_ids":[],"subtree_depth":0,"ancestry_depth":0,"time_trackable":true,"time_estimate_in_minutes":120,"logged_billable_time_in_minutes":60,"logged_nonbillable_time_in_minutes":0,"sub_stories_time_estimate_in_minutes":null,"sub_stories_billable_time_in_minutes":null,"weight":null,"budget_estimate_in_cents":10000,"budget_used_in_cents":30000,"uninvoiced_balance_in_cents":0,"invoiced_balance_in_cents":30000,"sub_stories_budget_estimate_in_cents":null,"sub_stories_budget_used_in_cents":null,"fixed_fee":false,"billable":true,"workspace_id":"31689165","creator_id":"13403745","parent_id":null,"root_id":null,"id":"568676695"},"568676755":{"title":"task2","description":"desc2","updated_at":"2020-06-22T13:56:40-07:00","assignment_timestamped_at":"2020-06-22T13:56:40-07:00","created_at":"2020-06-19T10:26:27-07:00","due_date":null,"start_date":"2020-06-19","story_type":"task","state":"completed","position":99990000,"archived":false,"deleted_at":null,"sub_story_count":0,"percentage_complete":100,"priority":"normal","has_proofing_access":false,"ancestor_ids":[],"subtree_depth":0,"ancestry_depth":0,"time_trackable":true,"time_estimate_in_minutes":120,"logged_billable_time_in_minutes":120,"logged_nonbillable_time_in_minutes":0,"sub_stories_time_estimate_in_minutes":null,"sub_stories_billable_time_in_minutes":null,"weight":null,"budget_estimate_in_cents":20000,"budget_used_in_cents":50000,"uninvoiced_balance_in_cents":0,"invoiced_balance_in_cents":50000,"sub_stories_budget_estimate_in_cents":null,"sub_stories_budget_used_in_cents":null,"fixed_fee":false,"billable":true,"workspace_id":"31689165","creator_id":"13403745","parent_id":null,"root_id":null,"id":"568676755"},"568676765":{"title":"task3","description":"desc3","updated_at":"2020-06-22T13:56:40-07:00","assignment_timestamped_at":"2020-06-22T13:56:40-07:00","created_at":"2020-06-19T10:26:36-07:00","due_date":null,"start_date":"2020-06-19","story_type":"task","state":"completed","position":99980000,"archived":false,"deleted_at":null,"sub_story_count":0,"percentage_complete":100,"priority":"normal","has_proofing_access":false,"ancestor_ids":[],"subtree_depth":0,"ancestry_depth":0,"time_trackable":true,"time_estimate_in_minutes":180,"logged_billable_time_in_minutes":180,"logged_nonbillable_time_in_minutes":0,"sub_stories_time_estimate_in_minutes":null,"sub_stories_billable_time_in_minutes":null,"weight":null,"budget_estimate_in_cents":30000,"budget_used_in_cents":75000,"uninvoiced_balance_in_cents":0,"invoiced_balance_in_cents":75000,"sub_stories_budget_estimate_in_cents":null,"sub_stories_budget_used_in_cents":null,"fixed_fee":false,"billable":true,"workspace_id":"31689165","creator_id":"13403745","parent_id":null,"root_id":null,"id":"568676765"}}}
Problem is, for every API call, I won't know what that time entry ID is. It will change every time.
What I want to do is to capture a string output of a specific key inside each of those time entry objects.
For example, for time_in_minutes, what I need to capture in this case is: "75, 90, 60, 120, 180"
And I also need it to be in that format, regardless of how many entries there are—whether there are five entries or twenty.
Any tips or advice would be GREATLY appreciated.
I'm not sure if this is the most elegant or performant solution, but I think this produces the correct values:
//copied from the question
const apiResponse = {"count":5,"results":[{"key":"time_entries","id":"1522040735"},{"key":"time_entries","id":"1522040395"},{"key":"time_entries","id":"1517855265"},{"key":"time_entries","id":"1517854385"},{"key":"time_entries","id":"1517853315"}],"time_entries":{"1522040735":{"created_at":"2020-06-22T13:54:54-07:00","updated_at":"2020-06-22T13:54:54-07:00","date_performed":"2020-06-22","time_in_minutes":75,"billable":true,"notes":"","rate_in_cents":25000,"cost_rate_in_cents":null,"currency":"USD","currency_symbol":"$","currency_base_unit":100,"user_can_edit":false,"approved":false,"taxable":false,"is_invoiced":true,"story_id":"569297425","workspace_id":"31689165","user_id":"13403745","active_submission_id":null,"recent_submission_id":null,"id":"1522040735"},"1522040395":{"created_at":"2020-06-22T13:54:42-07:00","updated_at":"2020-06-22T13:54:42-07:00","date_performed":"2020-06-22","time_in_minutes":90,"billable":true,"notes":"","rate_in_cents":25000,"cost_rate_in_cents":null,"currency":"USD","currency_symbol":"$","currency_base_unit":100,"user_can_edit":false,"approved":false,"taxable":false,"is_invoiced":true,"story_id":"569297345","workspace_id":"31689165","user_id":"13403745","active_submission_id":null,"recent_submission_id":null,"id":"1522040395"},"1517855265":{"created_at":"2020-06-19T10:32:50-07:00","updated_at":"2020-06-19T10:32:50-07:00","date_performed":"2020-06-19","time_in_minutes":60,"billable":true,"notes":"","rate_in_cents":25000,"cost_rate_in_cents":null,"currency":"USD","currency_symbol":"$","currency_base_unit":100,"user_can_edit":false,"approved":false,"taxable":false,"is_invoiced":true,"story_id":"568676695","workspace_id":"31689165","user_id":"13403745","active_submission_id":null,"recent_submission_id":null,"id":"1517855265"},"1517854385":{"created_at":"2020-06-19T10:32:12-07:00","updated_at":"2020-06-19T10:32:12-07:00","date_performed":"2020-06-19","time_in_minutes":120,"billable":true,"notes":"","rate_in_cents":25000,"cost_rate_in_cents":null,"currency":"USD","currency_symbol":"$","currency_base_unit":100,"user_can_edit":false,"approved":false,"taxable":false,"is_invoiced":true,"story_id":"568676755","workspace_id":"31689165","user_id":"13403745","active_submission_id":null,"recent_submission_id":null,"id":"1517854385"},"1517853315":{"created_at":"2020-06-19T10:31:40-07:00","updated_at":"2020-06-19T10:31:40-07:00","date_performed":"2020-06-19","time_in_minutes":180,"billable":true,"notes":"","rate_in_cents":25000,"cost_rate_in_cents":null,"currency":"USD","currency_symbol":"$","currency_base_unit":100,"user_can_edit":false,"approved":false,"taxable":false,"is_invoiced":true,"story_id":"568676765","workspace_id":"31689165","user_id":"13403745","active_submission_id":null,"recent_submission_id":null,"id":"1517853315"}},"meta":{"count":5,"page_count":1,"page_number":1,"page_size":20},"stories":{"569297425":{"title":"task5","description":"task5desc","updated_at":"2020-06-22T13:56:41-07:00","assignment_timestamped_at":"2020-06-22T13:56:41-07:00","created_at":"2020-06-22T13:54:08-07:00","due_date":null,"start_date":null,"story_type":"task","state":"started","position":99960000,"archived":false,"deleted_at":null,"sub_story_count":0,"percentage_complete":0,"priority":"normal","has_proofing_access":false,"ancestor_ids":[],"subtree_depth":0,"ancestry_depth":0,"time_trackable":true,"time_estimate_in_minutes":null,"logged_billable_time_in_minutes":75,"logged_nonbillable_time_in_minutes":0,"sub_stories_time_estimate_in_minutes":null,"sub_stories_billable_time_in_minutes":null,"weight":null,"budget_estimate_in_cents":null,"budget_used_in_cents":31250,"uninvoiced_balance_in_cents":0,"invoiced_balance_in_cents":31250,"sub_stories_budget_estimate_in_cents":null,"sub_stories_budget_used_in_cents":null,"fixed_fee":false,"billable":true,"workspace_id":"31689165","creator_id":"13403745","parent_id":null,"root_id":null,"id":"569297425"},"569297345":{"title":"task4","description":"task4desc","updated_at":"2020-06-22T13:56:41-07:00","assignment_timestamped_at":"2020-06-22T13:56:41-07:00","created_at":"2020-06-22T13:53:55-07:00","due_date":null,"start_date":null,"story_type":"task","state":"started","position":99970000,"archived":false,"deleted_at":null,"sub_story_count":0,"percentage_complete":0,"priority":"normal","has_proofing_access":false,"ancestor_ids":[],"subtree_depth":0,"ancestry_depth":0,"time_trackable":true,"time_estimate_in_minutes":null,"logged_billable_time_in_minutes":90,"logged_nonbillable_time_in_minutes":0,"sub_stories_time_estimate_in_minutes":null,"sub_stories_billable_time_in_minutes":null,"weight":null,"budget_estimate_in_cents":null,"budget_used_in_cents":37500,"uninvoiced_balance_in_cents":0,"invoiced_balance_in_cents":37500,"sub_stories_budget_estimate_in_cents":null,"sub_stories_budget_used_in_cents":null,"fixed_fee":false,"billable":true,"workspace_id":"31689165","creator_id":"13403745","parent_id":null,"root_id":null,"id":"569297345"},"568676695":{"title":"task1","description":"desc1","updated_at":"2020-06-22T13:56:40-07:00","assignment_timestamped_at":"2020-06-22T13:56:40-07:00","created_at":"2020-06-19T10:26:09-07:00","due_date":null,"start_date":"2020-06-19","story_type":"task","state":"completed","position":100000000,"archived":false,"deleted_at":null,"sub_story_count":0,"percentage_complete":100,"priority":"normal","has_proofing_access":false,"ancestor_ids":[],"subtree_depth":0,"ancestry_depth":0,"time_trackable":true,"time_estimate_in_minutes":120,"logged_billable_time_in_minutes":60,"logged_nonbillable_time_in_minutes":0,"sub_stories_time_estimate_in_minutes":null,"sub_stories_billable_time_in_minutes":null,"weight":null,"budget_estimate_in_cents":10000,"budget_used_in_cents":30000,"uninvoiced_balance_in_cents":0,"invoiced_balance_in_cents":30000,"sub_stories_budget_estimate_in_cents":null,"sub_stories_budget_used_in_cents":null,"fixed_fee":false,"billable":true,"workspace_id":"31689165","creator_id":"13403745","parent_id":null,"root_id":null,"id":"568676695"},"568676755":{"title":"task2","description":"desc2","updated_at":"2020-06-22T13:56:40-07:00","assignment_timestamped_at":"2020-06-22T13:56:40-07:00","created_at":"2020-06-19T10:26:27-07:00","due_date":null,"start_date":"2020-06-19","story_type":"task","state":"completed","position":99990000,"archived":false,"deleted_at":null,"sub_story_count":0,"percentage_complete":100,"priority":"normal","has_proofing_access":false,"ancestor_ids":[],"subtree_depth":0,"ancestry_depth":0,"time_trackable":true,"time_estimate_in_minutes":120,"logged_billable_time_in_minutes":120,"logged_nonbillable_time_in_minutes":0,"sub_stories_time_estimate_in_minutes":null,"sub_stories_billable_time_in_minutes":null,"weight":null,"budget_estimate_in_cents":20000,"budget_used_in_cents":50000,"uninvoiced_balance_in_cents":0,"invoiced_balance_in_cents":50000,"sub_stories_budget_estimate_in_cents":null,"sub_stories_budget_used_in_cents":null,"fixed_fee":false,"billable":true,"workspace_id":"31689165","creator_id":"13403745","parent_id":null,"root_id":null,"id":"568676755"},"568676765":{"title":"task3","description":"desc3","updated_at":"2020-06-22T13:56:40-07:00","assignment_timestamped_at":"2020-06-22T13:56:40-07:00","created_at":"2020-06-19T10:26:36-07:00","due_date":null,"start_date":"2020-06-19","story_type":"task","state":"completed","position":99980000,"archived":false,"deleted_at":null,"sub_story_count":0,"percentage_complete":100,"priority":"normal","has_proofing_access":false,"ancestor_ids":[],"subtree_depth":0,"ancestry_depth":0,"time_trackable":true,"time_estimate_in_minutes":180,"logged_billable_time_in_minutes":180,"logged_nonbillable_time_in_minutes":0,"sub_stories_time_estimate_in_minutes":null,"sub_stories_billable_time_in_minutes":null,"weight":null,"budget_estimate_in_cents":30000,"budget_used_in_cents":75000,"uninvoiced_balance_in_cents":0,"invoiced_balance_in_cents":75000,"sub_stories_budget_estimate_in_cents":null,"sub_stories_budget_used_in_cents":null,"fixed_fee":false,"billable":true,"workspace_id":"31689165","creator_id":"13403745","parent_id":null,"root_id":null,"id":"568676765"}}}
const timeEntries = apiResponse['time_entries'];
const minuteTimes = Object.values(timeEntries).map(entry => entry['time_in_minutes']);
//prints [180, 120, 60, 90, 75]
console.log(minuteTimes);
If you need to filter the time_entries by id (if you want to look at a subset, for instance) then you can probably incorporate this
Let's define response to hold the API return, and what is the
property you wish to extract from each time_entries object.
function extract(response, what){
const parsed = (typeof(response)==='object')? response: JSON.parse(response);
return (Object
.keys(parsed.time_entries) // finds the keys
.sort((a,b)=>(+b-a)) // sorts the keys in reverse order
.map((k)=>(parsed.time_entries[k][what])) // finds the data
.filter((v)=>(v!==undefined)) // removes undefined data
.join(",") // changes to CSV [75,90,60,...] -> '75,90,60,...'
);
}
Testing in node.js
extract(response, 'time_in_minutes');
'75,90,60,120,180'
Try,
Object.keys() method on the object.
you can use Object.values to get the values of each key and do the map iteration for the specific result.
var resp = {"count":5,"results":[{"key":"time_entries","id":"1522040735"},{"key":"time_entries","id":"1522040395"},{"key":"time_entries","id":"1517855265"},{"key":"time_entries","id":"1517854385"},{"key":"time_entries","id":"1517853315"}],"time_entries":{"1522040735":{"created_at":"2020-06-22T13:54:54-07:00","updated_at":"2020-06-22T13:54:54-07:00","date_performed":"2020-06-22","time_in_minutes":75,"billable":true,"notes":"","rate_in_cents":25000,"cost_rate_in_cents":null,"currency":"USD","currency_symbol":"$","currency_base_unit":100,"user_can_edit":false,"approved":false,"taxable":false,"is_invoiced":true,"story_id":"569297425","workspace_id":"31689165","user_id":"13403745","active_submission_id":null,"recent_submission_id":null,"id":"1522040735"},"1522040395":{"created_at":"2020-06-22T13:54:42-07:00","updated_at":"2020-06-22T13:54:42-07:00","date_performed":"2020-06-22","time_in_minutes":90,"billable":true,"notes":"","rate_in_cents":25000,"cost_rate_in_cents":null,"currency":"USD","currency_symbol":"$","currency_base_unit":100,"user_can_edit":false,"approved":false,"taxable":false,"is_invoiced":true,"story_id":"569297345","workspace_id":"31689165","user_id":"13403745","active_submission_id":null,"recent_submission_id":null,"id":"1522040395"},"1517855265":{"created_at":"2020-06-19T10:32:50-07:00","updated_at":"2020-06-19T10:32:50-07:00","date_performed":"2020-06-19","time_in_minutes":60,"billable":true,"notes":"","rate_in_cents":25000,"cost_rate_in_cents":null,"currency":"USD","currency_symbol":"$","currency_base_unit":100,"user_can_edit":false,"approved":false,"taxable":false,"is_invoiced":true,"story_id":"568676695","workspace_id":"31689165","user_id":"13403745","active_submission_id":null,"recent_submission_id":null,"id":"1517855265"},"1517854385":{"created_at":"2020-06-19T10:32:12-07:00","updated_at":"2020-06-19T10:32:12-07:00","date_performed":"2020-06-19","time_in_minutes":120,"billable":true,"notes":"","rate_in_cents":25000,"cost_rate_in_cents":null,"currency":"USD","currency_symbol":"$","currency_base_unit":100,"user_can_edit":false,"approved":false,"taxable":false,"is_invoiced":true,"story_id":"568676755","workspace_id":"31689165","user_id":"13403745","active_submission_id":null,"recent_submission_id":null,"id":"1517854385"},"1517853315":{"created_at":"2020-06-19T10:31:40-07:00","updated_at":"2020-06-19T10:31:40-07:00","date_performed":"2020-06-19","time_in_minutes":180,"billable":true,"notes":"","rate_in_cents":25000,"cost_rate_in_cents":null,"currency":"USD","currency_symbol":"$","currency_base_unit":100,"user_can_edit":false,"approved":false,"taxable":false,"is_invoiced":true,"story_id":"568676765","workspace_id":"31689165","user_id":"13403745","active_submission_id":null,"recent_submission_id":null,"id":"1517853315"}},"meta":{"count":5,"page_count":1,"page_number":1,"page_size":20},"stories":{"569297425":{"title":"task5","description":"task5desc","updated_at":"2020-06-22T13:56:41-07:00","assignment_timestamped_at":"2020-06-22T13:56:41-07:00","created_at":"2020-06-22T13:54:08-07:00","due_date":null,"start_date":null,"story_type":"task","state":"started","position":99960000,"archived":false,"deleted_at":null,"sub_story_count":0,"percentage_complete":0,"priority":"normal","has_proofing_access":false,"ancestor_ids":[],"subtree_depth":0,"ancestry_depth":0,"time_trackable":true,"time_estimate_in_minutes":null,"logged_billable_time_in_minutes":75,"logged_nonbillable_time_in_minutes":0,"sub_stories_time_estimate_in_minutes":null,"sub_stories_billable_time_in_minutes":null,"weight":null,"budget_estimate_in_cents":null,"budget_used_in_cents":31250,"uninvoiced_balance_in_cents":0,"invoiced_balance_in_cents":31250,"sub_stories_budget_estimate_in_cents":null,"sub_stories_budget_used_in_cents":null,"fixed_fee":false,"billable":true,"workspace_id":"31689165","creator_id":"13403745","parent_id":null,"root_id":null,"id":"569297425"},"569297345":{"title":"task4","description":"task4desc","updated_at":"2020-06-22T13:56:41-07:00","assignment_timestamped_at":"2020-06-22T13:56:41-07:00","created_at":"2020-06-22T13:53:55-07:00","due_date":null,"start_date":null,"story_type":"task","state":"started","position":99970000,"archived":false,"deleted_at":null,"sub_story_count":0,"percentage_complete":0,"priority":"normal","has_proofing_access":false,"ancestor_ids":[],"subtree_depth":0,"ancestry_depth":0,"time_trackable":true,"time_estimate_in_minutes":null,"logged_billable_time_in_minutes":90,"logged_nonbillable_time_in_minutes":0,"sub_stories_time_estimate_in_minutes":null,"sub_stories_billable_time_in_minutes":null,"weight":null,"budget_estimate_in_cents":null,"budget_used_in_cents":37500,"uninvoiced_balance_in_cents":0,"invoiced_balance_in_cents":37500,"sub_stories_budget_estimate_in_cents":null,"sub_stories_budget_used_in_cents":null,"fixed_fee":false,"billable":true,"workspace_id":"31689165","creator_id":"13403745","parent_id":null,"root_id":null,"id":"569297345"},"568676695":{"title":"task1","description":"desc1","updated_at":"2020-06-22T13:56:40-07:00","assignment_timestamped_at":"2020-06-22T13:56:40-07:00","created_at":"2020-06-19T10:26:09-07:00","due_date":null,"start_date":"2020-06-19","story_type":"task","state":"completed","position":100000000,"archived":false,"deleted_at":null,"sub_story_count":0,"percentage_complete":100,"priority":"normal","has_proofing_access":false,"ancestor_ids":[],"subtree_depth":0,"ancestry_depth":0,"time_trackable":true,"time_estimate_in_minutes":120,"logged_billable_time_in_minutes":60,"logged_nonbillable_time_in_minutes":0,"sub_stories_time_estimate_in_minutes":null,"sub_stories_billable_time_in_minutes":null,"weight":null,"budget_estimate_in_cents":10000,"budget_used_in_cents":30000,"uninvoiced_balance_in_cents":0,"invoiced_balance_in_cents":30000,"sub_stories_budget_estimate_in_cents":null,"sub_stories_budget_used_in_cents":null,"fixed_fee":false,"billable":true,"workspace_id":"31689165","creator_id":"13403745","parent_id":null,"root_id":null,"id":"568676695"},"568676755":{"title":"task2","description":"desc2","updated_at":"2020-06-22T13:56:40-07:00","assignment_timestamped_at":"2020-06-22T13:56:40-07:00","created_at":"2020-06-19T10:26:27-07:00","due_date":null,"start_date":"2020-06-19","story_type":"task","state":"completed","position":99990000,"archived":false,"deleted_at":null,"sub_story_count":0,"percentage_complete":100,"priority":"normal","has_proofing_access":false,"ancestor_ids":[],"subtree_depth":0,"ancestry_depth":0,"time_trackable":true,"time_estimate_in_minutes":120,"logged_billable_time_in_minutes":120,"logged_nonbillable_time_in_minutes":0,"sub_stories_time_estimate_in_minutes":null,"sub_stories_billable_time_in_minutes":null,"weight":null,"budget_estimate_in_cents":20000,"budget_used_in_cents":50000,"uninvoiced_balance_in_cents":0,"invoiced_balance_in_cents":50000,"sub_stories_budget_estimate_in_cents":null,"sub_stories_budget_used_in_cents":null,"fixed_fee":false,"billable":true,"workspace_id":"31689165","creator_id":"13403745","parent_id":null,"root_id":null,"id":"568676755"},"568676765":{"title":"task3","description":"desc3","updated_at":"2020-06-22T13:56:40-07:00","assignment_timestamped_at":"2020-06-22T13:56:40-07:00","created_at":"2020-06-19T10:26:36-07:00","due_date":null,"start_date":"2020-06-19","story_type":"task","state":"completed","position":99980000,"archived":false,"deleted_at":null,"sub_story_count":0,"percentage_complete":100,"priority":"normal","has_proofing_access":false,"ancestor_ids":[],"subtree_depth":0,"ancestry_depth":0,"time_trackable":true,"time_estimate_in_minutes":180,"logged_billable_time_in_minutes":180,"logged_nonbillable_time_in_minutes":0,"sub_stories_time_estimate_in_minutes":null,"sub_stories_billable_time_in_minutes":null,"weight":null,"budget_estimate_in_cents":30000,"budget_used_in_cents":75000,"uninvoiced_balance_in_cents":0,"invoiced_balance_in_cents":75000,"sub_stories_budget_estimate_in_cents":null,"sub_stories_budget_used_in_cents":null,"fixed_fee":false,"billable":true,"workspace_id":"31689165","creator_id":"13403745","parent_id":null,"root_id":null,"id":"568676765"}}}
var output = Object.values(resp.time_entries).map(val => val.time_in_minutes).join(',');
console.log(output);

How get maximum from array of string using node js?

I'm trying to get maximum value from the array of string using node JS.
I have enabled CDC on SQL server, I can get transaction-id of data in the string(converted from binary to string within SQL server itself), after that I'm getting an array of strings like below(I'm just posting array string that's being collected from array of objects)
arr1= [ '0x000001B1000000900009',
'0x000001B1000000900009',
'0x000001B1000000900009',
'0x000001B1000000900009']
If get max from array using the following code
Math.max.apply(Math, arrayofobject.map(function(o) { return o.y; }))
I get the below output
122441614897446930
it automatically converts string to a number and get max,
I don't want this way, I want max from the array of string, becasue I need to update nextOffset for CDC.
Thanks in advance
You mean
const arr1 = ['0x000001B1000000900016',
'0x000001B1000000900007',
'0x000001B1000000900008',
'0x000001B1000000900009'
]
console.log(arr1.sort().pop())

Counting a particular word in a string using Zapier code

I use Zapier to automate many of our business functions, which is great, but I got stuck trying to count the number of arrays or, if you like, a particular word pattern that comes from a string. I can tidy up the string with Zapier formatter, but cannot figure out how to carry out a count.
Here is an example of a tidied string where " have been removed:
[{Name:Jon,Surname:Smith},{Name:David,Surname:Michael},{Name:Sam,Surname:Fields},{Name:Katy,Surname:Milnes}]
In this instance I would want the count on say "Name" to return 4.
I have looked at different code examples for counting words but cannot execute them correctly in the code action of Zapier. This is probably really straight forward but I do not come from a coding background so a simple Java (or Python) script to drop into the Zapier code action or some pointers on how to solve this would be greatly appreciated.
Thanks
What are you really trying to achieve by trying to count the word?
Do you just want to know the number of objects the array contains? If that is the case something like this would work. Assuming that the array is in your inputData for the code step.
var data = JSON.stringify([{'Name':'Jon', 'Surname':'Smith'},{'Name':'David','Surname':'Michael'},{'Name':'Sam','Surname':'Fields'},{'Name':'Katy','Surname':'Milnes'}]);
var inputData = {objArr: data};
// Do not insert the above lines in your code step.
// Set the objArr to your array in the inputData step.
var parsedObjArr = JSON.parse(inputData.objArr);
// Skip the above step if the array is not in the inputData object.
var arrLen = parsedObjArr.length
console.log('Array Length: ', arrLen);
// The line below outputs data from the code step.
output = {arrLen}
Also note, you do not need to remove the quotes from the JSON string.
If the array is not in the inputData of the code step, you can just directly use the length method on the array.
Well in Python you can convert the json string into dictionary with key as the name. Length of dictionary is what you are looking for. Here is the example:
import json
from collections import defaultdict
d=defaultdict(list)
x=json.dumps([{'Name':'Jon', 'Surname':'Smith'},{'Name':'David','Surname':'Michael'},{'Name':'Sam','Surname':'Fields'},{'Name':'Katy','Surname':'Milnes'}])
json_string=json.loads(x)
for obj in json_string:
if(obj['Name'] in d):
d[obj['Name']].append([obj['Name']+' '+obj['Surname']])
else:
d[obj['Name']]=[obj['Name']+' '+obj['Surname']]
print(len(d))

correct a malformed javascript array

I send a php array like:
$var = array (
0=> 4,
1=> 6,
2=> 8,
...
as json_encode($var); into the uri and then I receive it into javascript file is still ok here but when I push it into new array like this :
this.patg.push(attd);
Is inserted like this below .
var attds = ["4,6,7,8,9,5558,5560,5573,5574,5586,5589,5606"]
I know I have to find the problem. but
Questions:
could you please tell me why this could happends or help me to deal with it.
but in any case just for knowledge . how you would add the extra " " surrounding the , that i miss to be an array , or is that crazy idea to fix this?
If you are receiving a string value and you want to use it as an array of integers you should split it into an array first:
var receivedData = "4,6,7,8,9,5558,5560,5573,5574,5586,5589,5606";
var dataArray = receivedData.split(",");
Afterwards you can use it with another array, however be aware that if you already have a defined array into which you want to push the dataArray you shouldn't push but concat instead.
In other words if you have:
var previousArray = [1,2,3];
previousArray.push(dataArray);
You will get
[1,2,3,[4,6,7,8,9,5558,5560,5573,5574,5586,5589,5606]]
meaning that the whole array is pushed onto the 4th position of previousArray.
If, on the other hand, you concat the arrays will merge:
var previousArray = [1,2,3];
previousArray.concat(dataArray);
[1,2,3,4,6,7,8,9,5558,5560,5573,5574,5586,5589,5606]
Source: http://www.w3schools.com/jsref/jsref_concat_array.asp

jQuery: $.getJSON sorting the data on Chrome / IE?

I'm passing an associative array (id => val) using Ajax and receiving it with jQuery's $.getJSON which read the data properly and prepared the object. There is, however, very annoying sorting issue.
It appears that on Chrome and IE the data becomes sorted by the id part of the associate array. So if the array should be (5=> 'xxx', 3 => 'fff') it actually becomes (3 => 'fff',5=> 'xxx'). On FireFox it works as expected, i.e. not sorted.
Any ideas?
You can add a leading 0 for all integer indexes.
var json = { '05' => 'xxx', '03' => 'fff' };
Seems the best way is to avoid associative arrays at all. When you want to send an associate array simply send it as two separate arrays - one of keys and one of values. Here's the PHP code to do that:
$arWrapper = array();
$arWrapper['k'] = array_keys($arChoices);
$arWrapper['v'] = array_values($arChoices);
$json = json_encode($arWrapper);
and the simple JavaScript code to do whatever you'd like with it
for (i=0; i < data['k'].length; i++) {
console.log('key:' + data['k'][i] + ' val:' + data['v'][i]);
}
Another option is to return the data as an array of objects. That will ensure that the objects stay in the order that you return them.
Edit:
Basically, for each key > value pair, push it to a new array and json_encode that array.

Categories