0

I am making a function that returns element of an array and return the prev and next element, but my problem is my function return the index of the elements of the array and i want to return the items

this is the array

var items = ['bill','hill','dill',4,5,6,7,8,9,10];

this is my function

function getItem(index, arr) {
    if(index > arr.length) {
        return -1; 
    }

    const prev = index - 1;
    const next = index + 1;
    const prevPrev = prev - 1;
    const nextNext = next + 1;

    if(index == 0) { 
        return {index, next, nextNext}
    } else if (index == arr.length - 1) { 
        return {prevPrev, prev, index}
    } else {
        return {prev, index, next};
    }
}

output is

{prev: 1, index: 2, next: 3}

i want to return

{prev:'bill', index:'hill', next:'dill'}
3
  • How you are calling getItem function Commented Feb 7, 2019 at 16:15
  • console.log(getItem(2, items)); Commented Feb 7, 2019 at 16:18
  • ES6 object property shorthands aren't suitable for your case. You have to explicitly define value for each key in returning object, e.g.: {index: arr[index], next: arr[next], nextNext: arr[nextNext]} Commented Feb 7, 2019 at 16:28

6 Answers 6

1

You have to make a small change in inside the function check these lines

const prev = index - 1;
const next = index + 1;
return { prev, index: index, next };

Just change them to

const prev = arr[index - 1];
const next = arr[index + 1];
return { prev, index: arr[index], next };

so it returns value instead of index

var items = ['bill','hill','dill',4,5,6,7,8,9,10];

function getItem(index, arr) {
    if(index > arr.length) {
        return -1; 
    }

    const prev = arr[index - 1];
    const next = arr[index + 1];
    const prevPrev = prev - 1;
    const nextNext = next + 1;

    if(index == 0) { 
        return {index, next, nextNext}
    } else if (index == arr.length - 1) { 
        return {prevPrev, prev, index}
    } else {
        return {prev, index:arr[index], next};
    }
}
console.log(getItem(1, items))

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

Comments

0

You could do it this way :

var items = ['bill', 'hill', 'dill', 4, 5, 6, 7, 8, 9, 10];

function getItem(index, arr) {
  if (index > arr.length) {
    return -1;
  }

  const prev = items[index - 1];
  const next = items[index + 1];
  const prevPrev = items[prev - 1];
  const nextNext = items[next + 1];
  const index1 = items[index]

  if (index == 0) {
    return {
      index1,
      next,
      nextNext
    }
  } else if (index == arr.length - 1) {
    return {
      prevPrev,
      prev,
      index1
    }
  } else {
    return {
      prev,
      index1,
      next
    };
  }
}

console.log(getItem(1, items));

You return the value of the each index in the array rather than the index number by itself.

1 Comment

Thank you for answering. There is just 1 problem if i have this array var items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; and when i console.log(getItem(1, items)); my output is prev: 1, index: 1, next: 3
0

Assuming that your inputs to your function are a number for "index" and an array for your "arr", returning arr[number] should return the elements you need.

i.e.

if(index > arr.length) {
    return -1; 
}
    const prev = index - 1;
    const next = index + 1;
    const prevPrev = prev - 1;
    const nextNext = next + 1;

    if(index == 0) { 
        return {arr[index], arr[next], arr[nextNext]}
    } else if (index == arr.length - 1) { 
        return {arr[prevPrev], arr[prev], arr[index]}
    } else {
        return {arr[prev], arr[index], arr[next]};
    }
}

Though you should safe-guard against your returned values being out of bound (ie checking if nextNext and prevPrev are still within the size of the array - eg an array of only 2)

Comments

0

If you know the index, you can use:

    if(index == 0) { 
        return {arr[index], arr[next], arr[nextNext]}

...and so on and so forth.

Comments

0

You can use map to achieve the same.

var items = ['bill', 'hill', 'dill', 4, 5, 6, 7, 8, 9, 10];

var arr = items.map((item, index, array) => {
  return {
    prev: item,
    index: array[index + 1],
    next: array[index + 2]
  }
});

console.log(arr)

Comments

0

Here is how I would do it using an inner safeGet function to return undefined when the indices are out of bound:

const items = ['bill', 'hill', 'dill', 4, 5, 6, 7, 8, 9, 10];

const getItem = (arr, i) => {
  const safeGet = i => i >= 0 && i < arr.length ? arr[i] : undefined;
  return { prev: safeGet(i-1), index: safeGet(i), next: safeGet(i+1) };
};

console.log(getItem(items, 0));
console.log(getItem(items, 2));
console.log(getItem(items, 9));
console.log(getItem(items, 20));

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.