dabocaiqq
2018-06-15 03:27
采纳率: 66.6%
浏览 2.2k

javascript两个数组合并,不是连接,而是相加

javascript两个数组合并,不是连接,而是相加
a=[1,2,3,4,5]
b=[2,4,3,2,1]
算出来
3,6,6,6,6

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

10条回答 默认 最新

  • 已采纳
     a=[1,2,3,4,5]
    b=[2,4,3,2,1]
    var rst=[];
    a.forEach(function(v,index){rst.push(v+b[index])})
    console.log(rst)
    
    已采纳该答案
    打赏 评论
  • qq_38762162 2018-06-15 03:31

    a.concat(b)试试。。

    打赏 评论
  • wuwa 2018-06-15 03:32

    for(int i=0;i<a.length;i++)
    {
    c[i]=a[i]+b[i];
    }

    打赏 评论
  • ambit_tsai-微信 2018-06-15 03:41

    数组用map遍历相加,简洁明了

    a.map((val,index)=>(val+b[index]));
    
    打赏 评论
  • 邪恶八进制 2018-06-15 03:42

    两个数组对应位相加,如果和>9进一位。例如

    int[] a={1,2,3,4,5};

    int[] b={4,5,6,7,8};

    则结果:int[] c={5,7,9,1,4,1}

    下面直接贴代码:

     public class CopyArray {
        public int[] add(int[] a, int[] b) {
            if (a.length == b.length) {
                for(int i=0;i<a.length-1;i++){
                    a[i]=a[i]+b[i];
                    if(a[i]>9){
                        a[i]=a[i]%10;
                        a[i+1]=a[i+1]+1;
                    }
                }
                a[a.length-1]=a[a.length-1]+b[b.length-1];
                if(a[a.length-1]>9){
                    a[a.length-1]=a[a.length-1]%10;
                    int[] c=new int[a.length+1];
                    for(int i=0;i<a.length;i++){
                        c[i]=a[i];
                    }
                    c[c.length-1]=1;
                    return c;
                }else{
                    return a;
                }
    
    
            } else {
                int[] bigger = a.length > b.length ? a : b;
                int[] smaller = a.length < b.length ? a : b;
                int temp = bigger.length - smaller.length;
                for(int i=0;i<=smaller.length-1;i++){
                    bigger[i] = smaller[i] + bigger[i];
                    if (bigger[i] > 9) {
                        bigger[i] = bigger[i] % 10;
                        bigger[i + 1] = bigger[i + 1] + 1;
                    }
                }
                for (int i = smaller.length; i < bigger.length-1; i++) {
                    if (bigger[i] > 9) {
                        bigger[i] = bigger[i] % 10;
                        bigger[i - 1] = bigger[i - 1] + 1;
                    }
                }
                if (bigger[bigger.length-1] > 9) {
                    bigger[bigger.length-1] = bigger[bigger.length-1] % 10;
                    int[] c = new int[bigger.length + 1];
                    for (int i = 0; i < bigger.length; i++) {
                        c[i + 1] = bigger[i];
                    }
                    c[bigger.length-1] = 1;
                    return c;
                } else {
                    return bigger;
                }
            }
        }
        public static void main(String[] args){
             int[] a={1,3,6,8,4,5,7};
             int[] b={2,4,6,7,8};
             CopyArray copy=new CopyArray();
             int[] c=copy.add(a,b);
             for(int i=0;i<c.length;i++){
                 System.out.println(c[i]);
             }
        }
    }
    
    打赏 评论
  • 王海虎 2018-06-15 03:43
    arr3 = [1,2,3];  
    arr4 = [1,2,3];  
    let newarr = arr3.map(function(item, index) {
        return item + arr4[index]
    }) 
    console.log(newarr) 
    
    打赏 评论
  • myhome908 2018-06-15 03:48

    function sum(a,b){
    var c=[];
    if(Array.isArray(a)&&Array.isArray(b){

    for(var i=0;i<a.length;i++)
    for(var j=0;j<b.length;j++){
    if(a.length!=b.length){
    window.alert("两数组长度不同")
    }else{
    if (i==j){
    c[i]=a[i]+b[j]
    }else{
    continue
    }
    }
    }
    return c
    }
    }
    大致这样,不保证正确,哈哈

    打赏 评论
  • rxw3691 2018-06-15 06:35

    function(a,b){
    var c = [];
    for (var i = 0; i < a.length; i++) {
    c[i] = a[i] + b[i];
    }
    return c;
    }

    打赏 评论
  • 天际的海浪 2018-06-15 09:34
    a=[1,2,3,4,5]
    b=[2,4,3,2,1]
    c=a.map((v,i)=>v+b[i]);
    console.log(c);
    
    
    打赏 评论
  • 阿鱼啊 2018-06-26 08:21

    var a = [1,2,3,4];
    var b = [1,2,3,4,5,6,7,8,9];
    var arr_= [];
    if(a.length==b.length){
    for (var i=0;i<a.length;i++){
    arr_.push(parseFloat(a[i])+parseFloat(b[i]))
    }
    console.log(arr_)
    }else if(a.length<b.length){
    for (var i=0;i<a.length;i++){
    arr_.push(parseFloat(a[i])+parseFloat(b[i]))
    }
    for(var j=a.length;j<b.length;j++){
    arr_.push(b[j])
    }
    console.log(arr_)
    }else{
    for (var i=0;i<b.length;i++){
    arr_.push(parseFloat(a[i])+parseFloat(b[i]))
    }
    for(var j=b.length;j<a.length;j++){
    arr_.push(a[j])
    }
    console.log(arr_)

        }
        //输出[2,4,6,8,5,6,7,8,9]
    
    打赏 评论

相关推荐 更多相似问题