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

181
Visualizações
How to get the object from array and make a new array from it

Here is the sample json data that I have:

a = [
    {
        "first_name": "Andrew",
        "last_name": " ",
        "job": "actor",

    },
    {
        "first_name": "Andrew",
        "last_name": "Petrov",
        "job": "",
        "contry": "Russia"
    },
    {
        "first_name": "Andrew",
        "last_name": "Petrov",
        "contry": "Russia"
    },

]

And I want to extract the single object from this array and make a new array from this object.

This is the sample output which I wants to get:

[
    {
        "first_name": "Andrew",
        "last_name": "Petrov",
        "job": "actor",
        "contry": "Russia"
    }
]
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

As simple as a[1]

[n] is the index of the array. Index starts at 0

If you mean to get an object in array in array, a[1][0]

about 4 years ago · Juan Pablo Isaza Relatório

0

I think this should get you to the result you want. Basically what I am doing is using the array.reduce method to iterate over all the objects in the input array and add the properties that are missing in the result object.

let   a = [
  {
    "first_name": "Andrew",
    "last_name": " ",
    "job": "actor",
    
  },
  {
    "first_name": "Andrew",
    "last_name": "Petrov",
    "job": "",
    "contry": "Russia"
  },
  {
    "first_name": "Andrew",
    "last_name": "Petrov",
    "contry": "Russia"
  },
  
]

let result = a.reduce((prevValue, currentValue) => {for (let prop in currentValue) {
    if (currentValue[prop].trim() && !prevValue[prop]) {
    prevValue[prop] = currentValue[prop];    
  }
 
}
 return prevValue;
}, {});

console.log(result);

about 4 years ago · Juan Pablo Isaza Relatório

0

In array you can get the n th item by providing the index number to it.

For example:

a = [10, 11, 12]

if you want's to get the 10 from this array you can do console.log(a[0]) which will log the 10 in your console. In here a[0] 0 is the index.

Same as in your condition: if you wants to get the object from your array you need to pass the index number of that particular object.

In your case, a[0] will return this output:

{
    "first_name": "Andrew",
    "last_name": " ",
    "job": "actor",
}

Now the second case that you want is , you need to create a new array from this object.

There are number of ways to create an array, here is the simple one:

const myNewArray = [a[0]]

If you wrap your object with big brackets, it'll create a new array.

In your case, there are some fields which are missing in objects, to get that, there are number of ways that you can use like: reducer, map, forEach loop etc.

Here is the example with reducer:

let res = a.reduce((xs, cv) => {
    for (let prop in cv) {
        if (cv[prop].trim() && !xs[prop]) {
            xs[prop] = cv[prop];
        }
    }
    return xs;
}, {});

console.log("result is :", res)
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