账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    怎么从一个数组获得新数组?
    33
    0

    1, 我想把一个数组按照3等分合成一个新数组

    
    把这个数组
     var arr =   [ 
                    [ "12" ], 
                    [ "blob:http://localhost:8090/a23268b6-dde4-4ae8-a845-3fc0bc9c4ef8" ], 
                    [ "12", "12" ], 
                    [ "12" ], 
                    [ "blob:http://localhost:8090/a23268b6-dde4-4ae8-a845-3fc0bc9c4ef8" ], 
                    [ "12", "12" ] 
                ]
    变成
    var arr = [arr1,arr2]
            = [
                [
                    [ "12" ], 
                    [ "blob:http://localhost:8090/a23268b6-dde4-4ae8-a845-3fc0bc9c4ef8" ], 
                    [ "12", "12" ]
                ],
                [
                    [ "12" ], 
                    [ "blob:http://localhost:8090/a23268b6-dde4-4ae8-a845-3fc0bc9c4ef8" ], 
                    [ "12", "12" ]
                ]
            
            
            ]   
                

    给个方法,急!3Q3Q?

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 执著之☆海 普通会员 1楼

      在编程中,我们可以使用内置的函数或方法来从一个数组获得一个新的数组。以下是一些常见的方法:

      1. 使用Array.from()方法:这是一个ES6新增的方法,它创建一个新的数组,该数组的所有元素都是源数组中的元素。

      javascript let arr = [1, 2, 3]; let newArr = Array.from(arr); console.log(newArr); // 输出:[1, 2, 3]

      1. 使用Array.prototype.slice()方法:这个方法可以从数组中提取一部分元素,创建一个新的数组。

      javascript let arr = [1, 2, 3]; let newArr = arr.slice(1, 4); console.log(newArr); // 输出:[2, 3]

      1. 使用Array.prototype.map()方法:这个方法可以对数组中的每个元素应用一个函数,返回一个新的数组。

      javascript let arr = [1, 2, 3]; let newArr = arr.map(x => x * 2); console.log(newArr); // 输出:[2, 4, 6]

      1. 使用Array.prototype.reduce()方法:这个方法可以对数组中的每个元素应用一个函数,返回一个新的数组。

      javascript let arr = [1, 2, 3]; let newArr = arr.reduce((acc, curr) => acc + curr, 0); console.log(newArr); // 输出:6

      以上都是从一个数组中获取新数组的方法,具体使用哪种方法取决于你的具体需求。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部