Cleaner way to get a key and value from JSON - json

Currently the way that I am taking a JSON string where I "Don't know the contents" and pulling the keys and the values is as follows:
var arr = [{"manager_first_name":"jim","manager_last_name":"gaffigan"}];
var arrLen = arr.length;
for(var i = 0; i < arrLen; i++){
var myKeys = Object.keys(arr[i]);
var keysLen = myKeys.length;
for(var x = 0; x < keysLen; x++){
keyName = myKeys[x];
keyValueStr = "arr[i]."+keyName;
keyValue = eval(keyValueStr);
console.log(keyName+':'+keyValue);
}
}
There has to be a cleaner and more efficient way of doing this. Any suggestion would be appreciated.

Using jQuery you can use http://api.jquery.com/jQuery.parseJSON/ & Object.keys - Than:
var obj = jQuery.parseJSON('{"manager_first_name":"jim","manager_last_name":"gaffigan"}');
var keys = Object.keys(obj);
jQuery.each(keys,function(k, v){
console.log(v);
console.log(obj[v]);
});

Create an object and initialize it with your JSON:
var arr = [{"manager_first_name":"jim","manager_last_name":"gaffigan"}];
var JSONObject = new MyJSONObject(arr);
in your constructor, set the object's properties.

You could use for-in loop which iterates over the enumerable properties of an object, in arbitrary order.
var arr = [{"manager_first_name":"jim","manager_last_name":"gaffigan"}];
for (var i = 0; i < arr.length; i++) {
var currentObj = arr[i];
for (var item in currentObj) {
console.log(item + " : " + currentObj[item]);
}
}
If your array always have only one item, you could omit the outermost for loop and just use arr[0]
var currentObj = arr[0];
for (var item in currentObj) {
console.log(item + " : " + currentObj[item]);
}

Related

Retrieving google form response data

I have a google form, where I need to use item ID and get all the response for that Item.
I have the below script which will timeout if the form has more than 3000 responses, as its inefficient
How Do I optimize it to retrieve all the items in a short span of time
fO.items = ["ItemID1","ItemID2","ItemID3"...];
for (var i = 0; i < responses.length; i++) {
var response = responses[i];
var otherItems = '';
var flag = true;
for (var j = 0; j < fO.items.length; j++) {
var item = form.getItemById(parseInt(fO.items[j]));
if (response.getResponseForItem(item))
var otherItems = otherItems + "\t" + response.getResponseForItem(item).getResponse();
else
flag = false;
}
if (flag) {
columnData.push(otherItems);
responseIds.push(response.getId());
}
}
Currently, your code is getting the item object with the following line:
var item = form.getItemById(parseInt(fO.items[j]));
So, that line of code is reading the Form many times.
You could try getting the item objects once, putting them into a JSON object, and then retrieving them as needed.
I haven't tested this code, and I don't know if it will work, or if it will be faster if it does work. But thought I'd share the idea.
function getSomeAnswers() {
var form,flag,i,item,itemID,itemList,itemsObject,
k,L,L_items,otherItems,responses,response,thisAnswer;
itemList = ["ItemID1","ItemID2","ItemID3"];
form = FormApp.getActiveForm();
responses = FormApp.getActiveForm().getResponses();
itemsObject = {};
L_items = itemList.length;
for (i = 0; i < L; i++) {//Compile a list of item objects
itemID = parseInt(itemList[i]);
itemsObject[itemID] = form.getItemById(itemID);
}
L = responses.length;
for (i = 0; i < L; i++) {
response = responses[i];
otherItems = '';
flag = true;
for (k in itemsObject) {//Loop through every item to get
item = itemsObject[k];
thisAnswer = response.getResponseForItem(item);
Logger.log(thisAnswer)
if (thisAnswer)
otherItems = otherItems + "\t" + response.getResponseForItem(item).getResponse();
else
flag = false;
}
/*
if (flag) {
columnData.push(otherItems);
responseIds.push(response.getId());
}
*/
}
}

How to merge arrays by index AS3

