1

Having a bi-dimensional array like this

anArray = [[1, 2, 3], [null, 4, null, null], [1, 0, null, 5, null]];

my goal is to get as result a new array containing last element of each of the sub-arrays but also to be non-null. In this case: [3, 4, 5]

For getting the last element of each I used:

anArray.map(a => a.slice(-1)[0]); - it gets the last element of each sub-array.

For getting the last non-null element of an array it works using this:

_.findLast([1, null, 2, null], (el) => el !==null); 

It works for a simple array but I don't know how to use it for a bi-dimensional array. Any ideas?

2
  • 2
    Hello Samurai Jack. Why not simple combine the answer from this question with one of the answer from these questions or the comment from this deleted question? Commented Mar 9, 2018 at 11:20
  • you may continue to use your underscorejs func: anArray.map((a) => a[_.findLastIndex(a, (el) => el !== null)]) Commented Mar 9, 2018 at 11:35

7 Answers 7

1

Use map and reduceRight (based on this)

var anArray = [[1, 2, 3], [null, 4, null, null], [1, 0, null, 5, null]];
anArray.map( arr => arr.reduceRight( (a,c) => ( c != null && a == null ? c : a) , null))
Sign up to request clarification or add additional context in comments.

Comments

1

You can use a combination of Array#map, Array#filter and Array#slice:

.map() will go through your initial array

.filter() will filter out the null values from your sub-arrays

.slice() will select the last element from each filtered sub-array

var anArray = [[1, 2, 3], [null, 4, null, null], [1, 0, null, 5, null]];

var res = anArray.map(o => o.filter(n => n != null).slice(-1)[0]);

console.log(res);

2 Comments

You might be best using slice not splice or you will mutate the original array..
@Keith Corrected it :)
1

You could use concat with spread syntax and then map and filter methods to remove null.

const anArray = [[1, 2, 3], [null, 4, null, null], [1, 0, null, 5, null]];

const result = [].concat(...anArray.map(e => e.filter(Boolean).slice(-1)))
console.log(result)

Comments

0

You could pop the last element for mapping.

var array = [[1, 2, 3], [null, 4, null, null], [1, 0, null, 5, null]];
    result = array.map(o => o.filter(n => n != null).pop());

console.log(result);

Comments

0

You can use Array#map in addition to _.findLast:

anArray.map(a => _.findLast(a, el => el !== null));

Working example:

let anArray = [
  [1, 2, 3],
  [null, 4, null, null],
  [1, 0, null, 5, null]
];

let result = anArray.map(a => _.findLast(a, el => el !== null));
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js"></script>

Comments

0

You can try like this:

Idea:

  • Use map to return value for every sub-array.
  • Use while to loop backwards and get first not-null value.

Note: while because, most of the looping mechanism would loop over all values. To break at first match, and to reduce number of iterations, I prefer this.

var anArray = [ [1, 2, 3], [null, 4, null, null], [1, 0, null, 5, null] ];
var result = anArray.map((subArray) => {
  var i = subArray.length - 1;
  while (i > 0) {
    if (subArray[i] !== null) {
      return subArray[i];
    }
    i--;
  }
})
console.log(result)

Comments

0
var anArray = [[1, 2, 3], [null, 4, null, null], [1, 0, null, 5, null]];
var anArray = anArray.map(function(arr){
    return findLastTruthyElement(arr);
});
console.log(anArray);
function findLastTruthyElement(arr){
    if(!Array.isArray(arr)){
        throw 'passed element is not array';
        return undefined;
    }
    arr = arr.filter(function(item){
        if(item) return true;
        else return false;
    });
    return arr[arr.length-1];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.