Fetch and filter Images in array by field value width












1














How can I get url to only get the image in the array that has width: 2048? Alternatively, perhaps simpler is every image that I require has at the end of the filename TABLET_LANDSCAPE_LARGE_16_9.jpg



I am using createRemoteFileNode in gatsby-node to import images in a random array from an external api as



fileNode = await createRemoteFileNode({
url: event.images.url,
cache,
store,
createNode,
createNodeId
});


where as event is returned as



{ "events": [
{
"id": "177YvfG65Xi34_c",
"images": [
{
"url": "///.jpg",
"width": 305,
"height": 203
},
{
"url": "///.jpg",
"width": 1024,
"height": 683
},
{
"url": "///.jpg",
"width": 2048,
"height": 1152
}
]
},
{
"id": "Z7r9jZ1AeC4_Y",
"images": [
{
"url": "///.jpg",
"width": 305,
"height": 203
},
{
"url": "///.jpg",
"width": 2048,
"height": 1152,
},
{
"url": "///.jpg",
"width": 1136,
"height": 639,
}
]
}
]}


I need only the url that has a width >= 1900 but the url in the array are random, therefore event.images[2].url may work for the first events data but not second.





For those needing more information, here is the full code



const { createRemoteFileNode } = require(`gatsby-source-filesystem`);
const axios = require('axios');

// Replace ACCESS_TOKEN with your Instagram token
const API_URI = `https://app.ticketmaster.com/discovery/v2/events.json?countryCode=AU&size=100&apikey=${API_KEY}`;

exports.sourceNodes = async ({ actions, store, cache, createNodeId }) => {
const { createNode, createNodeField } = actions;
// Fetch data
const { data } = await axios.get(API_URI);

// use for loop for async/await support
for (const event of data._embedded.events) {
let fileNode;
try {
fileNode = await createRemoteFileNode({
url: event.images.url,
cache,
store,
createNode,
createNodeId
});
} catch (error) {
console.warn('error creating node', error);
}
}
};