Seeking a way to merge arrays by index like so:
var a1:Array = ["a", "b", "c"];
var a2:Array = ["1", "2", "3"];
var a3:Array = result: ["a", 1, "b", 2, "c", 3]
I've tried concat, push, splice... not getting close. Thanks.
function arrayMerge(arr1:Array, arr2:Array):Array {
var o:Array = new Array();
for (var i:int=0; i<Math.max(arr1.length, arr2.length); i++) {
if (i<arr1.length) o.push(arr1[i]);
if (i<arr2.length) o.push(arr2[i]);
}
return o;
}
Thanks Payam for answer and non-judgement. Here's how I applied your work:
var arr1:Array = ["question1", "question2", "question3"];
var arr2:Array = ["answer1", "answer2", "answer3"];
var o:Array = new Array();
for (var i:int=0; i<Math.max(arr1.length, arr2.length); i++) {
if (i<arr1.length) o.push(arr1[i]);
if (i<arr2.length) o.push(arr2[i]);
}
trace(o); //(question1,answer1,question2,answer2,question3,question3)
#AndyH :
payamsbr is right, but you may work with Vectors or Arrays
Perhaps tl; dr; but this is the principle.
If You want to understand something try those possibilities.
If you don't, just copy and paste some shorter code ;)
var v1:Vector.<String> = new <String>["a", "b", "c"];
var v2:Vector.<uint> = new <uint>[1, 2, 3]; // why do you use String here and not uint?
// if you want to convert a uint to a String, use myUint.toString();
function convertVectorToArray(v1:Vector.<String>,v2:Vector.<uint>):Array{
var mergedArray:Array = new Array();
if (v1.length != v2.length){
throw(new Error(" ***ERROR : the two Vectors or Arrays have not the same lenfgth!"));
}else{
for(var i:uint = 0; i <v1.length ; i++){
mergedArray.push(v1[i]);
mergedArray.push(v2[i]);
}
}
return(mergedArray);
}
function mergeVectors(v1:Vector.<String>,v2:Vector.<uint>):Vector.<Object>{
var mergedVector:Vector.<Object> = new Vector.<Object>();
if (v1.length != v2.length){
throw(new Error(" ***ERROR : the two Vectors or Arrays have not the same length!"));
}
for(var i:uint = 0; i <v1.length ; i++){
mergedVector.push(v1[i] as String);
mergedVector.push(v2[i] as uint);
}
return(mergedVector);
}
var mergedArray:Array = (convertVectorToArray(v1,v2));
var mergedVector:Vector.<Object> = (mergeVectors(v1,v2));
function listArray(arr:Array):String{
var str: String="";
if ((v1.length*2) != (v1.length + v2.length)){
throw(new Error(" ***ERROR : the two Vectors or Arrays have not the same length!"));
}else{
for (var i:uint = 0; i < arr.length ; i++){
str+="typeof(arr[" + i + "]) = " + (typeof(arr[i]) as String).toUpperCase() + ", value = " + arr[i] + "\n";
}
}
return str;
}
function listVector(vect:Vector.<Object>):String{
var str: String = "";
if ((v1.length*2) != (v1.length + v2.length)){
throw(new Error(" ***ERROR : the two Vectors or Arrays have not the same length!"));
}else{
for (var i:uint = 0; i < vect.length ; i++){
str+="typeof(vect[" + i + "]) = " + (typeof(vect[i]) as String).toUpperCase() + ", value = " + vect[i] + "\n";
}
}
return str;
}
trace(listArray(mergedArray));
trace(listVector(mergedVector));
You may add a sort() method if You need it (you didn't told about it)
And Always throw an Error if the 2 Arrays or Vectors don't have the same length!
Throwing an Error is the best way to understand if something goes wrong...
This will avoid You a lot of time if You need to debug Your code!!!
As You can see the output is the same, but if the Vector Class is used correctly, this is more efficient than an Array.
Output :
Since there's a Vector Class, I don't understand a lot of people who chose Arrays instead...
Of course Vector. is a nonsense, but I posted it anyway so You can figure You out the Vector Class.
Output is the same :
typeof(arr[0]) = STRING, value = a
typeof(arr[1]) = NUMBER, value = 1
typeof(arr[2]) = STRING, value = b
typeof(arr[3]) = NUMBER, value = 2
typeof(arr[4]) = STRING, value = c
typeof(arr[5]) = NUMBER, value = 3
typeof(vect[0]) = STRING, value = a
typeof(vect[1]) = NUMBER, value = 1
typeof(vect[2]) = STRING, value = b
typeof(vect[3]) = NUMBER, value = 2
typeof(vect[4]) = STRING, value = c
typeof(vect[5]) = NUMBER, value = 3
I forgot this easiest way if you really want an Array...
Quick done!
var ar1:Array = [1,2,3];
var ar2:Array = ["a","b","c"];
function merge(...arrays):Array {
var result:Array = [];
for(var i:int=0;i<arrays.length;i++){
result = result.concat(arrays[i]);
}
return result;
}
trace(merge(ar1, ar2));
// outputs : 1,2,3,a,b,c
Another possibility :
function populateObject(v1:Vector.<String>, v2:Vector.<uint>):Object{
var obj = new Object();
if ((v1.length*2) != (v1.length + v2.length)){
throw(new Error(" ***ERROR : the two Vectors or Arrays have not the same length!"));
}else{
for (var i:uint = 0; i < v1.length; i++){
obj[v2[i]] = v1[i];
}
}
return obj;
}
var o:Object = populateObject(v1,v2);
function listObject(someObj:Object):void{
var myObj:Object = someObj;
for (var i:String in someObj){
trace(someObj[i] + ": " + i);
}
}
listObject(o);
output =
a: 1
b: 2
c: 3
I think that You have a lot of possibilities to use here even it's my longer answer ;)
If You try those possibilities and understand them, this will certainty help You to think to find the best way to deal with Your issue.
But You may also copy and paste some shorter code.
I just wanted to show You that there's more than one answer.
If you understand this, You will be able to go further with coding.
Have fun ;)
Sincerely.
Nicolas
Best regards.
Nicolas.

