作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我通过调用 API 获得响应。就像这样。
[{"id":213132},{"id":241132},{"id":465413},{"id":546351},{"id":164854,"data":[{"rate" : 20}]},
{"id":564656,"data":[{"rate" : 40}]}]
如何检查特定 ID 是否存在关键数据
?
任何帮助都会很棒。谢谢。
最佳答案
您可以先使用Array.find()
获取具有所需ID的对象,然后检查属性data
是否未定义
const myArray = [{"id":213132},{"id":241132},{"id":465413},{"id":546351},{"id":164854,"data":[{"rate" : 20}]}];
function hasData(id) {
const myObject = myArray.find(x => x.id === id);
return typeof myObject !== "undefined" && typeof myObject.data !== "undefined";
}
console.log(hasData(164854));
console.log(hasData(241132));
关于JavaScript : How to check if particular key is exist or not,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53275244/
我是一名优秀的程序员,十分优秀!