You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaScript中如何构建对应元素配对的二维数组(适配zingChart)

How to Merge Two Arrays into a 2D Array for ZingChart Values

Hey there! I see you're trying to combine your date array arr1 and value array arr2 into a 2D array that fits ZingChart's values format. Let's walk through a couple of simple, reliable ways to get this done right.

Option 1: Using a Basic for Loop

If you prefer a straightforward approach, a for loop works perfectly here. We'll iterate through each index, pair the corresponding elements from arr1 and arr2, then push the pair into your target array arr4:

const arr1 = ["2018-05-20","2018-05-21"];
const arr2 = [5,4];
const arr4 = [];

for (let i = 0; i < arr1.length; i++) {
  arr4.push([arr1[i], arr2[i]]);
}

console.log(arr4); // Output: [["2018-05-20",5],["2018-05-21",4]]

Option 2: Using Array.map() (More Concise)

For a cleaner, one-liner solution, the map() method is ideal. It lets us transform each element in arr1 by pairing it with the matching element in arr2:

const arr1 = ["2018-05-20","2018-05-21"];
const arr2 = [5,4];
const arr4 = arr1.map((date, index) => [date, arr2[index]]);

console.log(arr4); // Output: [["2018-05-20",5],["2018-05-21",4]]

Quick Note

Make sure that arr1 and arr2 have the same length—if one is longer than the other, you'll end up with undefined in some of your subarrays, which might break your ZingChart config.

Either of these methods will give you the exact 2D array you need for ZingChart's values property. Give them a try!

内容的提问来源于stack exchange,提问作者Deepak

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:38:30