如何计算数组中的项数

时间:2016-07-26 18:26:31

标签: javascript json

下面是从API调用返回的JSON主体。我使用Postman并希望使用JavaScript创建一个测试来计算返回的JSON中的对象数量(&#34; ... Creating virtual environment... Installing Python packages... ... copying src/cryptography/hazmat/backends/commoncrypto/hmac.py -> build/lib.linux-x86_64-2.7/cryptography/hazmat/backends/commoncrypto copying src/cryptography/hazmat/backends/commoncrypto/__init__.py -> build/lib.linux-x86_64-2.7/cryptography/hazmat/backends/commoncrypto copying src/cryptography/hazmat/backends/commoncrypto/ciphers.py -> build/lib.linux-x86_64-2.7/cryptography/hazmat/backends/commoncrypto running egg_info writing requirements to src/cryptography.egg-info/requires.txt writing src/cryptography.egg-info/PKG-INFO writing top-level names to src/cryptography.egg-info/top_level.txt writing dependency_links to src/cryptography.egg-info/dependency_links.txt writing entry points to src/cryptography.egg-info/entry_points.txt warning: manifest_maker: standard file '-c' not found reading manifest file 'src/cryptography.egg-info/SOURCES.txt' reading manifest template 'MANIFEST.in' no previously-included directories found matching 'docs/_build' warning: no previously-included files matching '*' found under directory 'vectors' writing manifest file 'src/cryptography.egg-info/SOURCES.txt' running build_ext generating cffi module 'build/temp.linux-x86_64-2.7/_padding.c' creating build/temp.linux-x86_64-2.7 generating cffi module 'build/temp.linux-x86_64-2.7/_constant_time.c' generating cffi module 'build/temp.linux-x86_64-2.7/_openssl.c' building '_openssl' extension creating build/temp.linux-x86_64-2.7/build creating build/temp.linux-x86_64-2.7/build/temp.linux-x86_64-2.7 gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c build/temp.linux-x86_64-2.7/_openssl.c -o build/temp.linux-x86_64-2.7/build/temp.linux-x86_64-2.7/_openssl.o gcc: internal compiler error: Killed (program cc1) Please submit a full bug report, with preprocessed source if appropriate. See <file:///usr/share/doc/gcc-4.6/README.Bugs> for instructions. error: command 'gcc' failed with exit status 4 ---------------------------------------- Command "/root/.local/share/letsencrypt/bin/python2.7 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-7RK5lP/cryptography/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace(' ', ' '), __file__, 'exec'))" install --record /tmp/pip-sdFAkd-record/install-record.txt --single-version-externally-managed --compile --install-headers /root/.local/share/letsencrypt/include/site/python2.7/cryptography" failed with error code 1 in /tmp/pip-build-7RK5lP/cryptography You are using pip version 8.0.3, however version 8.1.2 is available. You should consider upgrading via the 'pip install --upgrade pip' command. &#34; s)。 id之类的内容为tests["Only 1 login"] = objects=1,其他内容为PASS

Fail

3 个答案:

答案 0 :(得分:1)

或者只是在阅读数组时计数

var count = 0;
ids.forEach(x => {if (x.id != undefined) count++});
console.log(count); // 2

答案 1 :(得分:0)

如果需要 ONLY 计算在数组中 EACH 对象上设置id属性的次数,可以使用for循环进行迭代。

var ids = []

for(var i = 0; i < obj.length; i++) {
    if(typeof entry.id !== 'undefined') {
        ids.push(entry[i].id);
    }
}

console.log(ids.length)

如果阵列中的每个对象都保证具有obj.length属性,您也可以使用id。你说计算ids的数量,这就是上面的for循环实现的原因,只是为了安全。

答案 2 :(得分:0)

如果JSON仍然是一个字符串,你需要解析它:

var data = JSON.parse(json);

如果您只想知道数组中元素的数量:

data.length

如果某些元素可能缺少ID,并且您不想计算它们:

data.filter(elt => 'id' in elt).length
相关问题