Ad

Extract Desired Value From Array By Searching With Suffix

I have an array like this:

{
  "result": [
    {
      "name": "2015.494782.1981AugustSapthagirikannada.gif",
      "source": "derivative",
      "format": "Animated GIF",
      "original": "2015.494782.1981AugustSapthagirikannada_jp2.zip",
      "mtime": "1490522158",
      "size": "270516",
      "md5": "5f0758aaef567d76916620f2ed568cb9",
      "crc32": "f70dda86",
      "sha1": "99c63d12e5d8c115a66d51715d998cd2534254a1"
    },
    {
      "name": "2015.494782.1981AugustSapthagirikannada.pdf",
      "source": "original",
      "mtime": "1484723772",
      "size": "7488708",
      "md5": "eecd0d8ae44a69fa0b398f8e00dda573",
      "crc32": "f06bf0f0",
      "sha1": "f00af63426b93a9587b0b59b20cc4beb79482243",
      "format": "Image Container PDF"
    }
  ]
}

Where i would like to extract the filename that ends with ".pdf".

Expected result :

2015.494782.1981AugustSapthagirikannada.pdf
Ad

Answer

You could use find if you only want one. Or filter and map if there may be multiple ones:

const data = {"result":[{"name":"2015.494782.1981AugustSapthagirikannada.gif","source":"derivative","format":"Animated GIF","original":"2015.494782.1981AugustSapthagirikannada_jp2.zip","mtime":"1490522158","size":"270516","md5":"5f0758aaef567d76916620f2ed568cb9","crc32":"f70dda86","sha1":"99c63d12e5d8c115a66d51715d998cd2534254a1"},{"name":"2015.494782.1981AugustSapthagirikannada.pdf","source":"original","mtime":"1484723772","size":"7488708","md5":"eecd0d8ae44a69fa0b398f8e00dda573","crc32":"f06bf0f0","sha1":"f00af63426b93a9587b0b59b20cc4beb79482243","format":"Image Container PDF"}]};
// Will return the first it finds
console.log(
  data.result.find(file => file.name.endsWith('.pdf')).name
);
// Will return an Array with all PDF file names
console.log(
  data.result.filter(file => file.name.endsWith('.pdf')).map(file => file.name)
);

Note: the code above uses ES6 syntax (const, arrow functions, .endsWith) which won't work in IE and other old browsers if you use it as-is without transpilation. If you want a cross-browser version:

var data = {"result":[{"name":"2015.494782.1981AugustSapthagirikannada.gif","source":"derivative","format":"Animated GIF","original":"2015.494782.1981AugustSapthagirikannada_jp2.zip","mtime":"1490522158","size":"270516","md5":"5f0758aaef567d76916620f2ed568cb9","crc32":"f70dda86","sha1":"99c63d12e5d8c115a66d51715d998cd2534254a1"},{"name":"2015.494782.1981AugustSapthagirikannada.pdf","source":"original","mtime":"1484723772","size":"7488708","md5":"eecd0d8ae44a69fa0b398f8e00dda573","crc32":"f06bf0f0","sha1":"f00af63426b93a9587b0b59b20cc4beb79482243","format":"Image Container PDF"}]};
// Will return the first it finds
console.log(
  data.result.find(function(file) {
    return file.name.slice(-4) === '.pdf';
  }).name
);
// Will return an Array with all PDF file names
console.log(
  data.result.filter(function(file) {
    return file.name.slice(-4) === '.pdf';
  }).map(function(file) {
    return file.name
  })
);

Ad
source: stackoverflow.com
Ad