javascript - how to convert 3d array to 2d array - Stack Overflow

I want to convert 3d array to 2d arrayso I have 3d array look like this[[["creative fee (example

I want to convert 3d array to 2d array

so I have 3d array look like this

[
  [
    ["creative fee (example)"],
    [1000]
  ],
  [
    ["Item 1...", "Item 2..."],
    [600, 1000]
  ],
  [
    ["Item 3...", "Item 4..."],
    [400, 600]
  ]
]

and want to convert to 2d array like this (expected result)

[
  ["creative fee (example)", 1000],
  ["Item 1...", 600],
  ["Item 2...", 1000],
  ["Item 3...", 400],
  ["Item 4...", 600]
]

this is what I've tried but it begins too hard

var carry = [
  [
    ["creative fee (example)"],
    [1000]
  ],
  [
    ["Item 1...", "Item 2..."],
    [600, 1000]
  ],
  [
    ["Item 3...", "Item 4..."],
    [400, 600]
  ]
],
result = []
for (var z = 0; z < carry.length; z++) {
  for (var m = 0; m < carry[z].length; m++) {
    for (var t = 0; t < carry[z][m].length; t++) {
     // console.log(carry[z][m][t])
     result[z+t] = []
     result[z+t][m] = carry[z][m][t]
    }
  }
}
console.log(result)

I want to convert 3d array to 2d array

so I have 3d array look like this

[
  [
    ["creative fee (example)"],
    [1000]
  ],
  [
    ["Item 1...", "Item 2..."],
    [600, 1000]
  ],
  [
    ["Item 3...", "Item 4..."],
    [400, 600]
  ]
]

and want to convert to 2d array like this (expected result)

[
  ["creative fee (example)", 1000],
  ["Item 1...", 600],
  ["Item 2...", 1000],
  ["Item 3...", 400],
  ["Item 4...", 600]
]

this is what I've tried but it begins too hard

var carry = [
  [
    ["creative fee (example)"],
    [1000]
  ],
  [
    ["Item 1...", "Item 2..."],
    [600, 1000]
  ],
  [
    ["Item 3...", "Item 4..."],
    [400, 600]
  ]
],
result = []
for (var z = 0; z < carry.length; z++) {
  for (var m = 0; m < carry[z].length; m++) {
    for (var t = 0; t < carry[z][m].length; t++) {
     // console.log(carry[z][m][t])
     result[z+t] = []
     result[z+t][m] = carry[z][m][t]
    }
  }
}
console.log(result)

Anyways thank you in advance

Share Improve this question asked Nov 11, 2020 at 11:59 zummonzummon 9864 gold badges11 silver badges30 bronze badges 1
  • not supported by all browsers, but can be useful, take a look: developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – Calvin Nunes Commented Nov 11, 2020 at 12:01
Add a ment  | 

4 Answers 4

Reset to default 4

You could take a Array#flatMap approach and map pairs.

let data = [[["creative fee (example)"], [1000]], [["Item 1...", "Item 2..."], [600, 1000]], [["Item 3...", "Item 4..."], [400, 600]]],
    result = data.flatMap(([l, r]) => l.map((v, i) => [v, r[i]]));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

This should work for your case, you just need to use the spreed operator(...) to spread items and push them into an array (items) and push the results into an new array (using map in this case)

var array2D = carry.map(el => {
  var items = [];
  el.forEach(e => items.push(...e))
  return items;
});

If you need to avoid Array.flatMap (it was introduced in EMCAScript 2019), the old vanilla way would be:

  carry.map(elt => [].concat(...elt))

By using flatMap:

const result = data.flatMap(([i, j]) => i.map((a, idx) => [a, j[idx]]))

const data = [
      [
        ["creative fee (example)"],
        [1000]
      ],
      [
        ["Item 1...", "Item 2..."],
        [600, 1000]
      ],
      [
        ["Item 3...", "Item 4..."],
        [400, 600]
      ]
    ];

    const result = data.flatMap(([i, j]) => i.map((a, idx) => [a, j[idx]]))

    console.log(result)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745204763a4616539.html

相关推荐

  • javascript - how to convert 3d array to 2d array - Stack Overflow

    I want to convert 3d array to 2d arrayso I have 3d array look like this[[["creative fee (example

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信