Hướng dẫn convert multidimensional array to object javascript - chuyển đổi mảng đa chiều thành javascript đối tượng

Tôi có một mảng 2D với các mục tối đa [32] [32]. Và tôi muốn chuyển đổi nó từ một cái gì đó như thế này:

[
    null, 
    null, 
    null, 
    null, 
    null, 
    null, 
    [null, null, null, null, null, null, null, null, null, null, "player1"],
    [null, null, null, null, null, "player2"]
]

đến

{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}

Vì vậy, đây sẽ là mảng của tôi:

var gameField = [];
gameField[6] = [];
gameField[6][10] = "player1";
gameField[7] = [];
gameField[7][5] = "player2";

Bây giờ tôi đã cố gắng sử dụng điều này:

var obj = {}
obj = Object.assign({},gameField);
console.log(JSON.stringify(obj));

Nhưng nó chỉ hoạt động cho mảng bên ngoài, các mảng bên trong không bị ảnh hưởng:

{
    "6": [null, null, null, null, null, null, null, null, null, null, "player1"],
    "7": [null, null, null, null, null, "player2"]
}

Điều gì sẽ là cách ngắn nhất để làm điều này đúng cách?

Phương pháp nào được sử dụng để chuyển đổi các mảng lồng nhau thành đối tượng trong JavaScript?

Example:

Input: [
         ["John", 12],
         ["Jack", 13],
         ["Matt", 14],
         ["Maxx", 15]
       ]

Output: {
          "John": 12,
          "Jack": 13,
          "Matt": 14,
          "Maxx": 15
        }

Khi chuyển đổi một đối tượng thành một mảng, chúng ta sẽ sử dụng phương thức .entries () từ lớp đối tượng. Điều này sẽ chuyển đổi đối tượng của chúng tôi thành một mảng mảng. Mỗi mảng lồng nhau là một danh sách hai giá trị trong đó mục đầu tiên là khóa và mục thứ hai là giá trị.

{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
5
{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
12
{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
13
In this approach, we create an empty object and use the Array.forEach() method to iterate over the array. On every iteration, we insert the first item of the child array into the object as a key and the second item as the value. Then it returns the object after the iterations.

Example:

JavaScript

function

{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
0

{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
1
{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
2

{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
1
{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
4

{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
5
{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
6

{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
1
var gameField = [];
gameField[6] = [];
gameField[6][10] = "player1";
gameField[7] = [];
gameField[7][5] = "player2";
4
Input: [
         ["John", 12],
         ["Jack", 13],
         ["Matt", 14],
         ["Maxx", 15]
       ]

Output: {
          "John": 12,
          "Jack": 13,
          "Matt": 14,
          "Maxx": 15
        }
3

{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
5
var gameField = [];
gameField[6] = [];
gameField[6][10] = "player1";
gameField[7] = [];
gameField[7][5] = "player2";
0

{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
1
var gameField = [];
gameField[6] = [];
gameField[6][10] = "player1";
gameField[7] = [];
gameField[7][5] = "player2";
2

Cách tiếp cận 3: Theo cách tiếp cận này, trước tiên chúng tôi làm phẳng mảng bằng phương thức mảng.flat () để chúng tôi nhận được một mảng một chiều. Sau đó, chúng ta có thể tạo một đối tượng trống và lặp lại mảng để gán các giá trị được định vị đồng đều làm khóa của đối tượng và các giá trị được định vị kỳ lạ & nbsp; làm giá trị.

var gameField = [];
gameField[6] = [];
gameField[6][10] = "player1";
gameField[7] = [];
gameField[7][5] = "player2";
6

var gameField = [];
gameField[6] = [];
gameField[6][10] = "player1";
gameField[7] = [];
gameField[7][5] = "player2";
7

{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
1
var gameField = [];
gameField[6] = [];
gameField[6][10] = "player1";
gameField[7] = [];
gameField[7][5] = "player2";
9

{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
5
var obj = {}
obj = Object.assign({},gameField);
console.log(JSON.stringify(obj));
1
var obj = {}
obj = Object.assign({},gameField);
console.log(JSON.stringify(obj));
2
var obj = {}
obj = Object.assign({},gameField);
console.log(JSON.stringify(obj));
3

{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
5
var obj = {}
obj = Object.assign({},gameField);
console.log(JSON.stringify(obj));
1
var obj = {}
obj = Object.assign({},gameField);
console.log(JSON.stringify(obj));
6
var obj = {}
obj = Object.assign({},gameField);
console.log(JSON.stringify(obj));
7

{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
5
var obj = {}
obj = Object.assign({},gameField);
console.log(JSON.stringify(obj));
1
{
    "6": [null, null, null, null, null, null, null, null, null, null, "player1"],
    "7": [null, null, null, null, null, "player2"]
}
0
{
    "6": [null, null, null, null, null, null, null, null, null, null, "player1"],
    "7": [null, null, null, null, null, "player2"]
}
1

{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
5
var obj = {}
obj = Object.assign({},gameField);
console.log(JSON.stringify(obj));
1
{
    "6": [null, null, null, null, null, null, null, null, null, null, "player1"],
    "7": [null, null, null, null, null, "player2"]
}
4
{
    "6": [null, null, null, null, null, null, null, null, null, null, "player1"],
    "7": [null, null, null, null, null, "player2"]
}
5

{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
1
{
    "6": [null, null, null, null, null, null, null, null, null, null, "player1"],
    "7": [null, null, null, null, null, "player2"]
}
7

{
    "6": [null, null, null, null, null, null, null, null, null, null, "player1"],
    "7": [null, null, null, null, null, "player2"]
}
8

Output:

{
  Jack: 13,
  John: 12,
  Matt: 14,
  Maxx: 15
}

{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
1
{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
09
{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
10
In this approach, we will use the Array.reduce() method and initialize the accumulator with an empty object. On every iteration, we assign the current value as the key’s value of the accumulator and return the accumulator. Then it returns the object after the iterations.

Example:

JavaScript

function

{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
0

{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
1
var gameField = [];
gameField[6] = [];
gameField[6][10] = "player1";
gameField[7] = [];
gameField[7][5] = "player2";
4
Input: [
         ["John", 12],
         ["Jack", 13],
         ["Matt", 14],
         ["Maxx", 15]
       ]

Output: {
          "John": 12,
          "Jack": 13,
          "Matt": 14,
          "Maxx": 15
        }
3

{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
5
Input: [
         ["John", 12],
         ["Jack", 13],
         ["Matt", 14],
         ["Maxx", 15]
       ]

Output: {
          "John": 12,
          "Jack": 13,
          "Matt": 14,
          "Maxx": 15
        }
5

Input: [
         ["John", 12],
         ["Jack", 13],
         ["Matt", 14],
         ["Maxx", 15]
       ]

Output: {
          "John": 12,
          "Jack": 13,
          "Matt": 14,
          "Maxx": 15
        }
6
Input: [
         ["John", 12],
         ["Jack", 13],
         ["Matt", 14],
         ["Maxx", 15]
       ]

Output: {
          "John": 12,
          "Jack": 13,
          "Matt": 14,
          "Maxx": 15
        }
7

Input: [
         ["John", 12],
         ["Jack", 13],
         ["Matt", 14],
         ["Maxx", 15]
       ]

Output: {
          "John": 12,
          "Jack": 13,
          "Matt": 14,
          "Maxx": 15
        }
6
Input: [
         ["John", 12],
         ["Jack", 13],
         ["Matt", 14],
         ["Maxx", 15]
       ]

Output: {
          "John": 12,
          "Jack": 13,
          "Matt": 14,
          "Maxx": 15
        }
9

Input: [
         ["John", 12],
         ["Jack", 13],
         ["Matt", 14],
         ["Maxx", 15]
       ]

Output: {
          "John": 12,
          "Jack": 13,
          "Matt": 14,
          "Maxx": 15
        }
6
{
  Jack: 13,
  John: 12,
  Matt: 14,
  Maxx: 15
}
1

Input: [
         ["John", 12],
         ["Jack", 13],
         ["Matt", 14],
         ["Maxx", 15]
       ]

Output: {
          "John": 12,
          "Jack": 13,
          "Matt": 14,
          "Maxx": 15
        }
6
var gameField = [];
gameField[6] = [];
gameField[6][10] = "player1";
gameField[7] = [];
gameField[7][5] = "player2";
4
{
  Jack: 13,
  John: 12,
  Matt: 14,
  Maxx: 15
}
4

{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
5
{
  Jack: 13,
  John: 12,
  Matt: 14,
  Maxx: 15
}
6

{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
5
{
  Jack: 13,
  John: 12,
  Matt: 14,
  Maxx: 15
}
8

{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
1
{
    "6": [null, null, null, null, null, null, null, null, null, null, "player1"],
    "7": [null, null, null, null, null, "player2"]
}
8

var gameField = [];
gameField[6] = [];
gameField[6][10] = "player1";
gameField[7] = [];
gameField[7][5] = "player2";
6

var gameField = [];
gameField[6] = [];
gameField[6][10] = "player1";
gameField[7] = [];
gameField[7][5] = "player2";
7

{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
1
var gameField = [];
gameField[6] = [];
gameField[6][10] = "player1";
gameField[7] = [];
gameField[7][5] = "player2";
9

{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
5
var obj = {}
obj = Object.assign({},gameField);
console.log(JSON.stringify(obj));
1
{
  Eren: 'Yeager',
  Mikasa: 'Ackermann',
  Armin: 'Arlelt',
  Levi: 'Ackermann'
}
7
{
  Eren: 'Yeager',
  Mikasa: 'Ackermann',
  Armin: 'Arlelt',
  Levi: 'Ackermann'
}
8
{
  Eren: 'Yeager',
  Mikasa: 'Ackermann',
  Armin: 'Arlelt',
  Levi: 'Ackermann'
}
9
{ 
  Max: 19,
  Chloe: 20, 
  Nathan: 22, 
  Mark: 31 
}
0

{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
5
var obj = {}
obj = Object.assign({},gameField);
console.log(JSON.stringify(obj));
1
{ 
  Max: 19,
  Chloe: 20, 
  Nathan: 22, 
  Mark: 31 
}
3
{
  Eren: 'Yeager',
  Mikasa: 'Ackermann',
  Armin: 'Arlelt',
  Levi: 'Ackermann'
}
8
{ 
  Max: 19,
  Chloe: 20, 
  Nathan: 22, 
  Mark: 31 
}
5
{ 
  Max: 19,
  Chloe: 20, 
  Nathan: 22, 
  Mark: 31 
}
0

{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
5
var obj = {}
obj = Object.assign({},gameField);
console.log(JSON.stringify(obj));
1
{ 
  Max: 19,
  Chloe: 20, 
  Nathan: 22, 
  Mark: 31 
}
9
{
  Eren: 'Yeager',
  Mikasa: 'Ackermann',
  Armin: 'Arlelt',
  Levi: 'Ackermann'
}
8function1
{ 
  Max: 19,
  Chloe: 20, 
  Nathan: 22, 
  Mark: 31 
}
0

{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
5
var obj = {}
obj = Object.assign({},gameField);
console.log(JSON.stringify(obj));
1function5
{
  Eren: 'Yeager',
  Mikasa: 'Ackermann',
  Armin: 'Arlelt',
  Levi: 'Ackermann'
}
8
{ 
  Max: 19,
  Chloe: 20, 
  Nathan: 22, 
  Mark: 31 
}
5
{ 
  Max: 19,
  Chloe: 20, 
  Nathan: 22, 
  Mark: 31 
}
0

{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
1
{
    "6": [null, null, null, null, null, null, null, null, null, null, "player1"],
    "7": [null, null, null, null, null, "player2"]
}
7

{
    "6": [null, null, null, null, null, null, null, null, null, null, "player1"],
    "7": [null, null, null, null, null, "player2"]
}
8

Output:

{
  Eren: 'Yeager',
  Mikasa: 'Ackermann',
  Armin: 'Arlelt',
  Levi: 'Ackermann'
}

Cách tiếp cận 3: Theo cách tiếp cận này, trước tiên chúng tôi làm phẳng mảng bằng phương thức mảng.flat () để chúng tôi nhận được một mảng một chiều. Sau đó, chúng ta có thể tạo một đối tượng trống và lặp lại mảng để gán các giá trị được định vị đồng đều làm khóa của đối tượng và các giá trị được định vị kỳ lạ & nbsp; làm giá trị. In this approach, we first flatten the array using the Array.flat() method so that we get a one-dimensional array. We can then create an empty object and iterate the array to assign evenly positioned values as the key of the object and oddly positioned values as the value.

Example:

JavaScript

function

{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
0

{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
1
{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
05

{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
1
{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
2

{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
1
{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
09
{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
10

{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
5
{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
12
{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
13

Input: [
         ["John", 12],
         ["Jack", 13],
         ["Matt", 14],
         ["Maxx", 15]
       ]

Output: {
          "John": 12,
          "Jack": 13,
          "Matt": 14,
          "Maxx": 15
        }
6
{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
15

Input: [
         ["John", 12],
         ["Jack", 13],
         ["Matt", 14],
         ["Maxx", 15]
       ]

Output: {
          "John": 12,
          "Jack": 13,
          "Matt": 14,
          "Maxx": 15
        }
6
{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
17

Input: [
         ["John", 12],
         ["Jack", 13],
         ["Matt", 14],
         ["Maxx", 15]
       ]

Output: {
          "John": 12,
          "Jack": 13,
          "Matt": 14,
          "Maxx": 15
        }
6
var gameField = [];
gameField[6] = [];
gameField[6][10] = "player1";
gameField[7] = [];
gameField[7][5] = "player2";
0

{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
5
var gameField = [];
gameField[6] = [];
gameField[6][10] = "player1";
gameField[7] = [];
gameField[7][5] = "player2";
6

{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
1
var gameField = [];
gameField[6] = [];
gameField[6][10] = "player1";
gameField[7] = [];
gameField[7][5] = "player2";
6

{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
1
var gameField = [];
gameField[6] = [];
gameField[6][10] = "player1";
gameField[7] = [];
gameField[7][5] = "player2";
4
var gameField = [];
gameField[6] = [];
gameField[6][10] = "player1";
gameField[7] = [];
gameField[7][5] = "player2";
5

var gameField = [];
gameField[6] = [];
gameField[6][10] = "player1";
gameField[7] = [];
gameField[7][5] = "player2";
6

var gameField = [];
gameField[6] = [];
gameField[6][10] = "player1";
gameField[7] = [];
gameField[7][5] = "player2";
7

{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
1
var gameField = [];
gameField[6] = [];
gameField[6][10] = "player1";
gameField[7] = [];
gameField[7][5] = "player2";
9

{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
5
var obj = {}
obj = Object.assign({},gameField);
console.log(JSON.stringify(obj));
1
{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
33
{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
34

{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
5
var obj = {}
obj = Object.assign({},gameField);
console.log(JSON.stringify(obj));
1
{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
37
{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
38

{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
5
var obj = {}
obj = Object.assign({},gameField);
console.log(JSON.stringify(obj));
1
{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
41
{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
42

{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
5
var obj = {}
obj = Object.assign({},gameField);
console.log(JSON.stringify(obj));
1
{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
45
{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
46

{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}
1
{
    "6": [null, null, null, null, null, null, null, null, null, null, "player1"],
    "7": [null, null, null, null, null, "player2"]
}
7

{
    "6": [null, null, null, null, null, null, null, null, null, null, "player1"],
    "7": [null, null, null, null, null, "player2"]
}
8

Output:

{ 
  Max: 19,
  Chloe: 20, 
  Nathan: 22, 
  Mark: 31 
}

Làm thế nào để chuyển đổi mảng đa chiều thành đối tượng trong JavaScript?

Cách tiếp cận 1: Theo cách tiếp cận này, chúng tôi tạo một đối tượng trống và sử dụng phương thức mảng.Foreach () để lặp qua mảng. Trên mỗi lần lặp, chúng tôi chèn mục đầu tiên của mảng con vào đối tượng làm khóa và mục thứ hai là giá trị. Sau đó, nó trả về đối tượng sau các lần lặp.create an empty object and use the Array. forEach() method to iterate over the array. On every iteration, we insert the first item of the child array into the object as a key and the second item as the value. Then it returns the object after the iterations.

Làm thế nào để chuyển đổi một mảng thành một javascript đối tượng?

Để chuyển đổi một mảng thành một đối tượng, hãy sử dụng phương thức giảm () để lặp qua mảng, chuyển nó một đối tượng làm giá trị ban đầu.Trên mỗi lần lặp, gán một cặp giá trị khóa mới cho đối tượng tích lũy và trả về kết quả.Đã sao chép!const arr = ['không', 'một', 'hai'];const obj4 = mảng.use the reduce() method to iterate over the array, passing it an object as the initial value. On each iteration, assign a new key-value pair to the accumulated object and return the result. Copied! const arr = ['zero', 'one', 'two']; const obj4 = arr.

Phương pháp nào được sử dụng để chuyển đổi các mảng lồng nhau thành đối tượng trong JavaScript?

Khi chuyển đổi một đối tượng thành một mảng, chúng ta sẽ sử dụng phương thức .entries () từ lớp đối tượng.Điều này sẽ chuyển đổi đối tượng của chúng tôi thành một mảng mảng.Mỗi mảng lồng nhau là một danh sách hai giá trị trong đó mục đầu tiên là khóa và mục thứ hai là giá trị.. entries() method from the Object class. This will convert our object to an array of arrays. Each nested array is a two-value list where the first item is the key and the second item is the value.

Làm thế nào để chuyển đổi mảng đa chiều sang đối tượng trong PHP?

Php mảng: kết hợp, đa chiều $ get_object = json_decode (json_encode ($ mảng));Đầu tiên, json_encode ($ mảng) chuyển đổi toàn bộ mảng đa chiều thành chuỗi JSON.Sau đó, JSON_DECODE ($ String) sẽ chuyển đổi chuỗi JSON thành một đối tượng STDCLASS.$get_object = json_decode(json_encode($array)); First, the json_encode($array) converts the entire multi-dimensional array to a JSON string. Then the json_decode($string) will convert the JSON string to a stdClass object.