Skip to content
Advertisement

How do I find a value in a parent key of a specific child in a JSON data set with javascript?

How do I find a value in a parent key of a specific child in a JSON data set? My JSON data looks something like this:

DATA = {
    "result":"Ok",
    "data":[{
        "orderNumber":"Order1",
        "anyKey":"something else",
        "samples":[{
            "sampleNumber":"sample1",
            "anyOtherKey":"something",
            "item123":"abc 123"
        },{
            "sampleNumber":"sample2",
            "anyOtherKey":"something",
            "item123":"def 123"
        }],
        "tests":[{
            "testNumber":"DP2200200-01",
            "testTypeName":"testtype1"
        }]
    },{
        "orderNumber":"Order2",
        "anyKey":"something",
        "samples":[{
            "sampleNumber":"sample2",
            "anyKey":"something",
        }],
        "tests":[{
            "testNumber":"test2",
            "testTypeName":"testtype2"
        }]
    },{
        "orderNumber":"Order3",
        "anyKey":"something",
        "samples":[{
            "sampleNumber":"sample1",
            "anyOtherKey":"something",
            "item123":"xyz 123"
        }]
    }
]}

I need a way to search for string (like abc) inside item123 and return the OrderNumber belonging to it. So far I did look it up by

  let keyword = "abc"
  let mydata = DATA.data.find(order => order.samples.find(sam => sam.item123.includes(keyword)));
  if ( typeof mydata == 'undefined') {
    document.getElementById("orderNumber").innerHTML = "nothing found :-(";
  } else {
    var myOutput = mydata ["orderNumber"];
    document.getElementById("orderNumber").innerHTML = myOutput ;
  }

This works fine as long as the child has the item123, so it works for Order1 but fails for Order3, because Order2 does not have the item123. However some sets don’t have this property.

Another drawback is that it only finds the first occurrence. How can I find all occurrences and return the associated sampleNumber as well?

Advertisement

Answer

First you need to filter out the objects

const filteredData = DATA.data.filter(d => d.samples.find(s => s?.item123?.includes('abc')));

You can then extract the value you need. Here i am extracting orderNumber.

const orderNumbers = filteredData.map(fd => fd.orderNumber);

You can even use flatMap instead of filter and map to filter and extract in the same loop.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement