我正在尝试计算json数组的值。
如果“ beta = b”,我想计算“ sierra”数据数组中id的数量。因此,请对照设置为值“ b”的环境变量(“ B”)检查data []。beta的值。
这里的问题是我在每次数据迭代中都没有“塞拉利昂” []。
const httpPostOptions = {
headers:
new HttpHeaders(
{
'Content-Type': 'application/json; charset=utf-8',
}),
withCredentials: true,
};
@Injectable({
providedIn: 'root'
})
export class Manager360CommonService {
webApiLocation = this.getApiLocation();
private getApiLocation() {
const port = location.port ? ':56888' : '';
return location.protocol + '//' + location.hostname + port;
}
constructor(private httpClient: HttpClient) { }
httpGet(url: string): Observable<any> {
return this.httpClient.get( this.webApiLocation + url, httpPostOptions)
.pipe(map((response: Response) => {
return response;
}), catchError(error => {
this.onError(error);
return Promise.reject(error);
}));
}
上面的json是我在邮递员中看到的响应正文。 “循环”是我的“ for”循环的计数。
编辑1: 我已经尝试过了:
{
"data": [{
"alpha": "a",
"beta": "b",
"delta": {
"cat": "dog"
},
"gamma": {
"sierra": {
"data": [
{
"type": "alphabet",
"id": "a"
},
{
"type": "alphabet",
"id": "b"
}
]
}
}
},
{
"alpha": "a",
"beta": "b",
"delta": {
"cat": "dog"
},
"bravo": {
"data": [
{
"type": "number",
"id": "1"
},
{
"type": "number",
"id": "2"
}
]
}
}
},
{
"alpha": "x",
"beta": "y",
"delta": {
"cat": "dog"
},
"gamma": {
"sierra": {
"data": [
{
"type": "alphabet",
"id": "c"
},
{
"type": "alphabet",
"id": "d"
}
]
}
}
}]
}
预期:2
1. pm.response.json().data[loop].gamma.sierra.data().id).size()
2. for(var loop =0; loop < pm.response.json().data.length; loop++)
{
if(pm.response.json().data[loop].beta===pm.variables.get("B"))
{
pm.response.json().data.map((item, loop) => {
if(item.gamma){ // check if gamma key is present
console.log(item.gamma.sierra.filter(data =>data.id
).length); //
}
});
result=true;
break;
}
}
pm.expect(true).to.eql(result);
答案 0 :(得分:1)
您可以像这样访问它。如果您有多个数据记录,则也可以使用每个循环进行计算。
a = {
"data": [
{
"alpha": "a",
"beta": "b",
"delta": {
"cat": "dog"
},
"gamma": {
"sierra": {
"data": [
{
"type": "alphabet",
"id": "a"
},
{
"type": "alphabet",
"id": "b"
}
]
}
}
}
]
}
console.log(a.data[0].gamma.sierra.data.length);
答案 1 :(得分:1)
您可以采用动态方法,将要计算特定内键的对象的键移交给
。
function count(object, key, subKey) {
const noObject = o => !o || typeof o !== 'object';
function subCount(object) {
if (noObject(object)) return 0;
if (subKey in object) return 1;
return Object.values(object).reduce((s, o) => s + subCount(o), 0);
}
if (noObject(object)) return 0;
if (key in object) return subCount(object[key]);
return Object.values(object).reduce((s, o) => s + count(o, key, subKey), 0);
}
var data = { data: [{ alpha: "a", beta: "b", delta: { cat: "dog" }, gamma: { sierra: { data: [{ type: "alphabet", id: "a" }, { type: "alphabet", id: "b" }] } } }] };
console.log(count(data, 'sierra', 'id')); // 2
答案 2 :(得分:0)
您可以使用以下代码:
pm.response.json().data[0].gamma.sierra.data.filter( d => d.id ).length
希望有帮助。
答案 3 :(得分:0)
pm.response.json().data.map((item, loop) => {
if(item.beta === "b" && item.gamma){ // check if gamma key is present
console.log(item.gamma.sierra.data.filter(data => data.id).length); //
}
});