更改第一个对象中的值会导致第二个对象更改

时间:2020-09-08 09:52:10

标签: javascript arrays typescript javascript-objects

我有一个对象数组:

/ClassName

我只想在第一个对象中重新分配“ a”,但是当我使用上述代码时,两个“ a”都被重新分配了。并输出:

from importlib import import_module
from pathlib import Path

HERE = Path(__file__).parent

for api_class_file in HERE.glob("apis/*.py"):
    # apis/ChambAvail.py -> ChambAvail
    api_class_name = api_class_file.stem
    if api_class_name.startswith("__"):
        # skip special files such as __init__ and __main__
        continue

    # equivalent to "import apis.ChambAvail"
    api_module = import_module(f"apis.{api_class_name}")
    # equivalent to "apis.ChambAvail.ChambAvail
    api_class = getattr(api_module, api_class_name)
    api.add_resource(api_class, f"/{api_class_name}")

1 个答案:

答案 0 :(得分:0)

如Array fill方法的this documentation中所述

如果第一个参数是一个对象,则数组中的每个插槽都会引用该对象。

可能的解决方案:

const test = Array.from({ length: 2 }).map(() => ({ a: 1, b: 2 }));

test[0].a = 3;

console.log(test);