Javascript Array Concat 不工作。为什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12803604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 08:30:03  来源:igfitidea点击:

Javascript Array Concat not working. Why?

javascriptjqueryjquery-ui

提问by Rafael Baptista

So I've created this jqueryui widget. Its creates a div that I can stream errors into. The widget code looks like this:

所以我创建了这个 jqueryui 小部件。它创建了一个 div,我可以将错误导入其中。小部件代码如下所示:

$.widget('ui.miniErrorLog', {
   logStart: "<ul>",   // these next 4 elements are actually a bunch more complicated.
   logEnd:   "</ul>",
   errStart: "<li>",
   errEnd:   "</li>",
   content:  "",
   refs:     [],

   _create: function() { $(this.element).addClass( "ui-state-error" ).hide(); },

   clear: function() { 
      this.content = ""; 
      for ( var i in this.refs )
         $( this.refs[i] ).removeClass( "ui-state-error" );
      this.refs = [];
      $(this.element).empty().hide(); 
   }, 

   addError: function( msg, ref ) {
      this.content += this.errStart + msg + this.errEnd; 
      if ( ref ) {
         if ( ref instanceof Array )
            this.refs.concat( ref );
         else
            this.refs.push( ref );
         for ( var i in this.refs )
            $( this.refs[i] ).addClass( "ui-state-error" );
      }
      $(this.element).html( this.logStart + this.content + this.logEnd ).show();
   }, 

   hasError: function()
   {
      if ( this.refs.length )
         return true;
      return false;
   },
});

I can add error messages into it, and references to page elements that is will put into an error state. I use it to validate dialogs. In the "addError" method I can pass in a single id, or an array of ids, like this:

我可以向其中添加错误消息,以及对将进入错误状态的页面元素的引用。我用它来验证对话框。在“addError”方法中,我可以传入一个 id 或一组 id,如下所示:

$( "#registerDialogError" ).miniErrorLog( 
   'addError', 
   "Your passwords don't match.", 
   [ "#registerDialogPassword1", "#registerDialogPassword2" ] );

But when I pass in an array of id's it doesn't work. The problem is in the following lines (i think):

但是当我传入一组 id 时,它不起作用。问题在于以下几行(我认为):

if ( ref instanceof Array )
   this.refs.concat( ref );
else
   this.refs.push( ref );

Why doesn't that concat work. this.refs and ref are both arrays. So why doesn't the concat work?

为什么那个 concat 不起作用。this.refs 和 ref 都是数组。那么为什么 concat 不起作用呢?

Bonus: am I doing anything else dumb in this widget? It's my first one.

奖励:我在这个小部件中做了其他愚蠢的事情吗?这是我的第一个。

回答by Alcides Queiroz Aguiar

The concat method doesn't change the original array, you need to reassign it.

concat 方法不会改变原始数组,您需要重新分配它。

if ( ref instanceof Array )
   this.refs = this.refs.concat( ref );
else
   this.refs.push( ref );

回答by Konstantin Dinev

Here is the reason why:

原因如下:

Definition and Usage

The concat() method is used to join two or more arrays.

This method does not change the existing arrays, but returns a new array, containing the values of the joined arrays.

定义和用法

concat() 方法用于连接两个或多个数组。

此方法不会更改现有数组,而是返回一个新数组,其中包含连接数组的值。

You need to assign the result of the concatenation back in the array that you have.

您需要将连接的结果分配回您拥有的数组中。

回答by mewc

To expand on Konstantin Dinev:

要扩展 Konstantin Dinev:

.concat()doesn't add to current object, so this will notwork:

.concat()不添加到当前对象,因此这将不会工作:

foo.bar.concat(otherArray);

This will:

这会:

foo.bar = foo.bar.concat(otherArray);

回答by Saurabh Mistry

you have to re-assign value using = to array , that you want to get concated value

您必须使用 = 重新分配值到数组,您希望获得连接值

let array1=[1,2,3,4];
let array2=[5,6,7,8];

array1.concat(array2);
console.log('NOT WORK :  array1.concat(array2); =>',array1);

array1= array1.concat(array2);
console.log('WORKING :  array1 = array1.concat(array2); =>',array1);

回答by PRATHYUSH P

dataArray = dataArray.concat(array2)