Capture array element javascript

0

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

listform: [
  {
    id: "1",
    nome: "juana"
  },
  {
    id: "2"
    nome: "maria"
  },
  {
    id: "3"
    nome: "carlos"
  } 
]
    
asked by anonymous 10.10.2018 / 15:38

3 answers

4

Hello, how are you ?! When we talk about Array, we can deal with one-dimensional and multidimensional arrays ...

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

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

Notethatintheaboveimagethecircled0referstotheunidimensionalindex(wehaveonlyoneindexforeach"space" in the array and no more connected to it.

Already 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

Notethatintheimageabove,index0ispointingtomore(2)indices,thatis,beyondeachof"space" in the primary array index, we have 2 more "spaces" in the secondary index.

Remembering that the index count for Arrays always starts from 0 in ascending order.

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

Then, just capture the name "maria" from index 1, we can use the following code:

 meu_array[1][1]

I hope I have helped!

    
10.10.2018 / 16:49
2

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

If you know the position of the object, you can easily capture with listform[1].nome , judging that the position starts at 0, maria is at 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 get the name maria based on your id 2 , just use the indexOf property together 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, you just have to adapt the method to a simpler looping.

    
10.10.2018 / 15:59
2

One of the most straightforward ways to get the result you want is by using find "that 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 id :

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

See it 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 find is not supported in the old Internet Explorer however the documentation page itself has polyfill that you can use if you need this support.

    
10.10.2018 / 16:37