0

Given the following sample JSON (stringified from the corresponding JavaScript object), I need to extract this information:

  1. Find the object in persons which has the reference = 2.
  2. If a person with this reference was found, get the name of the person's parent element (here: "B").

In the end, I need to build a new object looking similar to this. This won't be problematic but I'm struggling with how to extract these objects from the source. I tried different approaches with find(), map(), flatMap() and filter() but none of them really worked.

{
  companyName: "B",
  person: {
    "reference": 2,
    "name": "Bob"
  }
}

Source

{
  "root": [
    {
      "companies": [
        {
          "name": "A",
          "persons": [
            {
              "reference": 1,
              "name": "Alex"
            }
          ]
        }
      ]
    },
    {
      "companies": [
        {
          "name": "B",
          "persons": [
            {
              "reference": 2,
              "name": "Bob"
            },
            {
              "reference": 3,
              "name": "Charles"
            }
          ]
        }
      ]
    }
  ]
}

2 Answers 2

1

If you're just interested in the name of the company you can find it using:

const reference = 2;

const company = data.root.flatMap(item => item.companies)
  .find(company => company.persons.some(person => person.reference === reference));

const companyName = company?.name; 

// or if you cannot use optional chaining
const companyName = (company || {}).name;

In data.root.flatMap(item => item.companies) we iterate through all items in root, for each item we select its companies property. Since we don't want a nested array we use flatMap() to flatten the result by 1 level. This leaves us with an array of companies.

After that we'll call find() on the companies array, since we are looking for a specific company name. The criteria of the company is that some() (1 or more) of the persons should match the provided reference. If no match is found null will be returned (from find()).

We then take the find() result (company) and navigate to the name via optional chaining ?.. This will return the name of the company if present, or undefined if company is null

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

Comments

1

You can use array.reduce here

let data = JSON.parse(`{
    "root": [
      {
        "companies": [
          {
            "name": "A",
            "persons": [
              {
                "reference": 1,
                "name": "Alex"
              }
            ]
          }
        ]
      },
      {
        "companies": [
          {
            "name": "B",
            "persons": [
              {
                "reference": 2,
                "name": "Bob"
              },
              {
                "reference": 3,
                "name": "Charles"
              }
            ]
          }
        ]
      }
    ]
  }`)
  
// GET ALL THE COMPANIES DATA
let companies = data.root.reduce(
    (prevValue, currValue) => {
        prevValue.push(...currValue.companies)
        return prevValue
    },
    []
)

// FIND AND CREATE EXPECTED RESULT SET
let results = companies.reduce(
    (prevValue, currValue) => {
        // loop inside a loop, mind it 
        let refPerson = currValue.persons.find((item)=> item.reference == 2)
        if(refPerson){
            prevValue.push({
             companyName: currValue.name,
             person: refPerson
            })
        }
        return prevValue
    },
    []
)

console.log(results)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.