1

I have the following array of objects:

[
  {
    "id": 97,
    "name": "Jon",
    "technologies": [
      {
        "id": 6,
        "name": "React"
      },
      {
        "id": 7,
        "name": "Messageria"
      }
    ]
  },
  {
    "id": 98,
    "name": "Doe",
    "technologies": [
      {
        "id": 2,
        "name": "Javascript"
      },
      {
        "id": 6,
        "name": "React"
      }
    ]
  },
  {
    "id": 99,
    "name": "Mark",
    "technologies": [
      {
        "id": 8,
        "name": "PHP"
      },
      {
        "id": 9,
        "name": "Laravel"
      }
    ]
  }
]

How could I filter this array and return only developers who have, for example, technology with id 6?
The return I need is only developers who have a relationship with technology id 6, however I need other related technologies to also appear to the developer.

I know that through the find method it is possible to do this, but I don't know how to implement it.

const result = developers.find(dev => dev.technologies ?);

What would be the correct form?

1

3 Answers 3

2

You can use filter with some array operations for this task. Something like this:

persons.filter(person => person.technologies.some(tech => tech.id == 6))
Sign up to request clarification or add additional context in comments.

Comments

1

This will return details of person with technology id 6:

persons = [
  {
    "id": 97,
    "name": "Jon",
    "technologies": [
      {
        "id": 6,
        "name": "React"
      },
      {
        "id": 7,
        "name": "Messageria"
      }
    ]
  },
  {
    "id": 98,
    "name": "Doe",
    "technologies": [
      {
        "id": 2,
        "name": "Javascript"
      },
      {
        "id": 6,
        "name": "React"
      }
    ]
  },
  {
    "id": 99,
    "name": "Mark",
    "technologies": [
      {
        "id": 8,
        "name": "PHP"
      },
      {
        "id": 9,
        "name": "Laravel"
      }
    ]
  }
]

persons.filter(person => person.technologies.find(tech => tech.id == 6))

Comments

1

You need to use some for finding whether element exist with specific id or not:

const data = [
  {
    "id": 97,
    "name": "Jon",
    "technologies": [
      {
        "id": 6,
        "name": "React"
      },
      {
        "id": 7,
        "name": "Messageria"
      }
    ]
  },
  {
    "id": 98,
    "name": "Doe",
    "technologies": [
      {
        "id": 2,
        "name": "Javascript"
      },
      {
        "id": 6,
        "name": "React"
      }
    ]
  },
  {
    "id": 99,
    "name": "Mark",
    "technologies": [
      {
        "id": 8,
        "name": "PHP"
      },
      {
        "id": 9,
        "name": "Laravel"
      }
    ]
  }
]

const myFilteredData = data.filter(test => test.technologies.some(data => data.id===6));

console.log(
myFilteredData
)

Comments

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.