Skip to content Skip to sidebar Skip to footer

How To Get Value At Highest Index In A Array From The Values Of Another Array With Javascript Programming

I have 2 set of array var A = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L']; var B = ['B', 'J', 'A', 'C', 'G']; now I have to a value from var B which is at highes

Solution 1:

Sort the second array in descending order and then find the element by iterating. You can use sort() for sorting descending order and find() for getting the element.

var A = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"],
  B = ["B", "J", "A", "C", "G"];

functioncustomArrayFunction(A, B) {
  // create an additional array variable for storing sorted array, otherwise it will update the order of array Bvar sortB = [];
  B.forEach(function(v) {
    sortB.push(v);
  });

  // sorting array on descending order// instead of custom compare function // you can use `sortB.sort().reverse()` simplyvar res = sortB.sort(function(a, b) {
    if (a > b)
      return -1;
    elseif (a < b)
      return1;
    return0;
  })
  // finding element
  .find(function(v) {
    return A.indexOf(v) > -1;
  });
  
  return res;
}
console.log(customArrayFunction(A,B));
console.log(customArrayFunction(["A","D","G"],["B","D","G"]));

Using ES6 arrow function

var A = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"],
  B = ["B", "J", "A", "C", "G"];

functioncustomArrayFunction(A, B) {
  var sortB = [];
  B.forEach(v => sortB.push(v));
  var res = sortB.sort((a, b) => {
    if (a > b)
      return -1;
    elseif (a < b)
      return1;
    return0;
  }).find(v => A.indexOf(v) > -1);

  return res;
}
console.log(customArrayFunction(A, B));
console.log(customArrayFunction(["A", "D", "G"], ["B", "D", "G"]));

More easiest way without using any custom compare function with help of reverse(), which helps to reverse the array.

var A = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"],
  B = ["B", "J", "A", "C", "G"];

functioncustomArrayFunction(A, B) {
  var sortB = [];
  B.forEach(v => sortB.push(v));
  var res = sortB.sort()
    .reverse()
    .find(v => A.indexOf(v) > -1);

  return res;
}
console.log(customArrayFunction(A, B));
console.log(customArrayFunction(["A", "D", "G"], ["B", "D", "G"]));

UPDATE : If you want to search based on the index then there is no need of sort , just use find()

var A = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"],
  B = ["B", "J", "A", "C", "G"];

functioncustomArrayFunction(A, B) {
  var res = A
    .find(v => B.indexOf(v) > -1);
  return res;
}
console.log(customArrayFunction(A, B));
console.log(customArrayFunction(["A", "D", "G"], ["B", "D", "G"]));

Solution 2:

use forEach of list to get your desired result.

var A = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"];
var B = ["B", "J", "A", "C", "G"];
var index = -1; // find index of your array A
A.forEach(function(item1, index1) {
    B.forEach(function(item2, index2) {
        if (item1 === item2) {
            index = index1 > index ? index1 : index;
        }
    })
})

var result = index >= 0 ? A[index] : 'No Result found'; // gives your desired result.

Solution 3:

might be a little too low tech, but what about the following(console.logging it for testing purposes)

var A = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"];
    var B = ["B", "J", "A", "C", "G"];
    var max=0;
    var letter="";
    for(i=0;i<B.length;i++)
       {
          var test=A.indexOf(B[i]);
          if(test > max){max=test;letter=B[i]};
       }
    console.log(letter)

console.log give "J" as expected

Solution 4:

This is a solution with a temporary object and some array methods.

var a = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"],
    b = ["B", "J", "A", "C", "G"],
    result = function (array1, array2) {
        var o = {},
            r;
        array2.forEach(function (a) {
            o[a] = true;
        });
        a.reverse().some(function (a) {
            if (o[a]) {
                r = a;
                returntrue;
            }
        });
        return r;
    }(a, b);

document.write(result);

Post a Comment for "How To Get Value At Highest Index In A Array From The Values Of Another Array With Javascript Programming"