Avatar of Isaac
IsaacFlag for United States of America asked on

JSON error

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/
jQueryJavaScript

Avatar of undefined
Last Comment
Rainer Jeschor

8/22/2022 - Mon
leakim971

ASKER
Isaac

Yes, I dud
ASKER
Isaac

did
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER
Isaac

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

SOLUTION
leakim971

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Rainer Jeschor

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
ASKER
Isaac

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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
Isaac

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?
ASKER
Isaac

I get an error now.... It say "E is undefined"
ASKER
Isaac

Do I need more than just "jquery.linq.min.js" library?
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.