在 JavaScript 中,可以使用 hasOwnProperty() 方法来判断 JSON 对象中的字段是否存在。下面是一个示例:

var json = {
  'name': 'John',
  'age': 30,
  'city': 'New York'
};

if (json.hasOwnProperty('name')) {
  console.log('The 'name' field exists in the JSON object.');
} else {
  console.log('The 'name' field does not exist in the JSON object.');
}

输出结果将是:

The 'name' field exists in the JSON object.

你可以根据需要更改字段名称来判断其他字段是否存在。

JavaScript 判断 JSON 字段是否存在 - 使用 hasOwnProperty() 方法

原文地址: https://www.cveoy.top/t/topic/qqOY 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录