JSON error

Isaac
Isaac used Ask the Experts™
on
I get the error,

Message: Object doesn't support this property or method


function buildProjExecution(chartTy<wbr ></wbr>pe) {  
alert("chartType: "+chartType);
   getProjects();
   //var ProjectItem = []; // Initializes the array
   arrProjs = _.uniqObject(arrProjs,true<wbr ></wbr>);
   
   //arrProjs = _.uniq(['ACGY20137912PB','<wbr ></wbr>ACGY201379<wbr ></wbr>12PB','ACG<wbr ></wbr>Y20137912P<wbr ></wbr>B','ACGS20<wbr ></wbr>135001-1',<wbr ></wbr>'ACGS20135<wbr ></wbr>001-1','AC<wbr ></wbr>GS20135001<wbr ></wbr>-2']);
      $.each(ProjectDataArray, function (index, value) {																			
		alert(index + ':' + value);
    });
   alert("Prog Num: "+arrProjs.length);  //<-- 20 values here also after the uniq()
}


//Get values into an array so I can use the _.uniq() to remove the duplicates so I can add funding amount
function getProjects() {

					var fiscalYear = (new Date).getFullYear();					
					var prevYear = fiscalYear - 1;
					beginFY = prevYear + "-10-01"; // 2012-10-01
					endFY = fiscalYear + "-09-30"; // 2013-09-30
					var i=0;
         $().SPServices({
                operation: "GetListItems",
                listName: "ER_Projects",
                async: false,
                CAMLViewFields: "<ViewFields><FieldRef Name='ProjectNumber'></Fie<wbr ></wbr>ldRef><Fie<wbr ></wbr>ldRef Name='AmountProjected'></F<wbr ></wbr>ieldRef></<wbr ></wbr>ViewFields<wbr ></wbr>>",    
		CAMLRowLimit: 20, 
		CAMLQuery: "<Query><Where><And><Geq><<wbr ></wbr>FieldRef Name='ObligationDate' /><Value IncludeTimeValue='TRUE' Type='DateTime'>"+beginFY+<wbr ></wbr>"</Value><<wbr ></wbr>/Geq><Leq><wbr ></wbr><FieldRef Name='ObligationDate' /><Value IncludeTimeValue='TRUE' Type='DateTime'>"+endFY+"<<wbr ></wbr>/Value></L<wbr ></wbr>eq></And><<wbr ></wbr>/Where></Q<wbr ></wbr>uery>",
                completefunc: function (xData, Status) {
                    $(xData.responseXML).SPFil<wbr ></wbr>terNode("z<wbr ></wbr>:row").eac<wbr ></wbr>h(function<wbr ></wbr> () {
							/*ProjectItem = new Object();
							ProjectItem.projectNumber = $(this).attr("ows_ProjectN<wbr ></wbr>umber");
							ProjectItem.amountProjecte<wbr ></wbr>d = $(this).attr("ows_AmountPr<wbr ></wbr>ojected");<wbr ></wbr>		
							ProjectDataArray[i] = ProjectItem;
							i++;*/
							var tempProject = new Object();
                        tempProject["ProjectNumer"<wbr ></wbr>] = $(this).attr("ows_ProjectN<wbr ></wbr>umber");	
						tempProject["AmountProject<wbr ></wbr>ed"] = $(this).attr("ows_AmountPr<wbr ></wbr>ojected");<wbr ></wbr>	
						arrProjs.push(tempProject)<wbr ></wbr>;
							
							/*
						var tempProject = new Object();
                        tempProject["ProjectNumer"<wbr ></wbr>] = $(this).attr("ows_ProjectN<wbr ></wbr>umber");	
						tempProject["AmountProject<wbr ></wbr>ed"] = $(this).attr("ows_AmountPr<wbr ></wbr>ojected");<wbr ></wbr>	
						arrProjs.push(tempProject)<wbr ></wbr>;*/
                    });
                }
            });
}

/*
@function _.uniqObjects
@require Underscore.js and json.stringify
@purpose return an array of objects without duplicated objects.
*/
_.uniqObjects = function( arr ){
	return _.uniq( _.collect( arr, function( x ){
		return JSON.stringify( x );
	}));
};

Open in new window


I got the idea from this site
http://bateru.com/news/2012/03/code-of-the-day-get-unique-objects-from-an-array-of-json-objects/
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
leakim971Multitechnician
Top Expert 2014

Commented:
IsaacSharePoint Client Side Developer

Author

Commented:
Yes, I dud
IsaacSharePoint Client Side Developer