share|improve this question





























    1














    How can I get url to only get the image in the array that has width: 2048? Alternatively, perhaps simpler is every image that I require has at the end of the filename TABLET_LANDSCAPE_LARGE_16_9.jpg



    I am using createRemoteFileNode in gatsby-node to import images in a random array from an external api as



    fileNode = await createRemoteFileNode({
    url: event.images.url,
    cache,
    store,
    createNode,
    createNodeId
    });


    where as event is returned as



    { "events": [
    {
    "id": "177YvfG65Xi34_c",
    "images": [
    {
    "url": "///.jpg",
    "width": 305,
    "height": 203
    },
    {
    "url": "///.jpg",
    "width": 1024,
    "height": 683
    },
    {
    "url": "///.jpg",
    "width": 2048,
    "height": 1152
    }
    ]
    },
    {
    "id": "Z7r9jZ1AeC4_Y",
    "images": [
    {
    "url": "///.jpg",
    "width": 305,
    "height": 203
    },
    {
    "url": "///.jpg",
    "width": 2048,
    "height": 1152,
    },
    {
    "url": "///.jpg",
    "width": 1136,
    "height": 639,
    }
    ]
    }
    ]}


    I need only the url that has a width >= 1900 but the url in the array are random, therefore event.images[2].url may work for the first events data but not second.





    For those needing more information, here is the full code



    const { createRemoteFileNode } = require(`gatsby-source-filesystem`);
    const axios = require('axios');

    // Replace ACCESS_TOKEN with your Instagram token
    const API_URI = `https://app.ticketmaster.com/discovery/v2/events.json?countryCode=AU&size=100&apikey=${API_KEY}`;

    exports.sourceNodes = async ({ actions, store, cache, createNodeId }) => {
    const { createNode, createNodeField } = actions;
    // Fetch data
    const { data } = await axios.get(API_URI);

    // use for loop for async/await support
    for (const event of data._embedded.events) {
    let fileNode;
    try {
    fileNode = await createRemoteFileNode({
    url: event.images.url,
    cache,
    store,
    createNode,
    createNodeId
    });
    } catch (error) {
    console.warn('error creating node', error);
    }
    }
    };









    share|improve this question



























      1












      1








      1







      How can I get url to only get the image in the array that has width: 2048? Alternatively, perhaps simpler is every image that I require has at the end of the filename TABLET_LANDSCAPE_LARGE_16_9.jpg



      I am using createRemoteFileNode in gatsby-node to import images in a random array from an external api as



      fileNode = await createRemoteFileNode({
      url: event.images.url,
      cache,
      store,
      createNode,
      createNodeId
      });


      where as event is returned as



      { "events": [
      {
      "id": "177YvfG65Xi34_c",
      "images": [
      {
      "url": "///.jpg",
      "width": 305,
      "height": 203
      },
      {
      "url": "///.jpg",
      "width": 1024,
      "height": 683
      },
      {
      "url": "///.jpg",
      "width": 2048,
      "height": 1152
      }
      ]
      },
      {
      "id": "Z7r9jZ1AeC4_Y",
      "images": [
      {
      "url": "///.jpg",
      "width": 305,
      "height": 203
      },
      {
      "url": "///.jpg",
      "width": 2048,
      "height": 1152,
      },
      {
      "url": "///.jpg",
      "width": 1136,
      "height": 639,
      }
      ]
      }
      ]}


      I need only the url that has a width >= 1900 but the url in the array are random, therefore event.images[2].url may work for the first events data but not second.





      For those needing more information, here is the full code



      const { createRemoteFileNode } = require(`gatsby-source-filesystem`);
      const axios = require('axios');

      // Replace ACCESS_TOKEN with your Instagram token
      const API_URI = `https://app.ticketmaster.com/discovery/v2/events.json?countryCode=AU&size=100&apikey=${API_KEY}`;

      exports.sourceNodes = async ({ actions, store, cache, createNodeId }) => {
      const { createNode, createNodeField } = actions;
      // Fetch data
      const { data } = await axios.get(API_URI);

      // use for loop for async/await support
      for (const event of data._embedded.events) {
      let fileNode;
      try {
      fileNode = await createRemoteFileNode({
      url: event.images.url,
      cache,
      store,
      createNode,
      createNodeId
      });
      } catch (error) {
      console.warn('error creating node', error);
      }
      }
      };









      share|improve this question















      How can I get url to only get the image in the array that has width: 2048? Alternatively, perhaps simpler is every image that I require has at the end of the filename TABLET_LANDSCAPE_LARGE_16_9.jpg



      I am using createRemoteFileNode in gatsby-node to import images in a random array from an external api as



      fileNode = await createRemoteFileNode({
      url: event.images.url,
      cache,
      store,
      createNode,
      createNodeId
      });


      where as event is returned as



      { "events": [
      {
      "id": "177YvfG65Xi34_c",
      "images": [
      {
      "url": "///.jpg",
      "width": 305,
      "height": 203
      },
      {
      "url": "///.jpg",
      "width": 1024,
      "height": 683
      },
      {
      "url": "///.jpg",
      "width": 2048,
      "height": 1152
      }
      ]
      },
      {
      "id": "Z7r9jZ1AeC4_Y",
      "images": [
      {
      "url": "///.jpg",
      "width": 305,
      "height": 203
      },
      {
      "url": "///.jpg",
      "width": 2048,
      "height": 1152,
      },
      {
      "url": "///.jpg",
      "width": 1136,
      "height": 639,
      }
      ]
      }
      ]}


      I need only the url that has a width >= 1900 but the url in the array are random, therefore event.images[2].url may work for the first events data but not second.





      For those needing more information, here is the full code



      const { createRemoteFileNode } = require(`gatsby-source-filesystem`);
      const axios = require('axios');

      // Replace ACCESS_TOKEN with your Instagram token
      const API_URI = `https://app.ticketmaster.com/discovery/v2/events.json?countryCode=AU&size=100&apikey=${API_KEY}`;

      exports.sourceNodes = async ({ actions, store, cache, createNodeId }) => {
      const { createNode, createNodeField } = actions;
      // Fetch data
      const { data } = await axios.get(API_URI);

      // use for loop for async/await support
      for (const event of data._embedded.events) {
      let fileNode;
      try {
      fileNode = await createRemoteFileNode({
      url: event.images.url,
      cache,
      store,
      createNode,
      createNodeId
      });
      } catch (error) {
      console.warn('error creating node', error);
      }
      }
      };






      javascript reactjs axios gatsby






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 16 at 3:00

























      asked Nov 16 at 2:47









      Darren

      245627




      245627
























          1 Answer
          1






          active

          oldest

          votes


















          1














          if the results you are expecting is the 'events' you can try this... hope it helps.



           const obj = {
          "events": [
          {
          "id": "177YvfG65Xi34_c",
          "images": [
          {
          "url": "///.jpg",
          "width": 305,
          "height": 203
          },
          {
          "url": "///.jpg",
          "width": 1024,
          "height": 683
          },
          {
          "url": "///.jpg",
          "width": 2048,
          "height": 1152
          }
          ]
          },
          {
          "id": "Z7r9jZ1AeC4_Y",
          "images": [
          {
          "url": "///.jpg",
          "width": 305,
          "height": 203
          },
          {
          "url": "///.jpg",
          "width": 2048,
          "height": 1152,
          },
          {
          "url": "///.jpg",
          "width": 1136,
          "height": 639,
          }
          ]
          }
          ]
          }

          const events = obj.events;
          let output = ;
          events.forEach(elm => {
          let filterResults = elm.images.filter(e => e.width >= 1900);
          output.push([...filterResults]);
          });

          console.log(output);





          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53330712%2ffetch-and-filter-images-in-array-by-field-value-width%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            if the results you are expecting is the 'events' you can try this... hope it helps.



             const obj = {
            "events": [
            {
            "id": "177YvfG65Xi34_c",
            "images": [
            {
            "url": "///.jpg",
            "width": 305,
            "height": 203
            },
            {
            "url": "///.jpg",
            "width": 1024,
            "height": 683
            },
            {
            "url": "///.jpg",
            "width": 2048,
            "height": 1152
            }
            ]
            },
            {
            "id": "Z7r9jZ1AeC4_Y",
            "images": [
            {
            "url": "///.jpg",
            "width": 305,
            "height": 203
            },
            {
            "url": "///.jpg",
            "width": 2048,
            "height": 1152,
            },
            {
            "url": "///.jpg",
            "width": 1136,
            "height": 639,
            }
            ]
            }
            ]
            }

            const events = obj.events;
            let output = ;
            events.forEach(elm => {
            let filterResults = elm.images.filter(e => e.width >= 1900);
            output.push([...filterResults]);
            });

            console.log(output);





            share|improve this answer


























              1














              if the results you are expecting is the 'events' you can try this... hope it helps.



               const obj = {
              "events": [
              {
              "id": "177YvfG65Xi34_c",
              "images": [
              {
              "url": "///.jpg",
              "width": 305,
              "height": 203
              },
              {
              "url": "///.jpg",
              "width": 1024,
              "height": 683
              },
              {
              "url": "///.jpg",
              "width": 2048,
              "height": 1152
              }
              ]
              },
              {
              "id": "Z7r9jZ1AeC4_Y",
              "images": [
              {
              "url": "///.jpg",
              "width": 305,
              "height": 203
              },
              {
              "url": "///.jpg",
              "width": 2048,
              "height": 1152,
              },
              {
              "url": "///.jpg",
              "width": 1136,
              "height": 639,
              }
              ]
              }
              ]
              }

              const events = obj.events;
              let output = ;
              events.forEach(elm => {
              let filterResults = elm.images.filter(e => e.width >= 1900);
              output.push([...filterResults]);
              });

              console.log(output);





              share|improve this answer
























                1












                1








                1






                if the results you are expecting is the 'events' you can try this... hope it helps.



                 const obj = {
                "events": [
                {
                "id": "177YvfG65Xi34_c",
                "images": [
                {
                "url": "///.jpg",
                "width": 305,
                "height": 203
                },
                {
                "url": "///.jpg",
                "width": 1024,
                "height": 683
                },
                {
                "url": "///.jpg",
                "width": 2048,
                "height": 1152
                }
                ]
                },
                {
                "id": "Z7r9jZ1AeC4_Y",
                "images": [
                {
                "url": "///.jpg",
                "width": 305,
                "height": 203
                },
                {
                "url": "///.jpg",
                "width": 2048,
                "height": 1152,
                },
                {
                "url": "///.jpg",
                "width": 1136,
                "height": 639,
                }
                ]
                }
                ]
                }

                const events = obj.events;
                let output = ;
                events.forEach(elm => {
                let filterResults = elm.images.filter(e => e.width >= 1900);
                output.push([...filterResults]);
                });

                console.log(output);





                share|improve this answer












                if the results you are expecting is the 'events' you can try this... hope it helps.



                 const obj = {
                "events": [
                {
                "id": "177YvfG65Xi34_c",
                "images": [
                {
                "url": "///.jpg",
                "width": 305,
                "height": 203
                },
                {
                "url": "///.jpg",
                "width": 1024,
                "height": 683
                },
                {
                "url": "///.jpg",
                "width": 2048,
                "height": 1152
                }
                ]
                },
                {
                "id": "Z7r9jZ1AeC4_Y",
                "images": [
                {
                "url": "///.jpg",
                "width": 305,
                "height": 203
                },
                {
                "url": "///.jpg",
                "width": 2048,
                "height": 1152,
                },
                {
                "url": "///.jpg",
                "width": 1136,
                "height": 639,
                }
                ]
                }
                ]
                }

                const events = obj.events;
                let output = ;
                events.forEach(elm => {
                let filterResults = elm.images.filter(e => e.width >= 1900);
                output.push([...filterResults]);
                });

                console.log(output);






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 16 at 4:50









                Samso Iyanda

                817




                817






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53330712%2ffetch-and-filter-images-in-array-by-field-value-width%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    How to send String Array data to Server using php in android

                    Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

                    Is anime1.com a legal site for watching anime?