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

235
Visualizações
How Can I Change Constant Array in Javascript?

I have an array data but it declared with const. I am rather confused to change the data in array. Here is my array:

const person = {
   name: "Person A", ---> the data I want to change
   age: 100,
   favDrinks: [
     "coffee",
     "temulawak", --->> the data I want to change
     "tea"
       ],
 greeting: function() {
     console.log("Hello World"); --->> the data I want to change
   }
 }

How can I change the data of person.name, person.favDrinks[1], and greeting function of "Hello World". Is there a possible way to change constant data? I want to change to text strings. Here is my code :

function personName(nama ){
  let person.name = nama;
    person.push("name:" nama );

}

console.log(personName("Molly"));

But they are all wrong. Please help me. Thank you. :)

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You are doing a couple of things wrong.

  1. You are pushing into an object, instead of an array.
  2. You are redefining the object person by using let inside your function.
  3. You can change properties of a const object; however, you cannot reassign it.
  4. Simply add return person
const person = {
  name: "Person A",
  age: 100,
  favDrinks: ["coffee", "temulawak", "tea"],
  greeting: function () {
    console.log("Hello World");
  },
};

function personName(nama) {
  person.name = nama;
  return person;
}

console.log(personName("Molly"));

This is what you'll get after your changes have been made:

{
  name: 'Molly',
  age: 100,
  favDrinks: [ 'coffee', 'temulawak', 'tea' ],
  greeting: [Function: greeting]
}
about 4 years ago · Juan Pablo Isaza Relatório

0

You are using let to create a variable with the name person.name which is not possible and then pushing it into person, in your case this is not what you should do, instead just change the values directly.

function personName(nama) {
    person.name = name
    return person
}

console.log(personName("Molly"));

if a constant variable is a string or a jumber, it cannot be changed. but if it is an array or object it cannot be changed but items in the object or array can be added, removed, or edited.

about 4 years ago · Juan Pablo Isaza Relatório

0

  • person is an object, not an array.
  • You can change properties values of a constant object.

Working Demo :

const person = {
    name: "Person A",
  age: 100,
  favDrinks: [
    "coffee",
    "temulawak",
    "tea"
  ],
    greeting: function() {
    console.log("Hello World");
  }
 };
 
person.name = "Person B";
person.favDrinks[1] = "Soft Drinks";
person.greeting = function() {
    console.log('Hello Guys!');
}

console.log('updated object', person);

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