如何计算数组中值的数量?
How to count number of values in array?
我正在尝试计算一个 json 数组的值。
如果 "beta = b",我想计算 "sierra" 的数据数组中 ID 的数量。因此检查 data[].beta 的值与设置为值 'b'.
的环境变量 ("B")
这里的问题是我在数据[]的每次迭代中都没有"sierra"。
{
"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"
}
]
}
}
}]
}
上面json是我在postman看到的response body。 "loop" 是我的 "for" 循环的计数。
编辑 1:
我试过这个:
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);
预期:2
Actual: TypeError: Cannot read property 'sierra' of undefined
Actual: item.relationships.apps.filter is not a function
您可以这样访问它。如果你有多个数据记录,你也可以使用每个循环来计算。
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);
您可以使用以下代码:
pm.response.json().data[0].gamma.sierra.data.filter( d => d.id ).length
希望对您有所帮助。
你可以采取动态的方式,将你想要计算某个内键的对象的键交出来。
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
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); //
}
});
我正在尝试计算一个 json 数组的值。
如果 "beta = b",我想计算 "sierra" 的数据数组中 ID 的数量。因此检查 data[].beta 的值与设置为值 'b'.
的环境变量 ("B")这里的问题是我在数据[]的每次迭代中都没有"sierra"。
{
"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"
}
]
}
}
}]
}
上面json是我在postman看到的response body。 "loop" 是我的 "for" 循环的计数。
编辑 1: 我试过这个:
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);
预期:2
Actual: TypeError: Cannot read property 'sierra' of undefined
Actual: item.relationships.apps.filter is not a function
您可以这样访问它。如果你有多个数据记录,你也可以使用每个循环来计算。
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);
您可以使用以下代码:
pm.response.json().data[0].gamma.sierra.data.filter( d => d.id ).length
希望对您有所帮助。
你可以采取动态的方式,将你想要计算某个内键的对象的键交出来。
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
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); //
}
});