Asked  7 Months ago    Answers:  5   Viewed   27 times

Here is my JavaScript code so far:

var linkElement = document.getElementById("BackButton");
var loc_array = document.location.href.split('/');
var newT = document.createTextNode(unescape(capWords(loc_array[loc_array.length-2]))); 
linkElement.appendChild(newT);

Currently it takes the second to last item in the array from the URL. However, I want to do a check for the last item in the array to be "index.html" and if so, grab the third to last item instead.

 Answers

48
if (loc_array[loc_array.length - 1] === 'index.html') {
   // do something
} else {
   // something else
}

In the event that your server serves the same file for "index.html" and "inDEX.htML" you can also use: .toLowerCase().

Though, you might want to consider doing this server-side if possible: it will be cleaner and work for people without JS.

Tuesday, June 1, 2021
 
Amber
answered 7 Months ago
13

You can use Pagination

$sections = Section::orderBy('order', 'DESC')->orderBy('id', 'ASC')->paginate(2);
return response()->json($sections );

Then you will get something like (I convert into json)

{
    "total": 17,
    "per_page": 2,
    "current_page": 1,
    "last_page": 9,
    "next_page_url": "{{URL}}?page=2",
    "prev_page_url": null,
    "from": 1,
    "to": 2,
    "data": [
        {
            "id": 40,
            "order":1
        },
        {
            "id": 41,
            "order":2
        },
     ]
 }

Now use the url form "next_page_url" or "prev_page_url"

Wednesday, March 31, 2021
 
Gerardo
answered 9 Months ago
31
end($array);
$referenceToLastElement = &$array[key($array)];
Saturday, May 29, 2021
 
freeMagee
answered 7 Months ago
85

Usage: isEqArrays(arr1, arr2)

//
// Array comparsion
//

function inArray(array, el) {
  for ( var i = array.length; i--; ) {
    if ( array[i] === el ) return true;
  }
  return false;
}

function isEqArrays(arr1, arr2) {
  if ( arr1.length !== arr2.length ) {
    return false;
  }
  for ( var i = arr1.length; i--; ) {
    if ( !inArray( arr2, arr1[i] ) ) {
      return false;
    }
  }
  return true;
}
Thursday, July 29, 2021
 
maniclorn
answered 4 Months ago
74

There is Array.filter():

var numbers = [1, 2, 3, 4, 5];
var filtered = numbers.filter(function(x) { return x > 3; });

// As a JavaScript 1.8 expression closure
filtered = numbers.filter(function(x) x > 3);

Note that Array.filter() is not standard ECMAScript, and it does not appear in ECMAScript specs older than ES5 (thanks Yi Jiang and jAndy). As such, it may not be supported by other ECMAScript dialects like JScript (on MSIE).

Nov 2020 Update: Array.filter is now supported across all major browsers.

Saturday, September 4, 2021
 
Amumu
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share