0

I have a key value that I am searching for. Each object has the a key of key for each object in the array. I would like to match the term I am searching for to the value of the key and return the value for the sibling with a key of name.

Here is a sample object:

{
  "test1": {
    "functions": {
      "function1": {
        "inputs": [
          {
            "key": "key1",
            "name": "name1"
          },
          {
            "key": "key2",
            "name": "name3"
          },
          {
            "key": "key3",
            "name": "name3"
          }
        ]
      },
      "function2": {
        "inputs": [
          {
            "key": "key4",
            "name": "name4"
          },
          {
            "key": "key5",
            "name": "name5"
          },
          {
            "key": "key6",
            "name": "name6"
          }
        ]
      }
    }
  }
}

Let's say I want to find the name of an input with a key of key4. How would I achieve this in javascript?

1 Answer 1

2

Is this what you want?

var yourObject = {
  "inputs": [
    {
      "key": "callset_name",
      "name": "Callset Name",
      "type": "STRING",
      "required": false,
      "fromPrevious": false,
      "internalOnly": false
    },
    {
      "key": "reference_genome",
      "name": "Reference Genome",
      "description": "A reference genome.",
      "type": "DATASET",
      "required": true,
      "fromPrevious": false,
      "internalOnly": false,
      "constraints": {
        "dataset": {
          "types": [
            "REFERENCE_GENOME"
          ],
          "components": [
            "bwa_reference_genome"
          ]
        }
      }
    },
    {
      "key": "bam",
      "name": "BAM",
      "type": "FILE",
      "required": true,
      "fromPrevious": true,
      "internalOnly": false,
      "constraints": {
        "file": {
          "types": [
            "BAM"
          ],
          "indexedBy": "bai"
        }
      }
    },
    {
      "key": "bai",
      "name": "BAM Index",
      "type": "FILE",
      "required": true,
      "fromPrevious": true,
      "internalOnly": false,
      "constraints": {
        "file": {
          "types": [
            "BAM_INDEX"
          ]
        }
      }
    },
    {
      "key": "dbsnp_vcf",
      "name": "dbSNP VCF",
      "description": "Single Nucleotide Polymorphism Database",
      "type": "FILE",
      "required": false,
      "fromPrevious": false,
      "internalOnly": false,
      "constraints": {
        "file": {
          "types": [
            "VCF"
          ],
          "indexedBy": "dbsnp_vcf_index"
        }
      }
    },
    {
      "key": "dbsnp_vcf_index",
      "name": "dbSNP VCF Index",
      "description": "Single Nucleotide Polymorphism Database Index",
      "type": "FILE",
      "required": false,
      "fromPrevious": false,
      "internalOnly": false,
      "constraints": {
        "file": {
          "types": [
            "VCF_INDEX",
            "VCF_IDX"
          ]
        }
      }
    }
  ]
};

function getData(key, outputKey) {
  const output = yourObject.inputs.find(data => data.key === key);
  if (output && output.hasOwnProperty(outputKey)) {
    return output[outputKey];
  }
  return;
}

console.log(getData('reference_genome', 'name'));

Sign up to request clarification or add additional context in comments.

2 Comments

Yes thank you! This is in line with what I am searching for. However what if inputs was nested somewhere deeper (and I wouldn't be sure of the exact location) within the object and there were multiple inputs arrays. How would I search for it then? I've edited my original question
@ACC YOu can loop through each keys until key is inputs or typeof yout subData is not object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.