Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/374.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 通过循环将对象添加到数组集合中的对象_Javascript_Arrays_Object - Fatal编程技术网

Javascript 通过循环将对象添加到数组集合中的对象

Javascript 通过循环将对象添加到数组集合中的对象,javascript,arrays,object,Javascript,Arrays,Object,我试图通过循环将一组对象添加到一组数组中的对象中。下面是我的例子,我不确定我的解释是否有意义 var myObject = {x: ["random", "random person"], y: ["tree"]}; var array = [ { username: 'example', tagline: 'value', noises: ['noise', 'sneeze'] }, { username: 'example1', tagline: 'value1',

我试图通过循环将一组对象添加到一组数组中的对象中。下面是我的例子,我不确定我的解释是否有意义

var myObject = {x: ["random", "random person"], y: ["tree"]};

var array = [ {
  username: 'example',
  tagline: 'value',
  noises: ['noise', 'sneeze'] 
}, {
  username: 'example1',
  tagline: 'value1',
  noises: ['quack', 'honk', 'sneeze', 'growl'] 
}, {
  username: 'example2',
  tagline: 'value2', 
  noises: ['what', 'up', 'doc']
}, {
  username: 'example3',
  tagline: 'value3',
  noises: ['ptshshhh', 'spit', 'asdfsadf']
}];

for(var i = 0; i < array.length; i++) {
    array[i].newObject = myObject;
}
如果我从数组[I]中删除I,它将只添加

var myObject = {x: ["random", "random person"], y: ["tree"]};

只有在最后。我希望这样,myObject会作为newObject添加到数组中的每个对象中。

似乎可以工作,但您是否知道您是通过引用而不是值进行复制的

执行此操作时:

array[i].newObject = myObject;
您没有创建myObject的副本并将其分配给数组[i]中对象的属性newObject,而是创建了对myObject的引用。如果您将任何内容更改为newObject,它将影响对它的所有引用

如果这是您想要的,那么很好,如果不是,您将希望使用对象文字来代替您的引用,如下所示:

for(var i = 0; i < array.length; i++) {
    array[i].newObject = {
        x: ["random", "random person"], 
        y: ["tree"]
    };
}

// changing first x element in newObject of array[0]
array[0].newObject.x[0] = 'not random';

// change only effected array[0]
console.log(array[0].newObject.x[0]); // not random
console.log(array[1].newObject.x[0]); // random
for(变量i=0;i
其他解决方案将涉及构造函数,或使用某种克隆函数

在查看阵列状态时,您需要编写另一个函数,可以调用draw、viewer或render。您可以使用console.log()输出到控制台,如果这是客户端代码,也可以对DOM执行一些操作

// simple viewer that just logs username, and state of newObject to console.
var viewer = function(profiles){
    var i=0,len = profiles.length;
    while(i < len){
        console.log('**********')
        console.log('username: '+profiles[i].username);
        console.log('newObject: ' + JSON.stringify(profiles[i].newObject));
        i++;
    }
};
//简单的查看器,只需将用户名和newObject的状态记录到控制台。
变量查看器=函数(配置文件){
变量i=0,len=profiles.length;
而(我
不要将“array”用作变量名。尝试“usersArray”之类的东西,或者只是“users”。只是一个提示;)@andre如果它是一个保留字,他会得到一个语法错误,代码无法运行。在我看来,它很好,你确定对象的属性不存在吗?@user4852194:repl就是这样格式化的,很好。查看
JSON.stringify(数组)
doubt@andre当前位置我100%同意实际项目应该使用对项目有意义的名称。也就是说,我不认为变量名在编写良好的JS中应该经常遇到;函数/方法将这些细节抽象到一个字母的变量名就足够了。以jQuery为例。不过,我确实喜欢带有正式参数名的匈牙利符号,因为您在编写代码和在控制台中确实会看到这些符号。在这种情况下,Arraray真的很蠢。。。
// simple viewer that just logs username, and state of newObject to console.
var viewer = function(profiles){
    var i=0,len = profiles.length;
    while(i < len){
        console.log('**********')
        console.log('username: '+profiles[i].username);
        console.log('newObject: ' + JSON.stringify(profiles[i].newObject));
        i++;
    }
};