我从firebug控制台输出以下JSON数据,我试图计算第二项“打开”的总数,并按团队对结果进行分组。我从我们的SharePoint列表中提取数据。
"d":
{
"results":
[
{
"__metadata":
{
"id": "Web/Lists(guid'1234578785596655')/Items(53)",
"uri": "https://spteamsite.com/_api/web/lists/GetByTitle('Teams')/Items(53)",
"etag": ""18"",
"type": "SP.Data.TasksListItem"
},
"Team": "Team A",
"taskStatus": "Open"
},
{
"__metadata":
{
"id": "Web/Lists(guid'1234578785596655')/Items(54)",
"uri": "https://spteamsite.com/_api/web/lists/GetByTitle('Teams')/Items(54)",
"etag": ""97"",
"type": "SP.Data.TasksListItem"
},
"Team": "Team B",
"taskStatus": "Open"
},
{
"__metadata":
{
"id": "Web/Lists(guid'1234578785596655')/Items(82)",
"uri": "https://spteamsite.com/_api/web/lists/GetByTitle('Teams')/Items(82)",
"etag": ""65"",
"type": "SP.Data.TasksListItem"
},
"Team": "Team B",
"taskStatus": "Open"
},
{
"__metadata":
{
"id": "Web/Lists(guid'1234578785596655')/Items(97)",
"uri": "https://spteamsite.com/_api/web/lists/GetByTitle('Teams')/Items(97)",
"etag": ""18"",
"type": "SP.Data.TasksListItem"
},
"Team": "Team C",
"taskStatus": "Open"
},
{
"__metadata":
{
"id": "Web/Lists(guid'1234578785596655')/Items(99)",
"uri": "https://spteamsite.com/_api/web/lists/GetByTitle('Teams')/Items(99)",
"etag": ""8"",
"type": "SP.Data.TasksListItem"
},
"Team": "Team E",
"taskStatus": "Open"
},
{
"__metadata":
{
"id": "Web/Lists(guid'1234578785596655')/Items(106)",
"uri": "https://spteamsite.com/_api/web/lists/GetByTitle('Teams')/Items(106)",
"etag": ""44"",
"type": "SP.Data.TasksListItem"
},
"Team": "Team D",
"taskStatus": "Open"
},
我使用了以下JavaScript,但我只得到一个不正确的值。我只得到3,这是不正确的。它应该是300或更多。为简洁起见,我只发布了我的JSON数据的摘录。
// JavaScript source code
$.ajax({
url: _spPageContextInfo.webServerRelativeUrl + "/_api/web/lists/GetByTitle('Teams')/Items?$filter=taskStatus eq 'Open'&$select=Team,taskStatus",
type: "GET",
dataType: "json",
async: "true",
headers: {
"accept": "application/json;odata=verbose",
},
success: function (data) {
var dataArray = [];
var countArray = [];
var results = data.d.results;
for (var i = 0; i < results.length; i++){
for (key in results[i]) {
if (results[i].hasOwnProperty(key)) {
countArray.push(results[i][key]);
}
}
}
for (var i = 0; i < results.length; i++){
var team = Object.keys(results[1]).length; //This is returning only the value of 3
console.log(team);
}
console.log(countArray);
},
error: function(err) {
alert(JSON.stringify(err));
}
});
你能协助解决这个问题吗?
我已根据请求修改了原始JSON数据,以反映我从服务器获取的内容。
答案 0 :(得分:0)
如果我正确理解你的问题,你想按团队分组然后得到开放的总和。 使用linq.js很容易
var aggregatedObject = Enumerable.From(data).GroupBy("$.Team", null,function (key, g) {
return {
Team: key,
Open: g.Sum("$.Open")
}
}).ToArray();
var query = Enumerable.From(jsonObject)
.GroupBy(
"$.City",
null,
"{ City: $, Count: $$.Count() }").ToArray()
答案 1 :(得分:0)
console.log(team)
的输出始终为3,因为当您调用Object.keys(results[1]).length
时,您只是在第一个结果对象中找到属性(键)的数量(来自您的示例JSON数据)。
{
"__metadata":
{
"id": "Web/Lists(guid'1234578785596655')/Items(53)",
"uri": "https://spteamsite.com/_api/web/lists/GetByTitle('Teams')/Items(53)",
"etag": "18",
"type": "SP.Data.TasksListItem"
},
"Team": "Team A",
"taskStatus": "Open"
}
结果的简单循环可以对它们进行分组:
var results = data.d.results;
var openCount = results.length;
var groupedResults = {};
//Total number of open tasks
console.log(openCount);
for (var i = 0; i < openCount; i++) {
var team = results[i]["Team"]
if (!groupedResults[team]) {
groupedResults[team] = [];
}
groupedResults[team].push(results[i]);
}
//Open tasks grouped by team
console.log(groupedResults);
//Count of open tasks for "Team B"
console.log(groupedResults["Team B"].length);
请参阅下面的代码段,了解使用示例数据的完整功能演示。
var data = {
"d":
{
"results":
[
{
"__metadata":
{
"id": "Web/Lists(guid'1234578785596655')/Items(53)",
"uri": "https://spteamsite.com/_api/web/lists/GetByTitle('Teams')/Items(53)",
"etag": "18",
"type": "SP.Data.TasksListItem"
},
"Team": "Team A",
"taskStatus": "Open"
},
{
"__metadata":
{
"id": "Web/Lists(guid'1234578785596655')/Items(54)",
"uri": "https://spteamsite.com/_api/web/lists/GetByTitle('Teams')/Items(54)",
"etag": "97",
"type": "SP.Data.TasksListItem"
},
"Team": "Team B",
"taskStatus": "Open"
},
{
"__metadata":
{
"id": "Web/Lists(guid'1234578785596655')/Items(82)",
"uri": "https://spteamsite.com/_api/web/lists/GetByTitle('Teams')/Items(82)",
"etag": "65",
"type": "SP.Data.TasksListItem"
},
"Team": "Team B",
"taskStatus": "Open"
},
{
"__metadata":
{
"id": "Web/Lists(guid'1234578785596655')/Items(97)",
"uri": "https://spteamsite.com/_api/web/lists/GetByTitle('Teams')/Items(97)",
"etag": "18",
"type": "SP.Data.TasksListItem"
},
"Team": "Team C",
"taskStatus": "Open"
},
{
"__metadata":
{
"id": "Web/Lists(guid'1234578785596655')/Items(99)",
"uri": "https://spteamsite.com/_api/web/lists/GetByTitle('Teams')/Items(99)",
"etag": "8",
"type": "SP.Data.TasksListItem"
},
"Team": "Team E",
"taskStatus": "Open"
},
{
"__metadata":
{
"id": "Web/Lists(guid'1234578785596655')/Items(106)",
"uri": "https://spteamsite.com/_api/web/lists/GetByTitle('Teams')/Items(106)",
"etag": "44",
"type": "SP.Data.TasksListItem"
},
"Team": "Team D",
"taskStatus": "Open"
}
]
}
};
function printResult(value) {
var d = document.createElement('div');
d.innerHTML = JSON.stringify(value);
document.body.appendChild(d);
console.log(value);
}
var results = data.d.results;
var openCount = results.length;
var groupedResults = {};
//Total number of open tasks
printResult("Open tasks for all teams: " + openCount);
for (var i = 0; i < openCount; i++) {
var team = results[i]["Team"]
if (!groupedResults[team]) {
groupedResults[team] = [];
}
groupedResults[team].push(results[i]);
}
//Open tasks grouped by team
printResult(groupedResults);
//Count of open tasks for "Team B"
printResult("Open tasks for Team B: " + groupedResults["Team B"].length);
div {
border: 1px solid black;
margin: 10px;
}