I am retrieving JSON from a live streaming data. For the first call I am getting dataset array with time and value. But in the second JSON dataset array is empty. I want to check if dataset array contains time key.
我正在从实时流数据中检索JSON。对于第一次调用,我将获得具有时间和值的数据集数组。但是在第二个JSON数据集数组中是空的。我想检查数据集数组是否包含时间密钥。
Retrieved JSON after first call:
首次调用后检索到JSON:
{
"activities-heart-intraday": {
"dataset": [{
"time": "00:00:00",
"value": 91
}, {
"time": "00:01:00",
"value": 92
}, {
"time": "00:02:00",
"value": 92
}],
"datasetInterval": 1,
"datasetType": "second"
}
}
Retrieved JSON after second call:
第二次调用后检索JSON:
{
"activities-heart-intraday": {
"dataset": [],
"datasetInterval": 1,
"datasetType": "second"
}
}
I am doing
我在做
var value = JSON.parse(data);
if (value.hasOwnProperty('time')) {
console.log("here");
}
to check if time key exists in the JSON, but it's not working.
检查JSON中是否存在时间密钥,但它不起作用。
How can I check if a particular key exists in the array in json?
如何检查json中数组中是否存在特定键?
3 个解决方案
#1
2
Firstly you have to check that dataset
is not an empty array. Then check that time
is defined.
首先,您必须检查数据集是否为空数组。然后检查时间是否已定义。
This can be solved with:
这可以通过以下方式解决:
if (dataset[0] !== undefined && dataset[0].time !== undefined)
or just:
要不就:
if (dataset[0] && dataset[0].time)
If you want to iterate through the array:
如果要遍历数组:
dataset.forEach(function (data) {
if (data.time) {
// your code
}
});
#2
0
the data has a dataset array so we need to first check if the array is there and then if one of the arrays members has the time property
数据有一个数据集数组,所以我们需要首先检查数组是否存在,然后如果其中一个数组成员有时间属性
if( data.hasOwnProperty('dataset') && data.dataset.length != 0){
if( data.dataset[0].hasOwnProperty('time')){
console.log('true');
}
}
#3
0
Since in JS you can not natively set and access object properties those with a "-
" character in it by dot notation you have define them as strings and use bracket notation instead to set and access them. So you can make a check like this
因为在JS中你不能原生地设置和访问那些带有“ - ”字符的对象属性,用点符号将它们定义为字符串并使用括号表示法来设置和访问它们。所以你可以这样检查一下
data["activities-heart-intraday"].dataset.length > 0 && data["activities-heart-intraday"].dataset.every(e => !!e.time);
#1
2
Firstly you have to check that dataset
is not an empty array. Then check that time
is defined.
首先,您必须检查数据集是否为空数组。然后检查时间是否已定义。
This can be solved with:
这可以通过以下方式解决:
if (dataset[0] !== undefined && dataset[0].time !== undefined)
or just:
要不就:
if (dataset[0] && dataset[0].time)
If you want to iterate through the array:
如果要遍历数组:
dataset.forEach(function (data) {
if (data.time) {
// your code
}
});
#2
0
the data has a dataset array so we need to first check if the array is there and then if one of the arrays members has the time property
数据有一个数据集数组,所以我们需要首先检查数组是否存在,然后如果其中一个数组成员有时间属性
if( data.hasOwnProperty('dataset') && data.dataset.length != 0){
if( data.dataset[0].hasOwnProperty('time')){
console.log('true');
}
}
#3
0
Since in JS you can not natively set and access object properties those with a "-
" character in it by dot notation you have define them as strings and use bracket notation instead to set and access them. So you can make a check like this
因为在JS中你不能原生地设置和访问那些带有“ - ”字符的对象属性,用点符号将它们定义为字符串并使用括号表示法来设置和访问它们。所以你可以这样检查一下
data["activities-heart-intraday"].dataset.length > 0 && data["activities-heart-intraday"].dataset.every(e => !!e.time);