gpt4 book ai didi

JavaScript : How to check if particular key is exist or not

转载 作者:行者123 更新时间:2023-12-02 14:46:32 26 4
gpt4 key购买 nike

我通过调用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com