How to get value at highest index in a array from the values of another array with javascript programming [closed]

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 highest index in var A, it is J

I have to find out J in var B by JavaScript programming

Thanks in advance

Answers:

Answer

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"];

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

  // sorting array on descending order
  // instead of custom compare function 
  // you can use `sortB.sort().reverse()` simply
  var res = sortB.sort(function(a, b) {
    if (a > b)
      return -1;
    else if (a < b)
      return 1;
    return 0;
  })
  // 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"];

function customArrayFunction(A, B) {
  var sortB = [];
  B.forEach(v => sortB.push(v));
  var res = sortB.sort((a, b) => {
    if (a > b)
      return -1;
    else if (a < b)
      return 1;
    return 0;
  }).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"];

function customArrayFunction(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"];

function customArrayFunction(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"]));

Answer

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.
Answer

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

Answer

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;
                return true;
            }
        });
        return r;
    }(a, b);

document.write(result);

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.