Variable keeps turning into an object instead of an Array in Javascript - javascript

I'm learning javascript (and HTML on Electron), and I have a variable which is meant to be an array:
var arrayList = [];
When I add objects to it (taken from JSON data), using Push:
arrayList.push(object);
Everything works perfect. I then save this using an api from npm called: electron-json-storage
I then want to pull this data, push an object into it and save it back. The problem is when I use the api to obtain the data to save into the variable arrayList, it turns arrayList into an object, it then errors out when I try to push to it as it is the wrong variable type now compared to before.
Would love any help and guidance would be great, thanks!

apologies about this, turns out the 'electron-json-storage' api sends back a blank object if the file doesn't exist, which was actually my problem (even though I know I didn't write that above) and what kept turning it into an object. I wouldn't of figured it out without re-looking over my code before posting here (so thanks klugjo) and for trying to parse the object which double confirmed it was an object and lead me to find the actual problem (So thanks so much Robert).
Again thanks all for the help!

Related

JavaScript object has different value in source page than in console

So my application is wrote in java and Spring and I need to pass a hash map with [data,value] from backend to frontend , using THYMELEAF, so I can render them into a graph. These are my lines in my controller
model.addAttribute("hashValues",resultHash.values());
model.addAttribute("hashTime",resultHash.keySet());
When trying to access them in javascript, the hashValues seem to be right, but the time list has a problem. When using console.log to print hashTime my console says something like [1996,1997,1997] when in reality my dates are [2021-10-15, 2021-10-14, 2021-10-13]. I will add some images for a better understanding.
This is how I get the lists from the model.
var time = [[${hashTime}]]
var values = [[${hashValues}]]
console.log(time)
console.log(values)
I've tried using JSON.parse but I have the same problem. Any help is appreciated cause I've been working with JavaScript for some time and I never encountered this type of problem before.

VB Collection in JavaScript

I've been serching the net for quite some time and maybe there is no solution for my problem but I hoped you might be able to help me. I'm currently trying to establish a connection to CatiaV5 via JavaScript. The connection itsself is working just fine. Basically it looks like this now:
var catia = new ActiveXObject('CATIA.Application');
var doc = catia.ActiveDocument;
But here's my problem. The returned object is a Collection in VB and there seems to be no DataType equivalent to this. So this is what i get when i try to read my variables. For example:
doc.Product
returns
[object] {};
So this seems to be empty. However if then I try to get
doc.Product.Name
which by that logic should be undefined, instead i get
"Part1"
so the correct name of my Part/Product is returned.
All the Catia stuff probably isn't that relevant for the question.
my question: Is there any way to somehow parse a VB-Collection on a JavaScript object or something similar, to get the contents of what is returned?
OK,
here's the thing. I found out, that you can't really browse throug ActiveXObjects directly in the console in general. In the IE-Dev-Tools i was however able to use the Locals-Watch to browse through the Object. I didn't find a nice way to Parse it onto a JSON-Object, however the manual way to create a new Object and add the Keys piece by piece works.

Single Item Array Read as Object in React

I'm hoping someone can help me bug fix an issue I'm having where an array that contains a single item is read as an object in json data. I'm using .mapto loop over the values like defined here: https://facebook.github.io/react/docs/multiple-components.html
The build I'm using is: https://github.com/newtriks/generator-react-webpack, so I'm not sure if this is a webpack issue, fetch issue (how I'm requesting the data), or something much more obvious. Any insight would be appreciated. Thanks.
I found where the error was. I was using YQL as a proxy service to get around CORS. Turns out the default response is “lossy”, where single element arrays are returned as an object.. had me scratching my head for a while trying to figure out where this was coming from. Solution was to add jsonCompat=new to the query string. Listed in the docs here: https://developer.yahoo.com/yql/guide/response.html#json-to-json-transformation

"Uncaught TypeError: Converting circular structure to JSON" -- Alternatives to JSON.stringify()?

I am using a library, Instafeed, which creates a feed object. The feed object is pretty complex.
I need to convert this object to a string value for transfer.
Problem is, I'm getting the Uncaught TypeError: Converting circular structure to JSON. All of the answer I've come across say to alter the object to get rid of the circular reference. However, the object is pretty complex and I'd rather not have to do that.
Is there any other way to serialize this object that would avoid the circular reference error?
EDIT
Actually, I'm looking at the object in the console and I see this:
I expanded the context key 30 times in total and it just kept going. I'm not having memory issues, and the feed does run fine as intended with the loading of the images as the only delay, so it doesn't seem like this library creating an infinite loop of nested elements.
Is this just a peculiarity of how the object gets displayed and not indicative of the structure of the object itself?
And how do I serialize something like this?
ALSO, I tried a JSON-to-XML plugin but on conversion with that I got the following error:
Uncaught RangeError: Maximum call stack size exceeded
Examining https://github.com/stevenschobert/instafeed.js/blob/master/src/instafeed.coffee, it looks as though the context property will sometimes, but not always, be a circular reference. The one thing context is guaranteed NOT to be is null.
Test for the circular reference while serializing, and if found, serialize it as null. Deserialize on the other side by re-circularizing it.
(This is an answer only to the specific case, not the general - this object has the good grace to be IMMEDIATELY circular. A multi-step circle would be rather more complicated)
JSON is a data format, not a means of saving arbitrary JavaScript objects. So trying to push a JS object which is more than just data over the network via JSON sounds fraught. You might consider whether your design could move just the information that you actually need to move.
That said, it does look like at least one person has tried to solve the circular reference problem in library form.
If you're specifically looking for alternatives, you should check out the Circular JSON package.
Replace your calls to JSON with CircularJSON:
function MyCircularClass() {
this.circular = this;
}
var obj = new MyCircularClass();
var objSerialized = CircularJSON(obj);
console.log(obj);
// ouputs '{ "ciruclar": "~" }'
Here's your silver-bullet: cycle.js. If you're using npm it's available as a package too.
You can use it to de-cycle the object so that it can be serialized with:
var stringForm = JSON.stringify(cycle.decycle(myObject));
Later if you're loading the object back into javascript you can make it cycled again with:
cycle.retrocycle(JSON.parse(stringForm));

"Liferay.Language.get" javascript call returns key instead of value

In our portlet, we are trying to access the language properties in our javascript files using Liferay.Language.get("key").
But Liferay.Language.get("key") returns the key instead of the associated value.
Did any one face similar issue?
We are using Liferay 6.1 EE. And have already seen the LPS-16513
The strangest part is it works on our local boxes but fails on server.
Any pointers other than using ext?
My best guess based on the information you give is that you're asking for a key that does not have a translation associated with it - for these returning the key is the default behaviour.
But then, this question is quite old, you might have already solved it. (If so, please let us know how - if you can remember the cause)
I also can't get it to retrieve a value for a valid key that's specified in a resource bundle in a portlet itself. It will retrieve values for keys in the default Liferay language bundles though. One hack is to add a liferay-hook.xml to your portlet and add something like the following to it:
<language-properties>Language.properties</language-properties>
When I do this it will now retrieve my custom/additional keys. The side effect is that your keys will also be available globally in Liferay.
This does not work for portlet level language.pro.
I have same problem with 6.2 EE too, as an alternative I went for below solution.
<script>
var test = "<liferay-ui:message key='test'/>";
console.log("This is working :: "+test)
</script>
Thank,
Sagar Vyas
You can also get the desired value with the following snippet:
var value='<%=LanguageUtil.get(pageContext, "key") %>';

Categories

Resources