Search id in object array with Javascript

Asked

Viewed 798 times

7

Hello, I have the following object array:

[
  {
    "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 object and return only the developers that have, for example, the technology with id 6. The return I need are only the developers who have relationship with the technology id 6, however, I need to also appear the other Technologies linked to the Veloper.

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

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

What would be the right way?

4 answers

7


find returns only the first element found. But as the result may have more than one, an alternative is to use filter.

And as the criterion is "one of the technologies must have id 6", just use find to search within the technologies of each Veloper:

let developers = [
  { "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" }
    ]
  }
];

let result = developers.filter(dev => dev.technologies.find(t => t.id == 6));
console.log(result);

The filter returns the elements for which the condition is true.

If find finds nothing, is returned undefined (which is evaluated as "false"), and if it finds something, it returns the respective element (and any non-null object is considered "true").

So if the search for technologies find an element with id equal to 6, the respective dev is returned by filter.

Another good alternative is to use some instead of find to search in technologies, as indicated by another answer.

5

You can do this using the filter: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filtro

Basically, you need a boolean expression that returns true/false to select an array element, see the example:

var dados = [
  {
    "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"
      }
    ]
  }
];

var x = dados.filter(d => d.technologies.some(tec => tec.id == 6));
console.log(x)

  • 1

    Good, I forgot the some :-)

4

Like technologies is an array, you have to iterate over the array and see if there is an object with the id you want. For example:

developers.find(dev => {
    let hasReact = false;
    dev.technologies.forEach(tech => {
        if (tech.id == 6) {
            hasReact = true;
        }
    });
    return hasReact;
});

Attention that the find find only the first one. If you want everyone use filter.

developers.filter(dev => {
    let hasReact = false;
    dev.technologies.forEach(tech => {
        if (tech.id == 6) {
            hasReact = true;
        }
    });
    return hasReact;
});

1

Well, this is my first contribution. It’s more verbose than the contribution of hkotsubo, but if adapted can serve several other purposes that perhaps the use of filter and find cannot accomplish. Below!

const developers = [
    {
        "id": 97, "name": "Jon",
        "technologies": [
            { "id": 6, "name": "React" },
            { "id": 7, "name": "Messageria" },
            { "id": 9, "name": "Laravel" }
        ]
    },
    {
        "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 tecInf = 6;

function encontrarDevs(tecInf, developers) {
    const devsTecInf = [];
    const qtdDev = developers.length;
    
    for (let i = 0; i < qtdDev; i++) {
        const qtdTec = developers[i].technologies.length;
        for (let j = 0; j < qtdTec; j++) {
            if (developers[i].technologies[j].id === tecInf) {
                devsTecInf.push(developers[i]);
                break;
            }
        }
    }
    return devsTecInf;
}

console.log(encontrarDevs(tecInf, developers));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.