Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

159
Visualizações
How do I loop through a 2 d array to filter out the arrays whose data in the array matches the value wanted

This is the array: Local this: Object data: d: results: Array(3)

0: {__metadata: {…}, Id: 1, Title: 'Item 1', staffInChargeId: 99, ID: 1}
1: {__metadata: {…}, Id: 15, Title: 'Item 2', staffInChargeId: 99, ID: 15}
2: {__metadata: {…}, Id: 16, Title: 'Item 3', staffInChargeId: 80, ID: 16}

How can I filter through the array to filter out the arrays where staffInChargeId is equal to the LoginId?

        function LoadLinkResults(LoginId)
        {
                
                            var LoginId = GetID(userIdLoginName);
                        
        
            $.ajax({  
                    url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('userForm')/items?$select=ID,Title,staffInChargeId&?$top=20",  
                    headers: {  
                        Accept: "application/json;odata=verbose"  
                    },  
                   async: false,  
                    success: function(data) {
                                                
                            if (data.d.results.length > 0)
                            {
                                for (var i = 0; i < data.d.results.length; i++)
                                {  
                                    //how do I  filter out the data whose staffInChargeId == LoginId?
                                    var item = data.d.results[i];  
                                    $('#LinkResults').append($("<option></option>").attr("value", item .staffInChargeId).attr("value", item .ID).text(item.Title));                 
                                } 
                                                                        
                                }
                            
                            },            
                        error: function(data) 
                        {
                        console.log("An error occurred. Please try again.");  
                        }
                });
        }
    
    ```
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

One suggestion : Your JSON contains 2 id properties in each object. Ideally there should be one as both contains same value.

Now if I understand your requirement properly, You can use Array.filter() to get the loggedIn user details. Here is the demo :

// Original Array
const inputArr = [
  {Id: 1, Title: 'Item 1', staffInChargeId: 99},
  {Id: 15, Title: 'Item 2', staffInChargeId: 99},
  {Id: 16, Title: 'Item 3', staffInChargeId: 80}
];

// Login Id
const loginId = 80;

// filterout the record based on the login Id.
const filteredObject = inputArr.filter((obj) => obj.staffInChargeId === loginId);

// Result
console.log(filteredObject);

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda