如何在javascript中动态合并数组?

时间:2018-04-04 01:05:55

标签: javascript arrays

var series = 
[
    [{
        Name: "A",
        Type: "SC"
    }, {
        Name: "B",
        Type: "SC"
    }, {
        Name: "C",
        Type: "SC"
    }, {
        Name: "D",
        Type: "SC"
    }], 
    {
        ColumnName: "Target",
        Type: "Line"
    },
    {
        ColumnName: "bar",
        Type: "Line"
    }
];

我想合并数组如下

  

var series = [{Name:“A”,Type:“SC”},{Name:“B”,         键入:“SC”},{姓名:“C”,类型:“SC”},{姓名:“D”,         键入:“SC”},{ColumnName:“Target”,类型:“Line”},{         ColumnName:“bar”,类型:“Line”}];

非常感谢任何帮助。

7 个答案:

答案 0 :(得分:1)

这个简单的减少怎么样?

const orderedSeries = series.reduce((orderedAcum, elem) => [
    ...orderedAcum, ...(Array.isArray(elem) ? elem : [elem])
], []);

答案 1 :(得分:1)

Spread将数组导入Array.concat()

const series = [[{"Name":"A","Type":"SC"},{"Name":"B","Type":"SC"},{"Name":"C","Type":"SC"},{"Name":"D","Type":"SC"}],{"ColumnName":"Target","Type":"Line"},{"ColumnName":"bar","Type":"Line"}];

const flattened = [].concat(...series);

console.log(flattened);

答案 2 :(得分:0)

const input = [
  [{
    Name: "A",
    Type: "SC"
  }, {
    Name: "B",
    Type: "SC"
  }, {
    Name: "C",
    Type: "SC"
  }, {
    Name: "D",
    Type: "SC"
  }],
  {
    ColumnName: "Target",
    Type: "Line"
  },
  {
    ColumnName: "bar",
    Type: "Line"
  }
];

const output = input[0];
input.forEach((item, i) => {
  if (i >= 1) output.push(item);
});
console.log(output);

答案 3 :(得分:0)

您可以尝试使用.reduce

var series = 
[
    [{
        Name: "A",
        Type: "SC"
    }, {
        Name: "B",
        Type: "SC"
    }, {
        Name: "C",
        Type: "SC"
    }, {
        Name: "D",
        Type: "SC"
    }], 
    {
        ColumnName: "Target",
        Type: "Line"
    },
    {
        ColumnName: "bar",
        Type: "Line"
    }
];
series.reduce(
    (x,y)=>{
        var merged = [];
        if(Array.isArray(x)) 
        {
            for(var p=0;p<x.length;p++){
                merged.push(x[p]);
            }
        }
        else merged.push(x);
        if(Array.isArray(y)) 
        {
            for(var p=0;p<y.length;p++){
                merged.push(y[p]);
            }
        }
        else merged.push(y);
        return merged;
    }
)

在reduce中添加初始值可以强制数组连接

series.reduce(
    (x,y)=>{
      return x.concat(y);
    },[]
)

答案 4 :(得分:0)

你可以这样做

let newArr = [];
series.forEach((item) => {
  newArr = newArr.concat(item);
});
series = newArr;

答案 5 :(得分:0)

这是一个改变原始阵列而不是创建新阵列的衬垫。

console.clear();

const input = [
  [{
    Name: "A",
    Type: "SC"
  }, {
    Name: "B",
    Type: "SC"
  }, {
    Name: "C",
    Type: "SC"
  }, {
    Name: "D",
    Type: "SC"
  }],
  {
    ColumnName: "Target",
    Type: "Line"
  },
  {
    ColumnName: "bar",
    Type: "Line"
  }
];

input.forEach((item, i) => (item instanceof Array) ? input.push(...item) && input.splice(i, 1) : false)

console.log(input);

答案 6 :(得分:0)

只需将数组的第一个元素与第一个元素连接起来:

&#13;
&#13;
var series = 
[
    [{
        Name: "A",
        Type: "SC"
    }, {
        Name: "B",
        Type: "SC"
    }, {
        Name: "C",
        Type: "SC"
    }, {
        Name: "D",
        Type: "SC"
    }], 
    {
        ColumnName: "Target",
        Type: "Line"
    },
    {
        ColumnName: "bar",
        Type: "Line"
    }
];

console.log(series[0].concat(series.slice(1)));
//to flatten a possibly 2 dimensional array to a one dimensional array:
console.log("flattened:");
console.log(
  series.reduce(
    (all,item)=>all.concat(item),
    []
  )
);
&#13;
&#13;
&#13;

如果要将x维展平为一维数组,可以使用以下代码:

&#13;
&#13;
var flatten = arr => {
  const recur = arr =>
    arr.reduce(
      (all,item)=>
        (!Array.isArray(item))
          ? all.concat(item)
          : all.concat(recur(item)),
        []
    );
  return recur(arr);
}

console.log(
  flatten([
    [1,2,3],
    [
      [
        [4,5],
        [6],[[7]]
      ]
    ],
    8,
    [9,10]
  ])
);
&#13;
&#13;
&#13;