JavaScript Equivalent function without using a for loop in Javascript?

What would be the equivalent of this without using a for loop?

Unsure what array method to use

function func(x){
    var y = [];
    for(i=0;i<x.length;i++){
        y.push(x[i]);
        y.push(x[i]);
    }
    return y;
}

var input = [1,2,3,4,5,6];

console.log(func(input));// returns [1,1,2,2,3,3,4,4,5,5,6,6]
Answer:1

You cannot map directly, but you could reduce into a new expanded array:

arr.reduce((prev, curr) => prev.concat([curr, curr]), []);

And this is without ES6 arrow function:

arr.reduce(function(prev, curr) { return prev.concat([curr, curr]); }, []);
Answer:2

I have a MongoDB collection called 'Prices' that I am trying to query for prices between a 'startDate' and 'endDate'. The collection stores prices every 10 seconds, however when querying this ...

I have a MongoDB collection called 'Prices' that I am trying to query for prices between a 'startDate' and 'endDate'. The collection stores prices every 10 seconds, however when querying this ...

  1. mongodb query large data
  2. mongodb query larger than
  3. mongodb large query optimization
  4. mongodb large query

I'm doing something like: $.ajax({ type: 'POST', dataType: 'json', contentType: 'application/json', url: '/Modal/getModalInfo', data: '{"machine":"' + machine + '"}', success:...

I'm doing something like: $.ajax({ type: 'POST', dataType: 'json', contentType: 'application/json', url: '/Modal/getModalInfo', data: '{"machine":"' + machine + '"}', success:...

  1. ajax call returning object object
  2. ajax call returning empty response
  3. ajax call returning html page
  4. ajax call returning html instead of json
  5. ajax call returning error
  6. ajax call returning undefined
  7. ajax call returning 302
  8. ajax call returning 404
  9. ajax call returning html
  10. ajax call returning json data
  11. ajax call returning 500 error
  12. ajax request returning full page code
  13. ajax request returning undefined
  14. ajax request returning 404
  15. ajax request returning object
  16. ajax call not returning success or error
  17. ajax call not returning response
  18. ajax call not returning data
  19. ajax call not returning to success
  20. ajax call not returning view

I want to override console.log method to invoke a set of tasks whenever console.log is called. I referred other Stackoverflow answers but that give me the error: Uncaught RangeError: Maximum call ...

I want to override console.log method to invoke a set of tasks whenever console.log is called. I referred other Stackoverflow answers but that give me the error: Uncaught RangeError: Maximum call ...

  1. override javascript console.log
  2. override console js
  3. javascript override console.error
  4. javascript override console.log function
  5. javascript override console

I'm writing a form in Angular and I have one criteria that requires that I need to check for that requires the input to ONLY contain letters and numbers AND the first character cannot be a number. Is ...

I'm writing a form in Angular and I have one criteria that requires that I need to check for that requires the input to ONLY contain letters and numbers AND the first character cannot be a number. Is ...

  1. angular form control validation
  2. angular form control set value
  3. angular form control disabled
  4. angular form control example
  5. angular form control get value
  6. angular form control reset
  7. angular form control name
  8. angular form control checkbox
  9. angular form control default value
  10. angular form control valuechanges
  11. angular form control array
  12. angular form control custom validator
  13. angular form control radio button
  14. angular form control seterrors
  15. angular form control clear value
  16. angular form control readonly
  17. angular form control focus
  18. angular form control change event
  19. angular form control label
  20. angular form control type