Capture javascript array element

Asked

Viewed 790 times

1

Hello, how can I capture the name maria of id 2?

listform: [
  {
    id: "1",
    nome: "juana"
  },
  {
    id: "2"
    nome: "maria"
  },
  {
    id: "3"
    nome: "carlos"
  } 
]
  • You want to search for id or by nome?

  • @Marconi want to recover only the second middle values that in the case is id 2 and the name maria

  • I’m not sure if I understand but you can loop the list and return the object (or a property of it) whose id/name is desired

  • Josimara, I wonder if my answer has helped you

3 answers

3

In your code the array is being set incorrectly, the assignment is with = and a few commas are missing.

If you know the position of the object, you can easily capture with listform[1].nome, judging that the position begins at 0, maria is at the object of position 1, example.

let listform = [
  {
    id: "1",
    nome: "juana"
  },
  {
    id: "2",
    nome: "maria"
  },
  {
    id: "3",
    nome: "carlos"
  } 
];

console.log(listform[1].nome);

If you want to take the name maria based on your id 2, just use the indexof property along with a looping, example...

map

let listform = [
  {
    id: "1",
    nome: "juana"
  },
  {
    id: "2",
    nome: "maria"
  },
  {
    id: "3",
    nome: "carlos"
  } 
];

key = listform.map(function(e) { return e.id; }).indexOf('2');

if(key != -1)
    console.log(listform[key].nome)
else
    console.log('Não foi possível encontrar o id')

If you want more support between browsers, just adapt the method for a simpler looping.

3

One of the most direct ways to get the result you want is by using find which allows you to pass a function with the search you want to do and returns the first element that is valid for that search. In your case you must search through the id:

let pessoa = listform.find(p => p.id === "2");

See working:

const listform = [
  {
    id: "1",
    nome: "juana"
  },
  {
    id: "2",
    nome: "maria"
  },
  {
    id: "3",
    nome: "carlos"
  } 
];

let pessoa = listform.find(p => p.id === "2");
console.log(pessoa.nome);

It is important to mention that at this time the find is not supported in the old Internet Explorer however the documentation page itself has the polyfill which you can use if you need this support.

2


Hi, how are you! When we talk about Array, we can talk about one-dimensional and multidimensional arrays...

A one-dimensional array is an array that contains "only one level of indices", for example:

//Criando um array unidimensional
var meu_array = new Array();
meu_array[0] = 'Hello World'; 
meu_array

Imagem representativa de um Array unidimensional

Note that in the image above the circled 0 refers to the unidimencional Dice (we have only one Dice for each "space" in the array and no other linked to it.

A multidimensional array is an array that contains other arrays within it, for example:

// Criando um array multidimensional
var meu_array = new Array();
meu_array[0] = new Array("id:1","nome:juana");
meu_array[1] = new Array("id:2","nome:maria");
meu_array[2] = new Array("id:3","nome:carlos");
meu_array

Imagem representativa de um Array multidimencional

Note that in the image above, the index 0 is pointing to more (2) indices, that is, besides each of "space" in the primary index of the array, we have 2 more "spaces" in the secondary index.

Recalling that the index count for Arrays always start from number 0 in ascending order.

The listform which you are using fits the type of array multidimensional:

Then, capture only in the name "maria" of index 1, we can use the following code:

 meu_array[1][1]

I hope I’ve helped!

Browser other questions tagged

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