Replace an array parameter in a url request using URLSearchParams and $.param() - javascript

I would like to change the url request while clicking on a link one of the element of an array. For example :
?foo[]=1&foo[]=2
to
?foo[]=1&foo[]=3
I use URLSearchParams with methods .getAll('foo[]') and then .set('foo[]', array)
Finally, I use jquery method param() to generate a correct url string (as I was not able to use URLSearchParams.toString() with an array -- the result is always foo[]=1&3).
This is working, but if I had other parameter in the request I would like to keep, it becomes complicated to regenerate the url.
For example :
?foo[]=1&foo[]=2&bar=3&bar[]=4&page=1
to
?foo[]=1&foo[]=3&bar=3&bar[]=4&page=1
I have many parameters, so I would like to avoid the rebuild the entire query object to pass to $.param(). So :
Is there any option to get the whole array from URLSearchParams without using any loop.
Is there any trick to use URLSearchParams.toString() with arrays.

What you'll need to do is remove the foo[] entry, then iterate the values array from .getAll('foo[]') and .append().
For example
const usp = new URLSearchParams([
['foo[]', 1],
['foo[]', 2],
['bar', 3],
['bar[]', 4],
['page', 1]
])
console.info('original:', decodeURIComponent(usp.toString()))
// get all 'foo[]' entries
const foos = usp.getAll('foo[]')
// incremenent the last value
foos[foos.length - 1]++
// remove 'foo[]'
usp.delete('foo[]')
// iterate values and append
foos.forEach(foo => usp.append('foo[]', foo))
console.info('updated:', decodeURIComponent(usp.toString()))

Related

Find matching strings from array with multiple params

Not sure how to implement such a search with params as values and different params count. Looks like I need Regex & match. But params are not hardcoded in my matching (possible URLs array) - they are dynamic, and param value can be 0, 100, 777 and etc.
For example, I have such an array of strings to match:
[
'urlA/someId1',
'urlA/someId1/someSubUrlA/someId2',
'urlB/someId1/someId2'
'urlB/someId1/someSubUrlB/someId2'
]
ps: I can edit this array, so for example surround params with {} etc.
and when I try to run a search for: http://example.com/urlA/100/someSubUrlA/200
it should return urlA/someId1/someSubUrlA/someId2
when I try to run a search for: http://example.com/urlA/150
it should return urlA/someId1
and from what I can see I can use Regex & .match with .find(). But I'm confused about how to write custom matcher with custom parameters & their count. Is it possible somehow in JS?
Basically you want that given a url to find the abstract representation of it. Right?
let data = [
'urlA/someId1',
'urlA/someId1/someSubUrlA/someId2',
'urlB/someId1/someId2',
'urlB/someId1/someSubUrlB/someId2'
];
function finder(url) {
let afterDomain = url.match(/http:\/\/[^\/]*\.[^\/]*\/(.*)/)[1];
let matcher = afterDomain.replace(/\d+/g, "[^\/]+");
return data.find(x => (new RegExp(matcher)).test(x))
}
finder("http://example.com/urlA/100/"); // "urlA/someId1"
finder("http://example.com/urlA/100/someSubUrlA/200"); // "urlA/someId1/someSubUrlA/someId2"

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);

MongoDB search string in array of objects

