在lodash中将对象数组的数组转换为单个对象数组

时间:2017-12-08 10:32:34

标签: javascript jquery arrays lodash

我有一个由下面组成的数组对象,由toArray lodash函数生成,它为匹配的值返回对象

  def computeNCK[A](l:List[A], n: Int): List[List[A]] =
    if (n > l.size) Nil
    else l match {
      case _ :: _ if n == 1 => l.map(List(_))
      case hd :: tl => computeNCK(tl, n - 1).map(hd :: _) ::: computeNCK(tl,n)
      case _ => Nil
  }

我想要如下预期结果;

_.filter

我已尝试使用const matchedPositions = _.filter(allPositions, function(o) { return o.company.toLowerCase() === value.toLowerCase(); });//matchedPositions have array of objects let allCheckboxFiltered = [...allCheckboxFiltered, matchedPositions]; //allCheckboxFiltered have below results [ [{serialNo: 9, sector: null, company: "AMERICAN LIFE INSURANCE COMPANY", location: null, position: "INSURANCE Operations MANAGER"}, {serialNo: 9, sector: null, company: "AMERICAN LIFE INSURANCE COMPANY", location: null, position: "INSURANCE Operations MANAGER"}], [{serialNo: 9, sector: null, company: "AMERICAN LIFE INSURANCE COMPANY", location: null, position: "INSURANCE Operations MANAGER"}, {serialNo: 9, sector: null, company: "AMERICAN LIFE INSURANCE COMPANY", location: null, position: "INSURANCE Operations MANAGER"}, {serialNo: 9, sector: null, company: "AMERICAN LIFE INSURANCE COMPANY", location: null, position: "INSURANCE Operations MANAGER"}] ]; 函数生成如下预期结果:

[
{serialNo: 9, sector: null, company: "AMERICAN LIFE INSURANCE COMPANY", location: null, position: "INSURANCE Operations  MANAGER"},
{serialNo: 9, sector: null, company: "AMERICAN LIFE INSURANCE COMPANY", location: null, position: "INSURANCE Operations  MANAGER"},
{serialNo: 9, sector: null, company: "AMERICAN LIFE INSURANCE COMPANY", location: null, position: "INSURANCE Operations  MANAGER"},
{serialNo: 9, sector: null, company: "AMERICAN LIFE INSURANCE COMPANY", location: null, position: "INSURANCE Operations  MANAGER"},
{serialNo: 9, sector: null, company: "AMERICAN LIFE INSURANCE COMPANY", location: null, position: "INSURANCE Operations  MANAGER"}
];

上面的代码没有生成结果,我想要。我尝试了其他的东西,例如reduce也起作用,但都是徒劳的。

由于

6 个答案:

答案 0 :(得分:3)

您可以使用$(...)

reduce

或者只是

var output = temp1.reduce( (a,b) => (a = a.concat(b), a) ,[] ) ;

答案 1 :(得分:1)

为什么不使用lodash flatten https://lodash.com/docs/4.17.4#flatten

答案 2 :(得分:0)

你可以像下面这样使用reduce:



var temp1 = [
[{serialNo: 9, sector: null, company: "AMERICAN LIFE INSURANCE COMPANY", location: null, position: "INSURANCE Operations  MANAGER"},
{serialNo: 9, sector: null, company: "AMERICAN LIFE INSURANCE COMPANY", location: null, position: "INSURANCE Operations  MANAGER"}],
[{serialNo: 9, sector: null, company: "AMERICAN LIFE INSURANCE COMPANY", location: null, position: "INSURANCE Operations  MANAGER"},
{serialNo: 9, sector: null, company: "AMERICAN LIFE INSURANCE COMPANY", location: null, position: "INSURANCE Operations  MANAGER"},
{serialNo: 9, sector: null, company: "AMERICAN LIFE INSURANCE COMPANY", location: null, position: "INSURANCE Operations  MANAGER"}]
];

var result= temp1.reduce((o, m) => m.concat(o), []);
console.log(result);




如果您想要使用更多ES6功能:



var temp1 = [
[{serialNo: 9, sector: null, company: "AMERICAN LIFE INSURANCE COMPANY", location: null, position: "INSURANCE Operations  MANAGER"},
{serialNo: 9, sector: null, company: "AMERICAN LIFE INSURANCE COMPANY", location: null, position: "INSURANCE Operations  MANAGER"}],
[{serialNo: 9, sector: null, company: "AMERICAN LIFE INSURANCE COMPANY", location: null, position: "INSURANCE Operations  MANAGER"},
{serialNo: 9, sector: null, company: "AMERICAN LIFE INSURANCE COMPANY", location: null, position: "INSURANCE Operations  MANAGER"},
{serialNo: 9, sector: null, company: "AMERICAN LIFE INSURANCE COMPANY", location: null, position: "INSURANCE Operations  MANAGER"}]
];

var result= temp1.reduce((o, m) => [...m, ...o], []);
console.log(result);




答案 3 :(得分:0)

您可以使用Array.concat来传播内部数组。

var temp1 = [
  [{
      serialNo: 9,
      sector: null,
      company: "AMERICAN LIFE INSURANCE COMPANY",
      location: null,
      position: "INSURANCE Operations  MANAGER"
    },
    {
      serialNo: 9,
      sector: null,
      company: "AMERICAN LIFE INSURANCE COMPANY",
      location: null,
      position: "INSURANCE Operations  MANAGER"
    }
  ],
  [{
      serialNo: 9,
      sector: null,
      company: "AMERICAN LIFE INSURANCE COMPANY",
      location: null,
      position: "INSURANCE Operations  MANAGER"
    },
    {
      serialNo: 9,
      sector: null,
      company: "AMERICAN LIFE INSURANCE COMPANY",
      location: null,
      position: "INSURANCE Operations  MANAGER"
    },
    {
      serialNo: 9,
      sector: null,
      company: "AMERICAN LIFE INSURANCE COMPANY",
      location: null,
      position: "INSURANCE Operations  MANAGER"
    }
  ]
];

const result = temp1.reduce((res, currentArr) => res.concat(currentArr), [])
console.log(result);

答案 4 :(得分:0)

只需使用Lodash功能

android:drawableLeft

答案 5 :(得分:0)

使用 Lodash _。flattenDeep

csvWriterSuccess = csv.NewWriter(csvSuccess)
csvWriterError = csv.NewWriter(csvError)