How to add js to two arrays

for example, var a = [1rem 2je 3]; b = [4je 5je 6];
how can two arrays be added together to get an array like [5je 7je 9].

Mar.02,2021

a.map(function(item,index){
    return item+b[index]
})
for`for in``Array.foreach`

var result= array.reduce(function(initObj,item,index,array){
             //
                 if(item.ab<100){
                     initObj.a100.push(item)
                 }
                 if(100<=item.ab&&item.ab<1000){
                     console.log(`100-1000,${item.ab}`)
                    initObj.a1000.push(item)
                 }
                 if(1000<=item.ab&&item.ab<10000){
                    initObj.a10000.push(item)
                 }
                 if(10000<=item.ab&&item.ab<100000){
                    initObj.a100000.push(item)
                 }
                 if(100000<=item.ab&&item.ab<1000000){
                    initObj.a1000000.push(item)
                 }
                 //
                 
             return initObj;
         },{a10:[],a100:[],a1000:[],a10000:[],a100000:[],a1000000:[]})
         var finnalResult=[];
         for(var index in result){
             result[index].sort(function(itema,itemb){
                return itema.ab-itemb.ab
             })
             finnalResult.push(result[index].shift())
         }
         return finnalResult.splice(0,1)

var N = a.map ((venerI) = > v + b [I])


  1. if you want to use a var/let to define multiple variables, be especially careful not to write commas as semicolons.
  2. the program is very simple

     
    

    Thank you for the invitation.

    it is recommended to analyze the problem first.

      What is entered by
    • ? Two arrays [1, 2, 7, 3] and [4, 5, 5, and 6].
    • What is the output of
    • ? A new array [5, 7, 7, 9].

    if you want to write a function, you already have parameters and return.
    next, how can I get the output from the input?

    • from this point of view, items with the same subscript in two arrays can be added together to get items in a new array with the same subscript.

    Yes, this is the algorithm to be used.
    what can I do next?

    • has been talking about arrays for a long time, so it's natural to take a look at the Array standard library in javascript first. In addition, basic knowledge such as program flow and operators is also indispensable.

    well, that's about it. It may be clumsy to write at first, but it can be optimized over and over again. Practice makes perfect. The
    program is pretty much written above, so I won't repeat it any more.

    one last thing to note is: be careful!

    for example, it is mentioned in the title:

    var a = [1 code 2, blockquote 3]; b = [4, 5, 5, 6];

    pay attention to the semicolon in the middle, use a comma when assigning values in batches, and report an error in strict mode.

Menu