Creating variables in a systematical way using loops in AS3?

Is there? Like Let's say I need 5 variables in ActionScript 3.0 and would like to name them as follows:
var myVar_1 = "things";
var myVar_2 = "things";
var myVar_3 = "things";
var myVar_4 = "things";
var myVar_5 = "things";
But instead of having to type them 1 by 1, would it work in a loop? I can't seem to make it work and would really love some help/advice on this matter.
Yes, you can create a dynamic property name using [ ] array access:
var variables:Object = {};
for(var i:int = 0; i < 5; i++){
variables["myVar" + i] = "value " + i;
}
trace(variables.myVar3); // "value 3"
The variables object in this case could be replaced by any dynamic object, including MovieClips.
However, in most cases to store data by index it usually makes more sense to use an array. Example:
var variables:Array = [];
for(var i:int = 0; i < 5; i++){
variables.push("value " + i);
}
trace(variables[3]); // "value 3"
You should use Vector.<String>, Array, Object or Dictionary for that:
var variables:Vector.<String> = new <String>[];
for(var i:int = 0; i<5; i++)
{
variables[i] = "things";
}

How to add data dynamically in JSON object

Sample code
var mydata = [ {id:"1",invdate:"2007-10-01",name:"test",note:"note",amount:"200.00",tax:"10.00",total:"210.00"}, {id:"2",invdate:"2007-10-02",name:"test2",note:"note2",amount:"300.00",tax:"20.00",total:"320.00"}];
I have to get output something like above, have to add dynamically columns and its value to mydata.
I am trying to do something like this but its not working for me.
var mydata = [];
for (var r = 0; r < dataTable.getNumberOfRows(); r++) {
var items = "";
var y ="";
for (var c = 0; c < dataTable.getNumberOfColumns(); c++) {
items += '"'+dataTable.getColumnLabel(c)+'":'+dataTable.getValue(r, c)
if(c!=dataTable.getNumberOfColumns()-1){
items += ",";
}
}
y = "{"+items+"}";
mydata.push(y);
}
above code doesnt works for me. any other way for it
Easy:
oldJsonObj.vector = [] //this creates a new element into the object
foreach(dataTable.getNumberOfRows()){
var x = {}
x.id = XXX;
x.name = XXX;
oldJsonObj.vector.push(x); //adds the element x to array
}
alert(oldJsonObj.vector[i].name); //easy accses
Hope this can help you:
var json = [];
// add an field 'a'
json['a'] = 1;
alert(json["a"]); //1
// add an field 'b'
json['b'] = []; // another json object
...
in your case:
mydata=[];
// add json fields
mydata["id"] = 1;
mydata["invdate"] = "2007-10-01";
mydata["name"] = "test";
//...

Group for Array

Possible to group multiple MC and post it in an Array?
exp:
array([mc1,mc2,mc3],[ac1],[bc1,b2])
If its possible how could it be achieve in a for...loop function?
You are almost there.
var mcArray1:Array = [[mc1, mc2, mc2], [ac1], [bc1,b2]];
//or
var mcArray2:Array = new Array([mc1, mc2, mc2], [ac1], [bc1,b2]);
//you can access them with for loops
var array:Array;
var mc:MovieClip;
for(var i:int = 0; i < mcArray.length; i++)
{
array = mcArray1[i];
for(var j:int = 0; j < array.length; j++)
{
mc = MovieClip(array[j]);
mc.x = 20;//do whatever you want with it.
}
}
I could understand your question wrong, but it seems that you just need multidimensional array, i.e. array of arrays:
var array: Array = new Array(
new Array(mc1,mc2,mc3),
new Array(ac1),
new Array(bc1, b2)
);