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

157
Visualizações
Push an item to array of array in javascript

Hi I am having an array like below and trying to push the item on map, but i am not able to push, Please tell what wrong i am doing here.

firstClassPrices: [
  [
    "Kan ej ombokas",
    "431",
    "SEK",
    "Företagspris"
  ],
  [
    "Kan ombokas",
    "525",
    "SEK",
    "Företagspris"
  ]
]

trying to push '1' like below:

 let ticketi = firstClassPrices.map((price) => {
      price.push("1");
    });

i need o/p as

firstClassPrices: [
      [
        "Kan ej ombokas",
        "431",
        "SEK",
        "Företagspris",
         "1"
      ],
      [
        "Kan ombokas",
        "525",
        "SEK",
        "Företagspris",
         "1"
      ]
    ]
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You have to return the price

const firstClassPrices = [
  [
    "Kan ej ombokas",
    "431",
    "SEK",
    "Företagspris"
  ],
  [
    "Kan ombokas",
    "525",
    "SEK",
    "Företagspris"
  ]
];

 let ticketi = firstClassPrices.map((price) => {
      // skip if price already includes 1
      if (!price.includes("1")) {
        price.push("1");
      }
      return price;
    });

console.log(ticketi);

about 4 years ago · Juan Pablo Isaza Relatório

0

Your approach mutates the old array and maps undefined.


Instead, you could map the old and new content of the array.

This approach does not mutate the old data.

let ticketi = firstClassPrices.map(price => [...price, '1']); 
about 4 years ago · Juan Pablo Isaza Relatório

0

You can use Array.prototype.map() combined with Array.prototype.concat()

Code:

const firstClassPrices = [["Kan ej ombokas","431","SEK","Företagspris"],["Kan ombokas","525","SEK","Företagspris"]]

const ticketi = firstClassPrices.map(p => p.concat('1'))

console.log(ticketi)

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