Creating new array with specific objects from another array - Javascript

Asked

Viewed 320 times

1

In javascript I have an array "cardsUsuario" and I need to create a newArray only with the values of . text that will enter in the cardUsuario. At some point I need to give a . push and it will be added at the end of the cartsUsuario array an object in this model {text: "A ", value: 11}.

let cartasUsuario = []
let novoArray = []


(3) [{…}, {…}, {…}]
0: {texto: "A♠️", valor: 11}
1: {texto: "2♥️", valor: 2}
2: {texto: "3♥️", valor: 3}

What I need to do to create another array with the concatenated content I receive from . text?

console.log(`${cartasUsuario[0].texto + cartasUsuario[1].texto + cartasUsuario[2].texto} = pontuação ${usuarioValor}`)

I could use Swahili[0]. text, lettersUsuario[1]. text etc but it would look great when more cards come in. I wanted something that would solve this in a more practical way.

  • I really don’t understand very well what you’re trying to do... You want to concatenate all the properties texto of the array objects cartasUsuario or add the property numbers valor? Try [Edit] your question to make it a little clearer. :-)

  • You want to create a new array only with text atrubuto?

  • I tried editing, but that’s right. I need to concatenate all the text properties of the objects in the Sway array.

2 answers

3


To create a new array with only the text property it is necessary to iterate through the letters array and return the text, this can be done like this:

let cartasUsuario = [
{texto: "A♠️", valor: 11},
{texto: "2♥️", valor: 2},
{texto: "3♥️", valor: 3}
]

let novoArray = cartasUsuario.map(c => c.texto)

console.log(novoArray)

0

Thank you so much for your help. I was able to unlock it here. I used something similar to Ewerton Belo propos:

novoArray = Array.from(cartasUsuario, ({ texto }) => texto);

already solved my problem. Thank you to all!

  • Mark with the " " that appears next to the answer, the most appropriate answer to your question. Also, if you want to concatenate and do not know, you can use the join(). Staying novoArray.join()

  • I’m new here, I’m still learning. Thanks for the strength, I’ve already marked there in your reply

Browser other questions tagged

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