Home > database >  multiple arrays in javascript
multiple arrays in javascript

Time:10-21

I want to merge arrays based on index .how can I do this? following is my code

apple=["apple1","apple2"]
mango=["mango1","mango1"]
banana=["banana1","banana2"]
peach=["peach1","peach2"]
The expected output should be 
[{apple:"apple1",mango:"mango1",banana:"banana1",peach:"peach1"},
{apple:"apple2",mango:"mango2",banana:"banana2",peach:"peach2"}]

CodePudding user response:

So it looks like you want to map...

{string}{int} into a list?

If {int} is 0-9, then you can just take the last index of the string and order that.

If {int} is greater than possibly 0-9, I'd write a parser and parse the entire string and then check

if keyCode >= 48 || keyCode <= 57 intValue = keyCode

then at the end of the loop:

parseInt(intValue) and order based on that.

CodePudding user response:

I think, many new features from javascript, as per ex: ES6 can help you to achieve your result.

  • Map & Set From Mozilla Doc
  • A feu Logic with
    • loop (for ... of) style coupled with Array/Object manipulation and Spread operator can help you as well.
    • Using also the built-in functions/prototypes from Array like filter & map etc..
  • Related