2
function compareNumbers(x, y) {
return x - y;
}  
var a = [3,6,2,7,8,10,43] //I have one array
var b = a.slice().sort(compareNumbers).reverse(); //I reverse this array 

//Now I have 2 arrays

a = [3,6,2,7,8,10,43]
b = [43, 10, 8, 7, 6, 3, 2]   

My Question: I want to find the neighbour element of b in array a. For example:

b[0] that is 43 , want to find both neighbour of 43 in array a. // 10

b[1] that is 10 , want to find both neighbour of 10 in array a. // 43,8

b[6] that is 2, want to find both neighbour of 2 in array a. // 6,7

Mansi
  • 35
  • 1
  • 7

3 Answers3

2

If one of the neighbours doesn't exist you will get null.

var searchvalue = b[0]
var index = a.indexOf(searchvalue)

var firstNeighbour = index > 0 ? a[index-1] : null
var secondNeighbour = index < (a.length - 1) ? a[index + 1] : null
Alvaro Flaño Larrondo
  • 5,516
  • 2
  • 27
  • 46
paul
  • 21,653
  • 1
  • 53
  • 54
0

You could run a loop of the array and then check if it can have the left or right neighbour or not, if it has get it and if it doesn't leave that neigbour. For example if the loop is the first then there would be no left neighbor in that case leave the left_neigbour and if the loop is last than there will be no right neigbour in that case leave the right one and get the left_neighbour.Just use the below code

a = [3,6,2,7,8,10,43];
    b = [43, 10, 8, 7, 6, 3, 2];
    for (i = 0; i < b.length; i++) {
        if (i == 0) {
            var left_neighbour = '';
        } else {
            var left_neighbour = a[i - 1];
        }
        if (i == b.length) {
            var right_neighbour = '';
        } else {
            var right_neighbour = a[i + 1];
        }
        if (left_neighbour !== '') {
            if (right_neighbour) {
                console.log(left_neighbour + "," + right_neighbour);
            } else {
                console.log(left_neighbour);
            }
        } else {
            if (right_neighbour) {
                console.log(right_neighbour);
            } else {
                console.log(left_neighbour);
            }
        }

    }

Here is the fiddle: http://jsfiddle.net/r3vvba58/1/

Hope this helps you

Utkarsh Dixit
  • 4,267
  • 3
  • 15
  • 38
  • Thanks for the solution, but it still didnt solve my purpose. I want neighbour of Array a with correspondence to array b. – Mansi Mar 12 '15 at 03:42
  • @user3342244 Updated my answer... Have a look at it again. http://jsfiddle.net/r3vvba58/1/ . If its in reverse order decrement the value and change the loop accordingly – Utkarsh Dixit Mar 12 '15 at 03:46
0

Without the need of the second array and for a comfortable data reading:

http://jsfiddle.net/7pz13Lg2/1/

var a = [3,6,2,7,8,10,43];

a.retrieveNeighbour = function (needle) {
    if (this.indexOf(needle) == -1) {
        return {
            right: null,
            left: null
        }; 
    }
    else {
        return {
            left: (this[this.indexOf(needle)-1] != undefined) && this[this.indexOf(needle)-1],
            right: (this[this.indexOf(needle)+1] != undefined) &&  this[this.indexOf(needle)+1]
        }
    }
};
console.log(a.retrieveNeighbour(3).right);
console.log(a.retrieveNeighbour(6).left);
console.log(a.retrieveNeighbour(93).right);

To retrieve the LEFT neighbour, use:

a.retrieveNeighbour(search).left;

To retrieve the right one:

a.retrieveNeighbour(search).right;

If the element does not exist inside the array, the script returns null to both left and right.

If the element exists but the property doesn't (like a.retrieveNeighbour(3).left) the script assign false to that value.

Tests:

enter image description here

briosheje
  • 7,356
  • 2
  • 32
  • 54