Giả sử sau đây là mảng lồng nhau của chúng ta -
const arrayObject = [
[
{
Name: "John"
},
{
countryName: "US"
}
],
[
{
subjectName: "JavaScript"
},
{
teacherName: "Mike"
}
]
]; Để chuyển mảng lồng nhau thành mảng bình thường, hãy sử dụng khái niệm flat () như trong đoạn mã dưới đây -
Ví dụ
const arrayObject = [
[
{
Name: "John"
},
{
countryName: "US"
}
],
[
{
subjectName: "JavaScript"
},
{
teacherName: "Mike"
}
]
];
const output = arrayObject.flat();
console.log(output); Để chạy chương trình trên, bạn cần sử dụng lệnh sau -
node fileName.js.
Đây, tên tệp của tôi là demo50.js.
Đầu ra
Điều này sẽ tạo ra kết quả sau -
PS C:\Users\Amit\JavaScript-code> node demo50.js
[
{ Name: 'John' },
{ countryName: 'US' },
{ subjectName: 'JavaScript' },
{ teacherName: 'Mike' }
]