加入数组Javascript

Posted

技术标签:

【中文标题】加入数组Javascript【英文标题】:Joining arrays Javascript 【发布时间】:2018-12-20 03:08:33 【问题描述】:

我有两个数组:

Array1 = [
  [
    index: '1',
    value: '100'
  , 
    index: '2',
    value: '200'
  ],
  [
    index: '1.1',
    value: '100'
  , 
    index: '1.2',
    value: '200'
  ]
];

Array2 = [
  [
    index: '10',
    value: '100'
  , 
    index: '20',
    value: '200'
  ],
  [
    index: '10.1',
    value: '100'
  , 
    index: '10.2',
    value: '200'
  ]
]

我怎样才能加入这两个数组,结果数组将是

ResultArray = [
  [
    index: '1',
    value: '100'
  , 
    index: '2',
    value: '200'
  , 
    index: '10',
    value: '100'
  , 
    index: '20',
    value: '200'
  ],
  [
    [
      index: '1.1',
      value: '100'
    , 
      index: '1.2',
      value: '200'
    ], 
      index: '10.1',
      value: '100'
    , 
      index: '10.2',
      value: '200'
    
  ]
]

提前致谢

【问题讨论】:

显示你到目前为止所做的尝试。 您好,欢迎来到 ***!请查看how to ask a good question 指南并适当地编辑您的问题。你如何从这两个数组开始?您使用什么心理过程来确定最终结构? ResultArray = [Array1, Array2]? 看起来就像他想要一个数组中索引中具有十进制值的任何项,以及第二个子数组中的任何其他项 如果你想要ResultArray = [Array1, Array2]?使用spread operator。解决方案是ResultArray = [...Array1, ...Array2] 【参考方案1】:

只需连接源数组,然后循环遍历它们。在每次迭代中,检查index 是浮点数还是整数,并根据决定将结果推送到单独的数组中。

var arr1= [[index:'1',value:'100',index:'2',value:'200'],[index:'1.1',value:'100',index:'1.2',value:'200']];
var arr2 = [[index:'10',value:'100',index:'20',value:'200'],[index:'10.1',value:'100',index:'10.2',value:'200']];

var ints = [],
  floats = [];

function classify(items) 
  items.forEach(item => (/^\d+$/.test(item.index) ? ints : floats).push(item));


arr1.concat(arr2).forEach(classify);
console.log([ints, floats]);

【讨论】:

【参考方案2】:

这可以提高效率,但为简单起见,它分为两个步骤。第一步是将所有对象展平为一个数组。第二种是根据它们的 index 值(整数 vs 浮点数)将它们的内容分箱到一个数组中:

let Array1 = [
  [
    index: '1',
    value: '100'
  , 
    index: '2',
    value: '200'
  ],
  [
    index: '1.1',
    value: '100'
  , 
    index: '1.2',
    value: '200'
  ]
];

let Array2 = [
  [
    index: '10',
    value: '100'
  , 
    index: '20',
    value: '200'
  ],
  [
    index: '10.1',
    value: '100'
  , 
    index: '10.2',
    value: '200'
  ]
]


let ResultArray = [...flatten(Array1), ...flatten(Array2)]
console.log('Step 1: Flatten\n', ResultArray)

ResultArray = ResultArray.reduce((acc, o) => 
  let val = +o['index'];
  (val % 1 === 0) ? acc[0].push(o): acc[1].push(o)
  return acc;
, [[],[]]);
console.log('Step 2: Bin\n', ResultArray)

function flatten(arr) 
  return arr.reduce((acc, val) => Array.isArray(val) ? acc.concat(flatten(val)) : acc.concat(val), []);

【讨论】:

【参考方案3】:

正如 cmets 所说,您可能需要通过索引是整数字符串还是浮点字符串来分隔它们。

这是一个如何进行分离的示例。

Array1 = [
  [
    index: '1',
    value: '100'
  , 
    index: '2',
    value: '200'
  ],
  [
    index: '1.1',
    value: '100'
  , 
    index: '1.2',
    value: '200'
  ]
];

Array2 = [
  [
    index: '10',
    value: '100'
  , 
    index: '20',
    value: '200'
  ],
  [
    index: '10.1',
    value: '100'
  , 
    index: '10.2',
    value: '200'
  ]
]

ResultArray = [[],[]];

// first combine all of the elements.
var arr = [];
[...Array1, ...Array2].forEach(function(el)
  arr.push(...el);
)

// check all elements and set to result array
arr.forEach(function(el)
  // check whether it is integer or not to set to index 0 or 1
  // if Number(el.index) is integer, mod 1 will equal to 0, else it is float.
  var val = Number(el.index);
  if(val % 1 === 0)
    ResultArray[0].push(el);
  else
    ResultArray[1].push(el);
)

console.log(ResultArray);

【讨论】:

【参考方案4】:

此代码仅在 Array1 和 Array2 具有相同大小时才有效。此代码根据各自的键组合Array1和Array2中的对象。

var arr = [];
for (var i = 0; i < Array1.length; i++)
    arr.push(Array1[i].concat(Array2[i]));

console.log(arr);

【讨论】:

以上是关于加入数组Javascript的主要内容,如果未能解决你的问题,请参考以下文章

JavaScriptJavaScript基础

javascriptjavascript常用函数大全

javascriptjavascript设计模式之工厂模式

JavaScriptJavascript闭包

JavaScriptJavascript闭包

JavaScriptJavaScript中的时间函数