is it possible to retrieve and print the data from a json object inside json array without using any index values and specific keys

huangapple go评论96阅读模式
英文:

is it possible to retrieve and print the data from a json object inside json array without using any index values and specific keys

问题

[
{
"id": "628ba44f5a6de600071d16fa",
"@baseType": "LogicalResource",
"isBundle": false,
"isMNP": false,
"businessType": [],
"category": [
{
"id": "628ba3ef5a6de600071d165f",
"name": "Starterpack2",
"description": "Starterpack2",
"code": "RC17",
"version": 2
}
]
}
]

现在我需要检查并打印JSON数组内的JSON对象,如果存在"category",则应该打印出来,并且在将来如果"category"更改,根据传递的参数输出应该打印出来,我们不要硬编码代码。

我已经尝试使用键值来实现,但如果键值更改,则不会打印对象。
例如:

[
{
"id": "628ba44f5a6de600071d16fa",
"@baseType": "LogicalResource",
"isBundle": false,
"isMNP": false,
"businessType": [],
"category": [
{
"id": "628ba3ef5a6de600071d165f",
"name": "Starterpack2",
"description": "Starterpack2",
"code": "RC17",
"version": 2
}
]
}
]

在上面的代码中,我已经打印了"category"对象,但如果"category"更改为"categories",则不会打印,所以我想要一段根据用户提供的参数来读取代码并打印输出的代码。

英文:
[
    {
        "id": "628ba44f5a6de600071d16fa",
        "@baseType": "LogicalResource",
        "isBundle": false,
        "isMNP": false,
        "businessType": [],
        "category": [
            {
                "id": "628ba3ef5a6de600071d165f",
                "name": "Starterpack2",
                "description": "Starterpack2",
                "code": "RC17",
                "version": 2
            }}]

now i need to check and print the JSON Object inside the JSON Array if category is present then it should print and in future if category is changed according to that if we pass parameter the output should print we don't hard code the code

i have tried by using key values it is coming but if the key value changes it is not printing the object
EX:-

[
    {
        "id": "628ba44f5a6de600071d16fa",
        "@baseType": "LogicalResource",
        "isBundle": false,
        "isMNP": false,
        "businessType": [],
        "category": [
            {
                "id": "628ba3ef5a6de600071d165f",
                "name": "Starterpack2",
                "description": "Starterpack2",
                "code": "RC17",
                "version": 2
            }}]

in the above code i have printed category object but if category changed to categories it is not printing so i want a code which can read the code and based on parameters user giving it should be print the output

答案1

得分: 1

尝试这个。

例如:

let a = [{"id": "628ba44f5a6de600071d16fa","category": [
        {
            "id": "628ba3ef5a6de600071d165f",
            "name": "Starterpack2",
            "description": "Starterpack2",
            "code": "RC17",
            "version": 2
        }]}]

function print (values){return (a[0][`${values}`])}

//现在只需传递任何名称,如“category”或将来“categories”

print("category")  //这将返回数组。

现在根据您的需求进行修改。

英文:

Try this.

For Example:

let a = [{"id": "628ba44f5a6de600071d16fa","category": [
        {
            "id": "628ba3ef5a6de600071d165f",
            "name": "Starterpack2",
            "description": "Starterpack2",
            "code": "RC17",
            "version": 2
        }]}]
        
      function print (values){return (a[0][`${values}`])}

     //now just pass any name like "category" or in future "categories"

     print("category")  //this will retrun the array.

Now modify with your requirements.

答案2

得分: 0

好的,以下是代码部分的翻译:

看起来你想获取可以参数化的键的值

const jsonArray = [
  {
    "id": "628ba44f5a6de600071d16fa",
    "@baseType": "LogicalResource",
    "isBundle": false,
    "isMNP": false,
    "businessType": [],
    "category": [
      {
        "id": "628ba3ef5a6de600071d165f",
        "name": "Starterpack2",
        "description": "Starterpack2",
        "code": "RC17",
        "version": 2
      }
    ]
  }
];

const parameter = "category";

const result = jsonArray.find(({ [parameter]: value }) => value);

if (result) {
  console.log(result);
} else {
  console.log(`未找到具有 ${parameter} 的对象`);
}

希望这对你有所帮助。如果有其他问题,请随时提出。

英文:

It seems you want to get the value of the key(that can be parameterized).

const jsonArray = [
  {
    "id": "628ba44f5a6de600071d16fa",
    "@baseType": "LogicalResource",
    "isBundle": false,
    "isMNP": false,
    "businessType": [],
    "category": [
      {
        "id": "628ba3ef5a6de600071d165f",
        "name": "Starterpack2",
        "description": "Starterpack2",
        "code": "RC17",
        "version": 2
      }
    ]
  }
];

const parameter = "category";

const result = jsonArray.find(({ [parameter]: value }) => value);

if (result) {
  console.log(result);
} else {
  console.log(`No object found with ${parameter}`);
}

If this is not what you are looking for, then please add your code snippet for better understanding.

huangapple
  • 本文由 发表于 2023年1月9日 15:23:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75054191.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定