1

    const count = [
        {number: 1},
        {number: 2},
        {number: 3},
        {number: 4},
        {number: 5},
        {number: 6},
        {number: 7},
        {number: 8},
        {number: 9},
        {number: 10},
        {number: 11},
        {number: 12},
        {number: 13},
        {number: 14},
        {number: 15}
        ];
 
 
    for(let i = 0;i < count.length; i++) {

    document.write(count[i].number);
    }

If the main number what i want to get out the loop is 5. But i want to write out one above 5 and one under 5. So 456. How can i achieve that?

3
  • document.write(count[i - 1] + count[i] + count[i + 1]); Commented Jan 22, 2019 at 5:18
  • @JacobSchneider but watch out for an out of bounds exception! Commented Jan 22, 2019 at 5:20
  • count[i - 1] || "" Commented Jan 22, 2019 at 5:21

4 Answers 4

2

In order to filter out the number which you want you can use an if statement to check if the current object's number count[i].number is the value you want, and then if it is, print out its associated values.

You can access the object before and after by changing your value of i. Using i-1 you will get the element before your current value. Similarly, using i+1 you will go to the object after your current value.

Lastly, you should check the value of i to make sure that it falls within the array. For instance, if you chose to find the number 1 it has no before object, and so you need to check for this case!

See working example below:

const count = [
        {number: 1},
        {number: 2},
        {number: 3},
        {number: 4},
        {number: 5},
        {number: 6},
        {number: 7},
        {number: 8},
        {number: 9},
        {number: 10},
        {number: 11},
        {number: 12},
        {number: 13},
        {number: 14},
        {number: 15}
        ];
 
const toFind = 5;
for(let i = 0; i < count.length; i++) {
  if(count[i].number == toFind) {
    if(i-1 >= 0) console.log(count[i-1].number);
    console.log(count[i].number);
    if(i+1 < count.length) console.log(count[i+1].number);
    break; // complete loop cycle
  }
}

Sign up to request clarification or add additional context in comments.

1 Comment

This is the most algorithmically efficient solution. One small optimization to make is to break; at the end of the if() statement, as you have already found your number and iterating the rest of the array is unnecessary.
2

Assuming the needle is unique, you can .filter the numbers you want, then .forEach to print them.

const needle = 5
const haystack = [
  {number: 1},
  {number: 2},
  {number: 3},
  {number: 4},
  {number: 5},
  {number: 6},
  {number: 7},
  {number: 8},
  {number: 9},
  {number: 10},
  {number: 11},
  {number: 12},
  {number: 13},
  {number: 14},
  {number: 15}
]

haystack
  .sort((a, b) => a.number - b.number) // skip this if haystack is already sorted.
  .filter(el => [needle - 1, needle, needle + 1].includes(el.number))
  .forEach(el => {
    document.write(el.number)
  })

Note that you have to guard against needle = 0 which has no -1 and needle = 15 which has no +1.

6 Comments

More generally I believe their query is "I need to get X number, and show the values at x-1 and x+1"
@Our_Benefactors Ah, true. Edited.
No need of filter here. why not do it simply with one forEach ?
@CodeManiac You mean using an if within forEach?
@NikKyriakides yes. there's no need of one extra loop
|
1

        const count = [
      { number: 1 },
      { number: 2 },
      { number: 3 },
      { number: 4 },
      { number: 5 },
      { number: 6 },
      { number: 7 },
      { number: 8 },
      { number: 9 },
      { number: 10 },
      { number: 11 },
      { number: 12 },
      { number: 13 },
      { number: 14 },
      { number: 15 }
    ];
    var index = count.findIndex(item => item.number === 5);
    document.write(count[index - 1].number);
    document.write(count[index].number);
    document.write(count[index + 1].number);

1 Comment

Make sure to bounds check your index-1 and index+1 or you'll get an exception for undefined.number
0

You can directly use the forEach and based on range you can write to document.

const count = [{number: 1},{number: 2},{number: 3},{number: 4},{number: 5},{number: 6},{number: 7},{number: 8},{number: 9},{number: 10},{number: 11},{number: 12},{number: 13},{number: 14},{number: 15}];
        
count.forEach(({number})=> {
  if(number>3 && number<7){
    document.write(number)
  }
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.