提问人:Tim Morford 提问时间:11/15/2023 更新时间:11/15/2023 访问量:62
Javascript 搜索 JSON 以查找名称返回父对象
Javascript search JSON for name return parent object
问:
使用此JSON对象,我尝试查找xpublisher的父对象:
{
type: "object",
title: "Scalars",
properties: {
stringProperty: {
description: "Property name's description (type is string)",
type: "string",
examples: [
"example",
"sample",
],
xpublisher: [
"Greg",
"Tom",
],
},
writeOnlyStringProperty: {
description: "Notice this only appears in the request.",
xpublisher: [
"John",
"Anna",
],
type: "string",
writeOnly: true,
examples: [
"example",
],
},
minLengthString: {
xpublisher: [
"Nancy",
"Tim",
],
description: "Property name's description (type is string)",
type: "string",
minLength: 4,
examples: [
"example",
],
},
},
}
我想搜索 xpublisher 并返回包含 xpublisher 标签的父对象,因此找到的第一个对象将是:
stringProperty: {
description: "Property name's description (type is string)",
type: "string",
examples: [
"example",
"sample",
],
xpublisher: [
"Greg",
"Tom",
],
},
我尝试了几种不同的方法,包括:
function getPublishers(obj, name) {
let result = [];
for (var key in obj) {
let keyName = obj[key]
console.log(keyName)
if (obj.hasOwnProperty(key)) {
if ("object" == typeof(obj[key])) {
getPublishers(obj[key], name);
} else if (key == name) {
result.push(obj[key]);
}
}
}
return result;
}
我也试图以这种方式找到密钥名称。
function searchObject(obj, name) {
let result = Object.keys(obj).find(k => obj[k][name]);
return result;
}
代码似乎只找到键号,而不是实际上的键名,所以我可以抓取对象。
答:
1赞
Ale_Bianco
11/15/2023
#1
您可以修改函数,以便在找到所需属性时跟踪父对象。
function findParentObject(obj, targetPropertyName, parent = null) {
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
if (key === targetPropertyName) {
return parent;
} else if (typeof obj[key] === 'object' && obj[key] !== null) {
const result = findParentObject(obj[key], targetPropertyName, obj);
if (result !== null) {
return result;
}
}
}
}
return null;
}
const yourJsonObject = {
// ... your JSON object ...
};
const parentObject = findParentObject(yourJsonObject.properties, 'xpublisher');
console.log(parentObject);
您可以使用 JSON 中的对象调用此函数。properties
const parentObject = findParentObject(yourJsonObject.properties, 'xpublisher');
console.log(parentObject);
0赞
muzzletov
11/15/2023
#2
function traverse(object: Object, propertyKey: string) {
if(propertyKey in object) return object;
const keys = Object.keys(object);
for(const key of keys) {
if(typeof object[key] === "object" && !Array.isArray(object[key])) {
const result = traverse(object[key], propertyKey);
if(result !== null) return result;
}
}
return null;
}
0赞
Nina Scholz
11/15/2023
#3
您可以通过检查对象和值来找到。
const
findParent = (object, key) => {
if (!object || typeof object !== 'object') return;
if (key in object) return object;
for (const o of Object.values(object)) {
const p = findParent(o, key);
if (p) return p;
}
},
data = { type: "object", title: "Scalars", properties: { stringProperty: { description: "Property name's description (type is string)", type: "string", examples: ["example", "sample"], xpublisher: ["Greg", "Tom"] }, writeOnlyStringProperty: { description: "Notice this only appears in the request.", xpublisher: ["John", "Anna"], type: "string", writeOnly: true, examples: ["example"] }, minLengthString: { xpublisher: ["Nancy", "Tim"], description: "Property name's description (type is string)", type: "string", minLength: 4, examples: ["example"] } } };
console.log(findParent(data, 'xpublisher'))
.as-console-wrapper { max-height: 100% !important; top: 0; }
评论