Duktape Copying Objects - javascript

I've got a duktape stack with the item on the top of the stack being effectively a JSON object, built with duk_push_string() / duk_put_prop_string().
My resulting object in javascript land is called 'obj', and I want to create a copy of obj, and store it within the same structure (a cached copy of the original data if you will).
i.e.
obj.id
obj.info
obj.orig.id
obj.orig.info
I though I may be able to do it by duplicating the entry at the top of the stack, then pushing it onto the stack like this:
duk_dup_top(ctx) ;
duk_put_prop_string(ctx, -2, "orig");
But when I run the program, I get a fatal error message: uncaught: 'cyclic input'
Any hints?
Ta, T
I can add json_data in with put_prop if I use duk_decode, but I can't find a way of getting JSON data out (i.e. converting the top to JSON in the first place).

Related

JSON Parse error: Unexpected identifier "undefined" when trying to parse an object from an array (React-Native)

I have created a way to store data the way I want in AsyncStorage. This includes:
Reading a variety of input (various arrays)
Storing the input in a local this.state.object that houses multiple arrays
Stringifying this.state.object
Storing the stringified object in a temp array
Assigning the array with all objects to another state, this.state.allObjects
Stringifying 'this.state.allObjects'
Storing stringified array in AsyncStorage
The end goal of all of this is to have an array of objects that each represent a very different instance of the same type (with different parameters etc.). It may not be the most elegant approach, but it seems to store and load fine.
The issue arises when I try to parse anything from this.state.allObjects[x]. Or rather, it only occurs when I need to access it in a case that is NOT right after I load it.
During my loadFromAsync function, I am perfectly able to execute this.state.allObjects = JSON.parse(fromAsync); to get the array of stringified objects and then var display = JSON.parse(this.state.allObjects[0]).name to retrieve the name of the first parsed object of the array.
Any time outside of this function when I try to call var display = JSON.parse(this.state.allObjects[0]).name or even simpler <Text>{JSON.parse(this.state.allObjects[0]).name}</Text> I receive the following errors:
when running
const parse = JSON.parse(this.state.allObjects[0]);
//JSON Parse error: Unexpected identifier "undefined"
console.log(parse.name);
and when running
const parse = JSON.parse(this.state.allObjects[0]).name;
//JSON Parse error: Unexpected identifier "undefined" + null is not an object (evaluating 'JSON.parse(this.state.allObjects[0]).name
console.log(parse);
and lastly while running
const parse = JSON.parse(this.state.allObjects);
//JSON Parse error: Unexpected EOF + JSON Parse error: Unexpected token ','
I assume that this.state.allObjects changes somewhere within my code, or appends an extra } somewhere, though it really shouldn't. When I display this.state.object and this.state.allObjects[0], their format is exactly the same visually. Adding more objects to this.state.allObjects and displaying each stringified component also works; it is just a matter of actually parsing these components that is not working outside the original load function.
I am extremely stuck. Any advice is appreciated. If I need to change my datatype, that's fine. It's just a bit annoying to have come this far with a stringified array of stringified objects holding arrays and not be able to parse it.
Because this.state.allObjects Already JSON object.
Try JSON.stringify()

The values in '{}' are different with when it expand [duplicate]

Why does Chrome display two differing datasets depending on if you have the object view expanded?
In contracted view, my object has two properties:
In expanded view, my object has three properties:
The object you see in the console is a snapshot of the object at a particular point in time - the time when you logged it. When you expand the object, it will evaluate the properties again.
In the example below, I have created an object with two array properties. I logged it the console, and then I added a third property, c to it.
Only the first two properties are showing still, even though I just added a third property. After expanding the object in the console, I can see the third one. It is the latest state of the object.
If you hover over the little blue i icon, it explains what it has done:
Value below was evaluated just now.
#Gideon Pyzer is right. the properties were caculated and added after expanding the object in the console.
Just add one line code above your debug code and reopen the chrome dev tool, you will see the differences.
obj = Object.freeze(obj); //add this line before your console.log
console.log(obj);
Before:
After:
one similar question of mine:
Why can't I access the attr of the javascript object shown in chrome dev tool
You can clone your object to prevent re-evaluate.
console.log(Object.assign({}, obj));
You can get a real hard copy object by using this module. It will give you the snapshot of the object at moment when you call it.
https://www.npmjs.com/package/nest-object-deep-copy
const nestedHardCopy = require('nest-object-deep-copy');
console.log(nestedHardCopy(obj));

Iterating PHP namespaced objects in ng-repeat

I'm trying to iterate trough array of objects using ng-repeat in Angular. Everything works fine except for an API, which returns json encoded PHP object, which resides in different namespace on server. That leads to situation, where returned object properties contains \ in name and I can't find the way how to access them while using {{Namespace\Property}} syntax in Angular. \ causes syntax error on client side, even when trying to escape this character doubling it.
Logging JSON-decoded object in browser returns correct contruction, but can't figure the way to read specified property.
Example output returned from API:
0: Object
Auth\Username: admin
Auth\Domain: acme.org
Auth\Enabled: true
1: Object
Auth\Username: guest
Auth\Domain: acme.org
Auth\Enabled: false

Does $.each not function on a json node if the node has only one child. Coffeescript

So I am trying to iterate through a json object, but I don't know if it will have one child or many. I am using the $.each jquery function with coffee script like so:
$.each data.searchresults.response.results.result, (i) ->
count = i + 1
console.log data.searchresults.response.result.address.street
Now this works if the "result" node has two instances; however, when it only has one instance it doesn't work. My question is, am I writing the $.each function in a sub optimal way and/or two should I just check how many result nodes exist then decide to either loop or just access the node?
JSON With Two: http://www.zillow.com/webservice/GetSearchResults.htm?zws-id=X1-ZWz1dj9f5y35l7_agge7&address=3925%20edwardsville%20galena%20road&citystatezip=47122
JSON with One: http://www.zillow.com/webservice/GetSearchResults.htm?zws-id=X1-ZWz1dj9f5y35l7_agge7&address=206%20Plum%20lake%20drive&citystatezip=47172
No error when running multiple result node, but here is the error when running it with only one result node:
TypeError: 'undefined' is not an object (evaluating 'data.searchresults.response.results.result[i].address.street')
So the issue I was having (stupid I know) was that inside the .each function I was using the full object path with [i] to access the correct iteration of the object.
So instead of using this console.log data.searchresults.response.result[i].address.street
I adjusted the script to look like this:
$.each data.searchresults.response.results.result, (i,result) ->
count = i + 1
console.log result.address.street

Unable to get properties of JavaScript Object

I'm returning JSON from a php file to a jQuery function, and am getting an object output with the properties agt_id and agt_type. With the webkit dev tools I can print see the object, expand it and see the properties. However, I get undefined when I try:
console.log(output.agt_id);
Here's what I'm seeing in my console with console.log(output):
From your screen shot, it looks like your object is in an Array. You can access the object at index 0 of the Array.
console.log(output[0].agt_id);

Categories