Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将数组传递给js中的对象_Javascript - Fatal编程技术网

Javascript 将数组传递给js中的对象

Javascript 将数组传递给js中的对象,javascript,Javascript,我正在尝试将数组传递给对象: function process(pid, size, time, IOCArray, IOCtimeA, status) { this.pid = pid; this.size = size; this.time = time; this.IOCtimeA = IOCtimeA; // should i use this? for (var j = 0; j < IOCArray.length; j++) {

我正在尝试将数组传递给对象:

function process(pid, size, time, IOCArray, IOCtimeA, status) {
    this.pid = pid;
    this.size = size;
    this.time = time;
    this.IOCtimeA = IOCtimeA; // should i use this?
    for (var j = 0; j < IOCArray.length; j++) {
        this.IOCArray[j] = IOCArray[j];
    } // or something like this?
    this.status = status;
}

proarray[ID] = new process(ID, size, time, IOCArray, IOCtimeA, status);
功能过程(pid、大小、时间、IOCArray、IOCtimeA、状态){
this.pid=pid;
这个。大小=大小;
这个时间=时间;
this.IOCtimeA=IOCtimeA;//我应该使用这个吗?
对于(var j=0;j
现在我如何访问例如
proarray[5].IOCArray[4]

实际上,我不知道如何将“this”用于数组将引用指定给数组。如果更改
IOCtimeA
proarray[ID]。IOCtimeA
也将更改


使用
for
循环,或者使用@adaneo的技巧,您将复制值并在其中创建一个独立的数组。

查看您提供的代码,两种建议的解决方案都不理想,因为如果您将对象的属性设置为另一个数组,您只是设置了对原始数组的引用。如果要修改对象的数组属性,则还要修改原始数组

相反,您必须使用
slice()
方法。这将为您的对象创建一个新的、独立的数组副本

例如:

this.IOCArray=IOCArray.slice()

还值得注意的是,
.slice()
只会生成数组的浅拷贝。嵌套数组将设置对原始嵌套数组的引用,因此,如果您知道任何变量中都有嵌套数组,则还需要
slice()
这些嵌套数组


您可以执行
this.IOCArray=IOCArray.slice(0)?您是否尝试过使用您所说的
proarray[5].IOCArray[4]
?你有什么错误吗?@adeneo谢谢你。。。如图所示,for循环缺少数组的初始化。它需要
this.IOCArray=[]