objecthub-io / ObjectHub

ObjectHub is an open repository for software objects and a development platform.
2 stars 1 forks source link

cb doesn not handle arrays well #226

Closed panditthecoder closed 7 years ago

panditthecoder commented 7 years ago

the cd function does not handle arrays very well. See below for example:

input for both the JS code snippets is the same { priceDetails:[{store:amazon,cost:23},{store:flipkart,cost:15}]
}

code snippet 1 (which works as expected) JS Code

var alldetails = [];
for (var i=0;i<input.priceDetails.length;i++) {
    alldetails[i] = new Array();
    alldetails[i][0] = input.priceDetails[i].store;
    alldetails[i][1] = input.priceDetails[i].cost;
}
var arrayfirstrow = "--- Seller = "+ (alldetails[0][0]).toString() + " cost = " + (alldetails[0][1]).toString();
cb(null, {message: "Total Cost"+ "<br> allDetails Array " + alldetails + "<br> manual method output" + arrayfirstrow});

Output / result { "message": "Total Cost allDetails Array amazon,23,flipkart,15 manual method output--- Seller = amazon cost = 23" }

code snippet 2 (which does not work as expected) JS Code

var alldetails = [];
for (var i=0;i<input.priceDetails.length;i++) {
    alldetails[i] = new Array();
    alldetails[i]["Store"] = input.priceDetails[i].store; 
    alldetails[i]["Price"] = input.priceDetails[i].cost; 
}
var arrayfirstrow = "--- Seller = "+ (alldetails[0]["Store"]).toString() + " cost = " + (alldetails[0]["Price"]).toString();
cb(null, {message: "Total Cost"+ "<br> allDetails Array " + alldetails + "<br> manual method output" + arrayfirstrow});

Output / result { "message": "Total Cost allDetails Array , manual method output--- Seller = amazon cost = 23" }

objecthub-io commented 7 years ago

Thank you for submitting this issues!

I don't quite understand the following line:

alldetails[i] = new Array();
alldetails[i]["Store"] = input.priceDetails[i].store; 

It might work if you change it to:

alldetails[i] = new Object();

Does that work?

panditthecoder commented 7 years ago

if i change the line alldetails[i] = new Array (); to alldetails[i] = new Object(); the output I get is { "message": "Total Cost allDetails Array [object Object],[object Object] manual method output--- Seller = amazon cost = 23" }

The fix would be in the cb routine where you would need to use Object.keys(obj) to get an ennumeration of the keys and then loop through them to get the values.

hth

objecthub-io commented 7 years ago

Thank you for getting back!

I think the remaining problem has to do with how JavaScript converts arrays of objects into strings.

I don't think it does. So the problem is this line:

cb(null, {message: "Total Cost"+ "<br> allDetails Array " + alldetails + "<br> manual method output" + arrayfirstrow});

And here specifically Array " + alldetails + "<br>.

alldetails is of the type array and JS implicitly turns this into a string. ObjectHub has no influence on this, so it's not an issue in OH.

You can fix this in the following two ways:

  1. Convert alldetails to string using JSON.stringify():
cb(null, {message: "Total Cost"+ "<br> allDetails Array " + JSON.stringify(alldetails) + "<br> manual method output" + arrayfirstrow});
  1. Return alldetails as a property of the returned JSON:
cb(null, {
    message: "Total Cost",
    alldetails: alldetails
});

Does this solve your issue?

panditthecoder commented 7 years ago

yes, you are correct. that is the way native js also handles it so this can be marked as closed.

objecthub-io commented 7 years ago

Thank you, closing this issue now!