Author

Commented:
did
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

IsaacSharePoint Client Side Developer

Author

Commented:
I also tried it this way but still get the same error:

function buildProjExecution(chartType) {  
   getProjects();
   
   arrProjs = _.uniqObject(arrProjs,true);
   
      $.each(ProjectDataArray, function (index, value) {																			
		alert(index + ':' + value);
    });
   alert("Prog Num: "+arrProjs.length);  //<-- 20 values here also after the uniq()
}


//Get values into an array so I can use the _.uniq() to remove the duplicates so I can add funding amount
function getProjects() {
					var i=0;
         $().SPServices({
                operation: "GetListItems",
                listName: "Project",
                async: false,
                CAMLViewFields: "<ViewFields><FieldRef Name='ProjNumber'></FieldRef><FieldRef Name='AmtProjected'></FieldRef></ViewFields>",    
		CAMLRowLimit: 20, 
		CAMLQuery: "<Query><Where><And><Geq><FieldRef Name='ObDate' /><Value IncludeTimeValue='TRUE' Type='DateTime'>"+beginFY+"</Value></Geq><Leq><FieldRef Name='ObligationDate' /><Value IncludeTimeValue='TRUE' Type='DateTime'>"+endFY+"</Value></Leq></And></Where></Query>",
                completefunc: function (xData, Status) {
                    $(xData.responseXML).SPFilterNode("z:row").each(function () {
							ProjectItem = new Object();
							ProjectItem.projNumber = $(this).attr("ows_ProjNumber");
							ProjectItem.amtProjected = $(this).attr("ows_AmtProjected");		
							ProjectDataArray[i] = ProjectItem;
							i++;
                    });
                }
            });
}

/*
@function _.uniqObjects
@require Underscore.js and json.stringify
@purpose return an array of objects without duplicated objects.
*/
_.uniqObjects = function( arr ){
	return _.uniq( _.collect( arr, function( x ){
		return JSON.stringify( x );
	}));
};

Open in new window


Did I do this part correct?
/*
@function _.uniqObjects
@require Underscore.js and json.stringify
@purpose return an array of objects without duplicated objects.
*/
_.uniqObjects = function( arr ){
	return _.uniq( _.collect( arr, function( x ){
		return JSON.stringify( x );
	}));
};

Open in new window

leakim971Multitechnician
Top Expert 2014
Commented:
did you add it before your script?

<script src="underscore.js"></script>
<script src="youscript.js"></script>

And not :

<script src="youscript.js"></script>
<script src="underscore.js"></script>
Most Valuable Expert 2013
Awarded 2012

Commented:
Hi,
sorry to bring this question to a different road, but I made some good experiences with another library: linq.js (http://linqjs.codeplex.com/)

I put up a sample here:
http://jsfiddle.net/EE_RainerJ/FCYFP/
(simply created an array with 3 distinct elements but added them multiple times).

Con of this library is that the documentation and samples are sometimes really pure, but the Linq syntax and the implemented functions look very promising and I was nearly always able to get to the point what I wanted.

HTH
Rainer
IsaacSharePoint Client Side Developer

Author

Commented:
Sorry for the late response...I had to run some errands.

 leakim971, I added the library before my script.

Rainer, wow! That seems very powerful.  I'll give that a try also.
IsaacSharePoint Client Side Developer

Author

Commented:
Rainer,

Nothing happens when i try what you have in js fiddle....I don't even get any errors.
Some of my "ProjectedAmount" is blank.  Maybe that's why.  How do I check for undefined/blank with the linq?
IsaacSharePoint Client Side Developer

Author

Commented:
I get an error now.... It say "E is undefined"
IsaacSharePoint Client Side Developer

Author

Commented:
Do I need more than just "jquery.linq.min.js" library?
Most Valuable Expert 2013
Awarded 2012
Commented:
Hi,
sorry for the late reply.
Hm, the script on jsfiddle works on IE9, IE10 and FF23.
I used the linq.js only - not the jquery one as I could not find any online version to be used (linked) by jsFiddle.
It seems that the public reference is not the latest version (which could be downloaded from Codeplex).
In jsFiddle I used the shortcut "E" for "Linq.Enumerable" as using Enumerable leads towards an "Undefined".
Had just a quick look into the JS sources and the "full" version has to be used with "Linq.Enumerable" whereas the minified version is just using "Enumerable".
In the download ZIP there is also a HTML page where you can test the Ling expressions.
And as far as I can see, jQUery and the jquery.linq.min.js should be sufficient.
HTH
Rainer

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial