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ị.
5{
"6": {"10":"player1"},
"7": {"5":"player2"}
}
12 {
"6": {"10":"player1"},
"7": {"5":"player2"}
}
13In 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.{
"6": {"10":"player1"},
"7": {"5":"player2"}
}
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"}
}
1var 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"}
}
5var gameField = [];
gameField[6] = [];
gameField[6][10] = "player1";
gameField[7] = [];
gameField[7][5] = "player2";
0{
"6": {"10":"player1"},
"7": {"5":"player2"}
}
1var gameField = [];
gameField[6] = [];
gameField[6][10] = "player1";
gameField[7] = [];
gameField[7][5] = "player2";
2Cá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";
6var gameField = [];
gameField[6] = [];
gameField[6][10] = "player1";
gameField[7] = [];
gameField[7][5] = "player2";
7{
"6": {"10":"player1"},
"7": {"5":"player2"}
}
1var gameField = [];
gameField[6] = [];
gameField[6][10] = "player1";
gameField[7] = [];
gameField[7][5] = "player2";
9{
"6": {"10":"player1"},
"7": {"5":"player2"}
}
5var obj = {}
obj = Object.assign[{},gameField];
console.log[JSON.stringify[obj]];
1var obj = {}
obj = Object.assign[{},gameField];
console.log[JSON.stringify[obj]];
2var obj = {}
obj = Object.assign[{},gameField];
console.log[JSON.stringify[obj]];
3{
"6": {"10":"player1"},
"7": {"5":"player2"}
}
5var obj = {}
obj = Object.assign[{},gameField];
console.log[JSON.stringify[obj]];
1var obj = {}
obj = Object.assign[{},gameField];
console.log[JSON.stringify[obj]];
6var obj = {}
obj = Object.assign[{},gameField];
console.log[JSON.stringify[obj]];
7{
"6": {"10":"player1"},
"7": {"5":"player2"}
}
5var 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"}
}
5var 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"]
}
8Output:
{ Jack: 13, John: 12, Matt: 14, Maxx: 15 }
1{
"6": {"10":"player1"},
"7": {"5":"player2"}
}
09 {
"6": {"10":"player1"},
"7": {"5":"player2"}
}
10In 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.{
"6": {"10":"player1"},
"7": {"5":"player2"}
}
Example:
JavaScript
function
{
"6": {"10":"player1"},
"7": {"5":"player2"}
}
0{
"6": {"10":"player1"},
"7": {"5":"player2"}
}
1var 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"}
}
5Input: [ ["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"]
}
8var gameField = [];
gameField[6] = [];
gameField[6][10] = "player1";
gameField[7] = [];
gameField[7][5] = "player2";
6var gameField = [];
gameField[6] = [];
gameField[6][10] = "player1";
gameField[7] = [];
gameField[7][5] = "player2";
7{
"6": {"10":"player1"},
"7": {"5":"player2"}
}
1var gameField = [];
gameField[6] = [];
gameField[6][10] = "player1";
gameField[7] = [];
gameField[7][5] = "player2";
9{
"6": {"10":"player1"},
"7": {"5":"player2"}
}
5var 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"}
}
5var 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"}
}
5var 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' }8
function
1{ Max: 19, Chloe: 20, Nathan: 22, Mark: 31 }0
{
"6": {"10":"player1"},
"7": {"5":"player2"}
}
5var obj = {}
obj = Object.assign[{},gameField];
console.log[JSON.stringify[obj]];
1function
5{ 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"]
}
8Output:
{ 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"}
}
13Input: [ ["John", 12], ["Jack", 13], ["Matt", 14], ["Maxx", 15] ] Output: { "John": 12, "Jack": 13, "Matt": 14, "Maxx": 15 }6
{
"6": {"10":"player1"},
"7": {"5":"player2"}
}
15Input: [ ["John", 12], ["Jack", 13], ["Matt", 14], ["Maxx", 15] ] Output: { "John": 12, "Jack": 13, "Matt": 14, "Maxx": 15 }6
{
"6": {"10":"player1"},
"7": {"5":"player2"}
}
17Input: [ ["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"}
}
5var gameField = [];
gameField[6] = [];
gameField[6][10] = "player1";
gameField[7] = [];
gameField[7][5] = "player2";
6{
"6": {"10":"player1"},
"7": {"5":"player2"}
}
1var gameField = [];
gameField[6] = [];
gameField[6][10] = "player1";
gameField[7] = [];
gameField[7][5] = "player2";
6{
"6": {"10":"player1"},
"7": {"5":"player2"}
}
1var 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";
5var gameField = [];
gameField[6] = [];
gameField[6][10] = "player1";
gameField[7] = [];
gameField[7][5] = "player2";
6var gameField = [];
gameField[6] = [];
gameField[6][10] = "player1";
gameField[7] = [];
gameField[7][5] = "player2";
7{
"6": {"10":"player1"},
"7": {"5":"player2"}
}
1var gameField = [];
gameField[6] = [];
gameField[6][10] = "player1";
gameField[7] = [];
gameField[7][5] = "player2";
9{
"6": {"10":"player1"},
"7": {"5":"player2"}
}
5var 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"}
}
5var 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"}
}
5var 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"}
}
5var 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"]
}
8Output:
{ Max: 19, Chloe: 20, Nathan: 22, Mark: 31 }