简体   繁体   中英

How to get the last array that includes a certain element?

I have an array of arrays and I want to check if there is a tie between the second elements and then return the first element of the last array that makes a tie.

for example this should return 4. (the first element in the last array that has a second element that makes a tie)

var optionsArray = [[1, 10], [2, 10], [3, 10], [4, 10], [6, 14]];

It is quite simple, you need to iterate over your source array, check if the given item matches the criteria, and save it to result if it does. Now if any other item does match the criteria, result's value will be overwritten with the new matching item.

 var optionsArray = [[1, 10], [2, 10], [3, 10], [4, 10], [6, 14]]; var result; optionsArray.forEach(function(item) { if(item[1] == 10) { result = item; } }); console.log(result); 

You can create a simple find function that iterates the array backwards, and returns as soon as a condition callback returns true .

 var optionsArray = [[1, 10], [2, 10], [3, 10], [4, 10], [6, 14]]; function find10(s) { return s[1] === 10; } function findFromTheEnd(arr, cb) { var l = arr.length; while(l--) { // iterate backwards if(cb(arr[l])){ // if the callback returns true return arr[l]; // return the item } } return null; // return null if none found } var result = findFromTheEnd(optionsArray, find10); console.log(result); 

You can use reduceRight() and return array.

 var arr = [[1, 10], [2, 10], [3, 10], [4, 10], [6, 14]]; var result = arr.reduceRight(function(r, e) { if(e[1] == 10 && !r) r = e; return r; }, 0) console.log(result) 

You can also use for loop that starts from end and break on first match.

 var arr = [[1, 10], [2, 10], [3, 10], [4, 10], [6, 14]]; var result; for (var i = arr.length - 1; i >= 0; i--) { if (arr[i][1] == 10) { result = arr[i] break; } } console.log(result) 

A classic for in the reserve order with a break seems enough :

var optionsArray = [[1, 10], [2, 10], [3, 10], [4, 10], [6, 14]];

var elementFound;
for (var i = optionsArray.length-1; i >=0; i--) {
  if(optionsArray[i].item[1] == 10) {
    elementFound = optionsArray[i].item[1];
    break;
  }
}

If elementFound is not undefined, it refers to the found array.

Rather than considering this as a multidimensional array problem, think of it as an array includes problem nested in an array search problem;

const aarr = [1, 2, 3, 4];
aarr.includes(3); // true
aarr.includes(10); // false

// and

const barr = ['hello', 'world'];
barr.find(item => item[0] === 'h'); // "hello"
barr.find(item => item[3] === 'l'); // "hello"
barr.find(item => item[1] === 'z'); // undefined

So to nest these,

const carr = [[1, 2, 3, 4], [4, 5, 6, 7]];
carr.find(arr => arr.includes(4)); // [1, 2, 3, 4]
carr.find(arr => arr.includes(6)); // [4, 5, 6, 7]

Next, we've reduced the whole problem down to "how to do this in reverse?"

You've a few options depending on how you want to implement it, but a simple way to do it is a shallow clone arr.slice() followed by a reverse arr.reverse() (we use the clone so there are no side-effects of reverse on the original array)

carr.slice().reverse().find(arr => arr.includes(4)); // [4, 5, 6, 7]

If you're working with an index , remember that you'll need to transform those too; -1 is fixed, otherwise transformed_index = arr.length - original_index - 1


Here is how you might implement the reverse of some of the Array methods

const optionsArray = [[1, 10], [2, 10], [3, 10], [4, 10], [6, 14]];
// index               0        1        2        3        4

function arrLast(arr, comparator, method = 'find', transform = x => x) {
    return transform(arr.slice().reverse()[method](comparator), arr);
}
const findLast = (arr, comparator) => arrLast(arr, comparator);
const findLastIndex = (arr, comparator) => arrLast(arr, comparator, 'findIndex', (i, arr) => i === -1 ? -1 : arr.length - i - 1);

arrLast(optionsArray, arr => arr.includes(10)); // [4, 10]
findLastIndex(optionsArray, arr => arr.includes(10)); // 3

If you have to make comparisons among array items and you need to cut short once you are satisfied a while loop is ideal. Accordingly you may do as follows;

 var arr = [[1, 10], [2, 10], [3, 10], [4, 10], [6, 14]], i = 0, result; while (arr[i][1] === arr[++i][1]); result = arr[i-1][0] console.log(result); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM