如何在javascript中去掉数组中的数组

如何在javascript中去掉数组中的数组,javascript,Javascript,当执行此函数时,它将以 [数组(0)]如果没有错误 当它有错误时,它以[array(1)]的形式返回我 与其在数组中返回数组,我只想以这种方式返回一个数组,如果它没有错误[],并且如果它有错误,它应该像['invalid']一样。您可以在数组构造函数中实现一个concatAll方法,然后使用它来展平结果: const addressZip = person.get('addresses').filter((address) => address.get('legacy_id')).filt

当执行此函数时,它将以
[数组(0)]
如果没有错误 当它有错误时,它以
[array(1)]
的形式返回我


与其在数组中返回数组,我只想以这种方式返回一个数组,如果它没有错误
[]
,并且如果它有错误,它应该像
['invalid']

一样。您可以在数组构造函数中实现一个concatAll方法,然后使用它来展平结果:

const addressZip = person.get('addresses').filter((address) => address.get('legacy_id')).filter((newAddress) => (
  getZIPErrors(newAddress.get('zip'))
))

通常,您可以使用
myArray[0]
获取
myArray
的第一个索引处的值。因此,在您的情况下,在右括号后添加
[0]
会将
['invalid']
[['invalid']]中拉出
我不想要0索引值,因为数组中可能有更多元素出现错误。第一项可能有错误。第二项可能为null,第三项可能再次出现错误。因此,与其在数组中显示数组,不如像下面这样返回inb主数组本身['invalid',null,“invalid”]
Array.prototype.concatAll = function() {
  return this.reduce((acc, curr) => acc = acc.concat(curr))
}

const addressZip = person
  .get('addresses')
  .filter(address => address.get('legacy_id'))
  .filter(newAddress => getZIPErrors(newAddress.get('zip')))
  .concatAll()