-2

I know that for an array it can be used last function of underscore, so in the case of this array it would be:

myArray = [32, 1, 8, 31]; 
lastElement = _.last(myArray);

The problem is when there is a matrix like this:

myArray = [[1, 3, 5], [55, 66, 77], [0, 1, 2]]; 

and the wanted result is

lastElement = [5, 77, 2];

Any suggestions?

1
  • _.last(_.zip(...myArray)) Commented Mar 9, 2018 at 9:48

9 Answers 9

2

Use map and slice (Won't mutate the original array)

[[1, 3, 5], [55, 66, 77], [0, 1, 2]].map( s => s.slice(-1)[0] );
Sign up to request clarification or add additional context in comments.

Comments

2

You can just do

var lastElement = myArray.map(_.last);

Comments

1

You could simply use Array.from :

var myArray = [[1, 3, 5], [55, 66, 77], [0, 1, 2]]; 

var res = Array.from(myArray, x => x[x.length - 1]);

console.log(res);

Another possibility not already answered here would be Array#reduce :

var myArray = [[1, 3, 5], [55, 66, 77], [0, 1, 2]]; 

var res = myArray.reduce((acc, curr) => acc.concat(curr[curr.length - 1]),[]);

console.log(res);

Comments

1
var lastElement = myArray.map((x) => {
   return _.last(x);
});

Comments

1

Or you can also use ES6 map

let myArray = [[1, 3, 5], [55, 66, 77], [0, 1, 2]]; 
let result = myArray.map(v => v[ v.length - 1] );

console.log(result );

Comments

1

Check this out. Iterate over the array using map and extract last element.

No need of any library.

let myArray = [[1, 3, 5], [55, 66, 77], [0, 1, 2]]; 
let output = [];

output = myArray.map(m => m[m.length - 1] )

console.log(output)

3 Comments

This is a bad implementation of .map. Please check other answers
Why is this a bad implementation? Any reference you have for your statement?
I don't have any reference as of now but I'll try to explain. .map is used to get some mutated/parsed value of every item. It returns n items but with specific value. .forEach will loop over and process values but will not return anything. So if you use .map but do not return value or do not respect/accept returned value, its essentially same as .forEach. All array functions are meant for a reason. Yes you can achieve same task from all methods but you should use them for intended purpose, else you are killing their essence.
0

You can use array.prototype.map to transorm each subarray to the last element in it. You can get those last elements with array.prototype.pop or arr[arr.length - 1] if you don't want to mutate myArray:

var myArray = [[1, 3, 5], [55, 66, 77], [0, 1, 2]]; 
var lastElements = myArray.map(arr => arr.pop());
console.log(lastElements);

1 Comment

.pop() will mutate array. Better use .slice().pop() or arr[arr.length - 1]
0

Perhaps loop through them with a for loop.

lastElement = [];

for (var i = 0 ; i < myArray.length ; i++) {
    lastElement.push(_.last(myArray[i]));
}

Comments

0

Use Following Code:-

var myArray = [[1, 3, 5], [55, 66, 77], [0, 1, 2]];
lastElement = _.map(myArray, _.last);
console.log(lastElement)
// **Result is lastElement = [5, 77, 2];**
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js"></script>

1 Comment

I have added lodash's CDN link and wrapped code in snippet(<> icon in editor). if you do not need the edit, please revert the changes.

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.