This might seem like an odd questions; I know its possible to slice an array, but I was thinking, if I'm calling an array externally via a $http GET (using Angular) like so:
$http.get('/url')
is it possible for me to declare how much of the array I want to retrieve first before making the request, instead of retrieving the whole thing (and therefore saving on performance). I know its possible to do something similar using PHP but wasn't sure to the extent I had in Javascript? Or can I only slice it once the array is declared fully?
The only way to retrieve part of the array is for the server that you are requesting the data from to support that capability. You cannot do that entirely from the client unless the server has that capability.
The usual way that this would be done would be to add query parameters to the URL that specify how much of the data you want and for the server to look at those query parameters and to send only those pieces of the data.
For example:
$http.get('/url?start=0&end=20');
Arrays can only be sliced once they exist and have some data in them so I'm not sure what that part of the question was trying to ask about. Either the client or the server could slice the result array once they had built the initial array.
Related
I'm new to programming, and I have been programming a small project with vanilla javascript, but I was using a lot of document.getElementById() tags, and I stored all of these in a javascript object, on a seperate file, but I was wondering If I could Just store that object on a SQL file, to make my project more organized.
I'm not sure if that's possible, I know that SQL stores data, so would I be able to store my JS object on a sql file, and import that object into my seperate Javascript files?
I'm trying to make sure if I can do what I want to do before I decide to start learning sql, but If it does do what I need, I was going to start incorporating it for organization, so I can learn it as I create projects.
You can use the JSON.stringify function to convert your javascript objects into strings. However, it is important to note that the only items within the javascript object that are converted into strings are: objects, arrays, strings, numbers, and values that are: null, true, or false. If you have references to functions or classes that have been instantiated, then these will be lost. You can convert the string back into a javascript object using JSON.parse.
One thing to consider before you do this is whether or not you need to perform database queries on the data that you are storing within the javascript object. If you need to search on the javascript object's data, then you should store the information directly within tables in the database. If you don't need to search on it, then converting the data to a string and saving it should be fine to do. Since it sounds as though you are using the data for your own purposes, doing this should be fine since extracting all of the data from the database shouldn't be an intensive task. Also, you can write your own scripts to parse the data.
Definitely, you can store as a JSON Blob
https://learn.microsoft.com/en-us/sql/relational-databases/json/store-json-documents-in-sql-tables?view=sql-server-ver15
I'm working with this returned API from a third party:
(note: returns LARGE dataset)
https://www.saferproducts.gov/RestWebServices/Recall?format=json
I know I can get this giant object like so:
$.getJSON('https://www.saferproducts.gov/RestWebServices/Recall?format=json', function(json, textStatus) {
console.log(json);
});
This returns ~7000 objects. There is no way in the API call to dictate how many objects you want returned. It's all or nothing.
Here's the question...can I use getJSON (or similar) to only get the first 5 objects and stop without having to load the entire JSON file first?
I did something similar a while a go. I used PHP to fetch a webpage of an api. Then I would cache it. Via PHP logic, I stored a variable inside a text file which contained all the information from the webpage. I had another file that stored the timestamp. Then, when the page was called, php would check the timestamp to see how old it was. If it was too old, it'd recache the page and return relevant information. If it was still valid, it would just return the cached information. If you only want the last 5, the PHP logic wouldn't be too hard to write that in. Then, jQuery would query the PHP page.
They don't have anything called out in their documentation for limiting the returns. I think their intent is for you to narrow down your search so you're not fetching every single item. You could always email them and ask as what Mike McCaughan said, if they don't have a 'limit' baked in, then no, it's not possible.
It also looks like they offer weekly downloads that you can just create your own API and add a limit property:
https://www.fda.gov/%20Safety/Recalls/EnforcementReports/default.htm
Reference:
https://github.com/presidential-innovation-fellows/recalls-api/wiki/data-sources
https://www.cpsc.gov/Recalls/CPSC-Recalls-Application-Program-Interface-API-Information/
https://www.cpsc.gov/Global/info/Recall/CPSC-Recalls-Retrieval-Web-Services-Programmers-Guide_3-25-2015.pdf
If there really is no option for limiting that call, then I'd suggest caching, showing some kind of processing wheel while the call takes place or narrowing your query. They have options to for filtering that may work for you such as the following:
RecallNumber
RecallDateStart
RecallDateEnd
LastPublishDateStart
LastPublishDateEnd
RecallURL
RecallTitle
ConsumerContact
RecallDescription
ProductName
ProductDescription
ProductModel
ProductType
RecallInconjunctionCountry
ImageURL
Injury
ManufacturerCountry
UPC – see caveat below
Hazard
Manufacturer
Remedy
Retailer
So, I'm originally a native application developer (mostly C++), but I've found myself working on a small business Coding project for a friend. I'm used to self contained native applications and I'm struggling with a current problem on my project:
I have an AppInstance class in PHP which is implemented as a singleton (I've heard that most of the time this isn't really the best way to do things, and I concede this). This class contains a few basic operating parameters for the application (mostly simple strings and numbers), as well as an array of complex objects (objects composed of other objects, arrays and other members and methods). I'd like to be able to pass any/all of this application data between this original PHP script (which initializes all of the values that my program needs to run, and outputs the default HTML of my UI).
I then begin to handle user input actions with Javascript. Some of these inputs require information from the original PHP script. Because the processing of these inputs requires a bit more computation, I'd like to use AJAX to call this processing on the server (in PHP) and return the output. However, Because this processing requires some of the objects from the original PHP script, I'm trying to figure out how I should pass these objects around in the most efficient way?
I've read that I can use JSON encode to effectively serialize my objects and send them to JS (the UI in this case), and these objects can be returned to PHP (see this). But my question really is a multi-part:
Would it be more efficient to perform all calculations and processing on the server (in PHP, rather than doing it all on the client-side in JS)?
If the answer to #1 is "Yes", my first inclination is to serialize the PHP object to a file on the server, and have the new PHP script read and deserialize the object. Is there a more efficient method than this and,if not, should I serialize in JSON or PHP's serialize() format?
Edit: I should probably mention that some of my objects also possess resource variable, as I know this has some effect on serialization.
In terms of efficiency I would be looking at whatever reduces the number of network (AJAX) calls, as this will usually be your largest overhead. This is hard to comment on in your case because I don't know how often a client is going to be making such calls. A bigger worry for me is security, as Javascript would be able to manipulate values it gets from the AppInstance. I personally would consider an API which only exposes actions you permit.
I'm ending up having to do hacks to convert 'true' to just true and it's creating code smell.
Is there a library like https://github.com/thephpleague/fractal that allows me to transform my response into the types I need?
In cases like this it's almost always better to fix the API to return data in a usable format rather than trying to post-process the result on the client.
In your case there are several routes you could take:
Store the list as a JSON string directly in the database.
This means you don't have to do any processing on the server and can just return it 'as is'. However you lose the ability to do queries on the data directly and need to resort to things like LIKE and string operations.
Store the data relationally, and process it on the server to turn it into JSON
Here you retain the ability to do queries on your data, but you may need to do several queries to get all the data you need and then connect it on the server. (eg. you would do one SELECT on the user table to get a user, and then you would need to do another SELECT on the friends table where the userid matches your first user. You would then need to merge these results to create your JSON.) This is usually the best way to do it.
You can also turn the result into JSON directly inside the database engine using a user defined function. For example using https://github.com/mysqludf/lib_mysqludf_json#readme
This is somewhat similar to 2, but it ties your stored procs to the JSON format.
My goal here is to cut down the number of cookies I'm using to store things like persistent states into a single cookie by setting and retrieving multiple key/value pairs within a single cookie using an array.
I'm mostly interested in knowing if someone else has already done this and has written a plugin for it, as I would think this has been done before.
You could serialize the object into a JSON string. That would make it super simple to re-load the object. See question 191881 for information on how to serialize an object to JSON in JavaScript.