Filip neskoski
Filip neskoski

Reputation: 23

How to return item from array instead of index

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'}

Upvotes: 0

Views: 1734

Answers (6)

Akhil Aravind
Akhil Aravind

Reputation: 6130

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))

Upvotes: 1

jo_va
jo_va

Reputation: 13993

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));

Upvotes: 0

arunmmanoharan
arunmmanoharan

Reputation: 2675

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)

Upvotes: 0

aTON
aTON

Reputation: 25

If you know the index, you can use:

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

...and so on and so forth.

Upvotes: 0

Mikeywan
Mikeywan

Reputation: 11

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)

Upvotes: 0

Alexandre Elshobokshy
Alexandre Elshobokshy

Reputation: 10922

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.

Upvotes: 0

Related Questions