I have a structure in MongoDB that have different amounts of items in an array called "items". To make the search, I am using the following command, which first turns the contents into a string, as in this.items there is a different structure depending on the object:
db.getCollection('docs').find.('JSON.stringify(this.items[0].value).toLowerCase().indexOf("text")!=-1')
My problem is that as I do not know the amount of items that each document has, I would have to use a wildcard as this.items[*].value, but it does not work.
Does anyone know any solution, or have another idea for this?
You can use the $elemMatch (https://docs.mongodb.com/manual/reference/operator/projection/elemMatch/)
db.docs.find({items: {$elemMatch: {value: {$regex : "text"}}}});
So this query will find all documents with an item in the items array that contain the string "text" in the value property, after this operation you can count how much items the document has.
You can use dot notation of items.value to target the value field of all items elements, and a regular expression to perform the case-insensitive sub-string match:
db.getCollection('docs').find({ 'items.value': /text/i })
You can iterate each document and apply the indexOf, something like this..
var cursor = db.getCollection('docs').find({}); // get all docs
var newOut = []; // new array of items if match with some condition
while ( cursor.hasNext() ){ // iterate all docs
var doc = cursor.next(); // get the document in focus
doc.items.forEach(function(item){ // iterate the items of doc.items
if ( item.toLowerCase().indexOf("text") !== -1 ) // check if text exists in array
newOut.push(item); // add to new array
});
};
printjson(newOut);

Best way to store array data as String in text file?

I'm working on a (JavaScript/JQuery) project that requires me to save data as a file.
The data being stored is an array, which normally wouldn't be an issue because I can would just store it as a string and then split the string based on "," on loading the data again. However the array I need to store is an array of other arrays of data, some of which have a few layers of arrays.
My initial thought is to run a function which converts each array to a string starting at the lowest levels and then add some sort of identifier (eg '/////') between each entry to separate each array of data and use them as the thing to detect for a Split function. This however make the storing/loading of the data very complex and I was wondering if there is a better way of saving multi-layer array data in Javascript.
You can still save it as a string but the serialization should be something like JSON.
To convert to json (encode) use JSON.stringify. This returns a string that you can save to the file:
var json_string = JSON.stringify(my_array);
Afterwards, to decode the string in the file (after reading it) use JSON.parse
var my_array = JSON.parse(json_string);
You can use JSON.stringify to convert a multi-level array to a string. And then you can use JSON.parse to convert it back to an array. Once you have the string you can save and restore it and use these methods to re-create the array.
a = [1, 2, [3, 4, [5, 6, [7, 8], 9]]]
> [1, 2, Array[3]]
s = JSON.stringify(a)
> "[1,2,[3,4,[5,6,[7,8],9]]]"
a2 = JSON.parse(s)
> [1, 2, Array[3]]

JavaScript - Get all but last item in array

This is my code:
function insert(){
var loc_array = document.location.href.split('/');
var linkElement = document.getElementById("waBackButton");
var linkElementLink = document.getElementById("waBackButtonlnk");
linkElement.innerHTML=loc_array[loc_array.length-2];
linkElementLink.href = loc_array[loc_array.length];
}
I want linkElementLink.href to grab everything but the last item in the array. Right now it is broken, and the item before it gets the second-to-last item.
I’m not quite sure what you’re trying to do. But you can use slice to slice the array:
loc_array = loc_array.slice(0, -1);
Use pathname in preference to href to retrieve only the path part of the link. Otherwise you'll get unexpected results if there is a ?query or #fragment suffix, or the path is / (no parent).
linkElementLink.href= location.pathname.split('/').slice(0, -1).join('/');
(But then, surely you could just say:)
linkElementLink.href= '.';
Don't do this:
linkElement.innerHTML=loc_array[loc_array.length-2];
Setting HTML from an arbitrary string is dangerous. If the URL you took this text from contains characters that are special in HTML, like < and &, users could inject markup. If you could get <script> in the URL (which you shouldn't be able to as it's invalid, but some browser might let you anyway) you'd have cross-site-scripting security holes.
To set the text of an element, instead of HTML, either use document.createTextNode('string') and append it to the element, or branch code to use innerText (IE) or textContent (other modern browsers).
If using lodash one could employ _.initial(array):
_.initial(array): Gets all but the last element of array.
Example:
_.initial([1, 2, 3]);
// → [1, 2]
Depending on whether or not you are ever going to reuse the array you could simply use the pop() method one time to remove the last element.
linkElementLink.href = loc_array[loc_array.length]; adds a new empty slot in the array because arrays run from 0 to array.length-1; So you returning an empty slot.
linkElement.innerHTML=loc_array[loc_array.length-2]; if you use the brackets you are only getting the contents of one index. I'm not sure if that is what you want? The next section tells how to get more than one index.
To get what you want you need for the .href you need to slice the array.
linkElementLink.href = loc_array.slice(0,loc_array.length-1);
or using a negative to count from the end
linkElementLink.href = loc_array.slice(0,-1);
and this is the faster way.
Also note that when getting to stuff straight from the array you will get the same as the .toString() method, which is item1, item2, item3. If you don't want the commas you need to use .join(). Like array.join('-') would return item1-item2-item3. Here is a list of all the array methods http://www.w3schools.com/jsref/jsref_obj_array.asp. It is a good resource for doing this.
.slice(number you want)
if you just want the first 4 elements in an array its array.slice(3)
doing a negative number starts from the end of the array

Categories