简体   繁体   English

如何在 Javascript 中更改常量数组?

[英]How Can I Change Constant Array in Javascript?

I have an array data but it declared with const.我有一个数组数据,但它是用 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".如何更改 person.name、person.favDrinks[1] 的数据以及“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.谢谢你。 :) :)

You are doing a couple of things wrong.你做错了几件事。

  1. You are pushing into an object, instead of an array.您正在推入 object,而不是数组。
  2. You are redefining the object person by using let inside your function.您通过在 function 中使用let来重新定义 object 人。
  3. You can change properties of a const object;您可以更改 const object 的属性; however, you cannot reassign it.但是,您不能重新分配它。
  4. Simply add return person只需添加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]
}

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.您正在使用 let 创建一个名为person.name的变量,这是不可能的,然后将其推送到 person 中,在您的情况下,这不是您应该做的,而是直接更改值。

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.如果一个常量变量是一个字符串或一个jumper,它就不能被改变。 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.但如果它是数组或 object 则无法更改,但可以添加、删除或编辑 object 或数组中的项目。

  • person is an object, not an array. person是 object,而不是数组。
  • You can change properties values of a constant object.您可以更改常量 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);

I am following " Learn javascript " on Scrimba and I got into a weird situation.我在 Scrimba 上关注“学习 javascript”,我遇到了一个奇怪的情况。

I learned that const variables can not be reassigned and there are methods in arrays and objects where you can add or remove elements from them (eg arr.push() )我了解到不能重新分配const变量,并且数组和对象中有一些方法可以在其中添加或删除元素(例如arr.push()

const s = [5, 7, 2];
function editInPlace() {
    "use strict";
    s = [2, 5, 7];
}
editInPlace();

Console error:控制台错误:

Error: SyntaxError: unknown: "s" is read-only (/index.js:1)

But here comes the magic, when they assigned the const array variables new values with specific indexes then it worked well.但神奇的是,当他们为 const 数组变量分配具有特定索引的新值时,它运行良好。

const s = [5, 7, 2];
function editInPlace() {
    "use strict";

    //s = [2, 5, 7];
    s[0] = 2;
    s[1] = 5;
    s[2] = 7;
}
editInPlace();

console.log(s)

Console output:控制台输出:

[2, 5, 7]

Now I am not able to understand why is this happening.现在我不明白为什么会这样。

Looking forward to listen from you.期待您的来信。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM