SharePoint, arrays, JSON

I need to use javascript/jquery to push() 2 column items into a multidimensional array.

list example:

Bank                                      ActType
PNC                                                   Savings
CHASE                                               Checking
BOA                                                   Savings
Credit Union                                     Checking

How can I get these corresponding values into an multidimensional array from a list using spservices?  It looks like it can be done with JSON but i've never used it before.

Any ideas?
LVL 5
IsaacSharePoint Client Side DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

leakim971PluritechnicianCommented:
JSON is a notation. Basically the same way you define a string or an array you can define an object using the JSON notation notation

var row1 = { "Bank":"PNC", "ActType":"Savings" };
var rowX = { Bank:"CHASE ", ActType:"Checking" }; // you can omit quote for the key (left part)
var table = []; // var table = new Array(); 
table.push( row1 );
table.push( rowX );
// now table = [ { "Bank":"PNC", "ActType":"Savings" }, { "Bank":"CHASE ", "ActType":"Checking" } ];

Open in new window

0
leakim971PluritechnicianCommented:
How do you have/receive/set this two columns?
0
IsaacSharePoint Client Side DeveloperAuthor Commented:
By querying a sharepoint list called projects using spservices..here's my code

function getProjects() {
         $().SPServices({
                operation: "GetListItems",
                listName: "Projects",
                async: false,
                CAMLViewFields: "<ViewFields><FieldRef Name='ProjectNumber'></FieldRef><FieldRef Name='AmountProjected'></FieldRef></ViewFields>",    
		CAMLRowLimit: 20, 
		CAMLQuery: "<Query><Where><And><Geq><FieldRef Name='ObligationDate' /><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 () {
                        var projNumber = $(this).attr("ows_ProjectNumber");	
						var proAmt = $(this).attr("ows_AmountProjected");	
						arrProjs.push([projNumber+":"+proAmt]);
                    });
                }
            });
}

Open in new window

0
Newly released Acronis True Image 2019

In announcing the release of the 15th Anniversary Edition of Acronis True Image 2019, the company revealed that its artificial intelligence-based anti-ransomware technology – stopped more than 200,000 ransomware attacks on 150,000 customers last year.

IsaacSharePoint Client Side DeveloperAuthor Commented:
my variables 'projNumber' and 'proAmt' is what i'm using to grab the values I need as I loop and I also push() the values into an array as it's looping, but that's totally wrong.
0
Rainer JeschorCommented:
Hi,
this should do the trick as well:
var arrProjs = []; // Initializes the array

function getProjects() {
         $().SPServices({
                operation: "GetListItems",
                listName: "Projects",
                async: false,
                CAMLViewFields: "<ViewFields><FieldRef Name='ProjectNumber'></FieldRef><FieldRef Name='AmountProjected'></FieldRef></ViewFields>",    
		CAMLRowLimit: 20, 
		CAMLQuery: "<Query><Where><And><Geq><FieldRef Name='ObligationDate' /><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 () {
						var tempProject = new Object();
                        tempProject["ProjectNumer"] = $(this).attr("ows_ProjectNumber");	
						tempProject["AmountProjected"] = $(this).attr("ows_AmountProjected");	
						arrProjs.push(tempProject);
                    });
                }
            });
}

Open in new window


HTH
Rainer
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
IsaacSharePoint Client Side DeveloperAuthor Commented:
So how does that work or what does it look like when the push() is done?

Would I be able to look for PNC and get "Savings"?
0
leakim971PluritechnicianCommented:
// now table = [ { "Bank":"PNC", "ActType":"Savings" }, { "Bank":"CHASE ", "ActType":"Checking" } ];

to access an item of an array use : [index]

var item1 = table[0]; // { "Bank":"PNC", "ActType":"Savings" }
var item2 = table[1]; // { "Bank":"CHASE ", "ActType":"Checking" }

to get PNC or Saving use the KEY :
var bank = item1.Bank; // or item1["Bank"]
0
IsaacSharePoint Client Side DeveloperAuthor Commented:
Ahhh....Ok. I see.  Thanks leakim971.

So, Rainer, what you did with spservices, (arrProjs.push(tempProject);)  would the result be the same as what 'leakim971' did?

[ { "Bank":"PNC", "ActType":"Savings" }, { "Bank":"CHASE ", "ActType":"Checking" } ];
0
leakim971PluritechnicianCommented:
In RainerJ code the keys are differents : "ProjectNumer" and "AmountProjected"
0
Rainer JeschorCommented:
Hi,
technically yes, as this is just another (identical) way how to code this but  just using the names from the SPServices sample (and not the sample table from the initial question)
0
IsaacSharePoint Client Side DeveloperAuthor Commented:
Hi leakim971,

sorry for the confusion.  same concept though.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.