简体   繁体   中英

How Can I convert a nested array of object into a flat Array javascript

I've got this

 const nested =  [
    [{ id: 1, name: "carl" }],    
    [{ id: 2, name: "lety" }],
    [{ id: 3, name: "jim" }]
 ];

but I need this:

 const flat =  [
    { id: 1, name: "carl" },    
    { id: 2, name: "lety" },
    { id: 3, name: "jim" }
 ];

thanks so muchs

If you are sure to have only 1 value in your subarrays, you can try :

const flat = []

for (n of nested) {
    flat.push(n[0])
}

nested.flat(); solved my issue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM