JavaScript finding a subset of an array centred on a given item [closed] find subset of array,find subset of array with given

You can do something like this.

var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];


function findSubset(arr, item) {
  // check index
  var index = arr.indexOf(item);
  // if element not found then return
  if (index == -1) return;

  // if element is at starting position 
  // then return first 5 element
  if (index < 3)
    return arr.slice(0, 5);

  // if elements at ending position
  // then return last 5 elements
  if (index > arr.length - 4)
    return arr.slice(-5);

  // otherwisse return elements based on the index
  // within the required range
  return arr.slice(index - 2, index + 3);
}

console.log(
  findSubset(arr, 1),
  findSubset(arr, 10),
  findSubset(arr, 5),
  findSubset(arr, 9),
  findSubset(arr, 3)
)
Answer:1

I am using React for this project. I need to read the state directly after I set it (using the callback), but when I print the state to the screen using the callback I get the values of the previous ...

I am using React for this project. I need to read the state directly after I set it (using the callback), but when I print the state to the screen using the callback I get the values of the previous ...

  1. react setstate callback example
  2. react setstate callback hooks
  3. react setstate callback arguments
  4. react setstate callback function
  5. react setstate callback async
  6. react setstate callback not called
  7. react setstate callback vs object
  8. react setstate callback state not updated
  9. react setstate callback prevstate
  10. react setstate callback before render
  11. react setstate callback previous state
  12. react setstate callback return
  13. react setstate callback doesn't work
  14. react setstate callback typescript
  15. react setstate callback functional component
  16. react setstate callback delay
  17. react setstate callback new state
  18. react setstate callback another setstate
  19. react setstate callback parameters
  20. react setstate callback called twice

I am working with React using Webpack and I am having trouble getting static images to load. The loader seems to be correctly converting the image to a URL but it doesn't seem to work as the img src ...

I am working with React using Webpack and I am having trouble getting static images to load. The loader seems to be correctly converting the image to a URL but it doesn't seem to work as the img src ...

  1. static images with movement
  2. static images with quotes
  3. static images in django
  4. static images in react
  5. static images in spring boot
  6. static images in flask
  7. static images in laravel
  8. static images in html
  9. static images in reactjs
  10. static images in java
  11. static images of

I'm having trouble calling a CFC through a jQuery ajax call. I've read at least a dozen articles/Overflows on the topic but cannot get a basic "Hello World" to return to the page. My environment is ...

I'm having trouble calling a CFC through a jQuery ajax call. I've read at least a dozen articles/Overflows on the topic but cannot get a basic "Hello World" to return to the page. My environment is ...

I want to loop over an array of arrays structure in order to filter out the duplicate datetime string values, and push the unique datetime string values sorted in a new array. I have tried to use a ...

I want to loop over an array of arrays structure in order to filter out the duplicate datetime string values, and push the unique datetime string values sorted in a new array. I have tried to use a ...

  1. unique datetime string c#