JS filter array by array within










1















I have an array as following



 [
"id": 68,
"proffesional_photo": "",
"top_image": "https://sampleimage.jpg",
"ratings": "1",
"price": 690,
"description": null,
"type": true,
"promo": 0,
"status": true,
"item":
"Item_name": "Dark Chocolate Latte"
,
"restaurant_dish_menus": [

"id": 1,
"res_dish_id": 1318,
"menu_id": 4,
"createdAt": "2018-11-13T04:28:17.000Z",
"updatedAt": "2018-11-13T04:28:17.000Z"
,

"id": 1,
"res_dish_id": 1318,
"menu_id": 3,
"createdAt": "2018-11-13T04:28:17.000Z",
"updatedAt": "2018-11-13T04:28:17.000Z"

]
,

"id": 69,
"proffesional_photo": "",
"top_image": "https://sampleimage2.jpg",
"ratings": "1",
"price": 700,
"description": null,
"type": true,
"promo": 0,
"status": true,
"item":
"Item_name": "Latte"
,
"restaurant_dish_menus": [

"id": 1,
"res_dish_id": 1318,
"menu_id": 3,
"createdAt": "2018-11-13T04:28:17.000Z",
"updatedAt": "2018-11-13T04:28:17.000Z"

]

],


And when the user select a certain menu it needs to be filtered through it,



Each dish objects may have more than one menu_id,



i attempted using array.filter but i am having trouble figuring out how to filter from the Dish array through the sub array within.



the code i attempted (filterBy = 4)



let result = data.filter(function(row) 
row.restaurant_dish_menus.filter(function(i)
return i.menu_id == filterBy;
);
);


console.log(result) gives me an empty array.



if filterBy is = 4 the expected output is




"id": 68,
"proffesional_photo": "",
"top_image": "https://sampleimage.jpg",
"ratings": "1",
"price": 690,
"description": null,
"type": true,
"promo": 0,
"status": true,
"item":
"Item_name": "Dark Chocolate Latte"
,
"restaurant_dish_menus": [

"id": 1,
"res_dish_id": 1318,
"menu_id": 4,
"createdAt": "2018-11-13T04:28:17.000Z",
"updatedAt": "2018-11-13T04:28:17.000Z"
,

"id": 1,
"res_dish_id": 1318,
"menu_id": 3,
"createdAt": "2018-11-13T04:28:17.000Z",
"updatedAt": "2018-11-13T04:28:17.000Z"

]



And if it filterBy is 3 then both objects should be the output










share|improve this question
























  • Are you trying to filter an array of objects like the one in your first code snippet?

    – Mark Meyer
    Nov 13 '18 at 5:07











  • @MarkMeyer yeah the main array object should be filtered depending on if the restaurant_dish_menus array contains the passed ID

    – Naveed Sheriffdeen
    Nov 13 '18 at 5:10











  • What is your expected outcome? Do you expect an array of dishes or an array of menu items that contain the dish?

    – Andrew
    Nov 13 '18 at 5:11















1















I have an array as following



 [
"id": 68,
"proffesional_photo": "",
"top_image": "https://sampleimage.jpg",
"ratings": "1",
"price": 690,
"description": null,
"type": true,
"promo": 0,
"status": true,
"item":
"Item_name": "Dark Chocolate Latte"
,
"restaurant_dish_menus": [

"id": 1,
"res_dish_id": 1318,
"menu_id": 4,
"createdAt": "2018-11-13T04:28:17.000Z",
"updatedAt": "2018-11-13T04:28:17.000Z"
,

"id": 1,
"res_dish_id": 1318,
"menu_id": 3,
"createdAt": "2018-11-13T04:28:17.000Z",
"updatedAt": "2018-11-13T04:28:17.000Z"

]
,

"id": 69,
"proffesional_photo": "",
"top_image": "https://sampleimage2.jpg",
"ratings": "1",
"price": 700,
"description": null,
"type": true,
"promo": 0,
"status": true,
"item":
"Item_name": "Latte"
,
"restaurant_dish_menus": [

"id": 1,
"res_dish_id": 1318,
"menu_id": 3,
"createdAt": "2018-11-13T04:28:17.000Z",
"updatedAt": "2018-11-13T04:28:17.000Z"

]

],


And when the user select a certain menu it needs to be filtered through it,



Each dish objects may have more than one menu_id,



i attempted using array.filter but i am having trouble figuring out how to filter from the Dish array through the sub array within.



the code i attempted (filterBy = 4)



let result = data.filter(function(row) 
row.restaurant_dish_menus.filter(function(i)
return i.menu_id == filterBy;
);
);


console.log(result) gives me an empty array.



if filterBy is = 4 the expected output is




"id": 68,
"proffesional_photo": "",
"top_image": "https://sampleimage.jpg",
"ratings": "1",
"price": 690,
"description": null,
"type": true,
"promo": 0,
"status": true,
"item":
"Item_name": "Dark Chocolate Latte"
,
"restaurant_dish_menus": [

"id": 1,
"res_dish_id": 1318,
"menu_id": 4,
"createdAt": "2018-11-13T04:28:17.000Z",
"updatedAt": "2018-11-13T04:28:17.000Z"
,

"id": 1,
"res_dish_id": 1318,
"menu_id": 3,
"createdAt": "2018-11-13T04:28:17.000Z",
"updatedAt": "2018-11-13T04:28:17.000Z"

]



And if it filterBy is 3 then both objects should be the output










share|improve this question
























  • Are you trying to filter an array of objects like the one in your first code snippet?

    – Mark Meyer
    Nov 13 '18 at 5:07











  • @MarkMeyer yeah the main array object should be filtered depending on if the restaurant_dish_menus array contains the passed ID

    – Naveed Sheriffdeen
    Nov 13 '18 at 5:10











  • What is your expected outcome? Do you expect an array of dishes or an array of menu items that contain the dish?

    – Andrew
    Nov 13 '18 at 5:11













1












1








1








I have an array as following



 [
"id": 68,
"proffesional_photo": "",
"top_image": "https://sampleimage.jpg",
"ratings": "1",
"price": 690,
"description": null,
"type": true,
"promo": 0,
"status": true,
"item":
"Item_name": "Dark Chocolate Latte"
,
"restaurant_dish_menus": [

"id": 1,
"res_dish_id": 1318,
"menu_id": 4,
"createdAt": "2018-11-13T04:28:17.000Z",
"updatedAt": "2018-11-13T04:28:17.000Z"
,

"id": 1,
"res_dish_id": 1318,
"menu_id": 3,
"createdAt": "2018-11-13T04:28:17.000Z",
"updatedAt": "2018-11-13T04:28:17.000Z"

]
,

"id": 69,
"proffesional_photo": "",
"top_image": "https://sampleimage2.jpg",
"ratings": "1",
"price": 700,
"description": null,
"type": true,
"promo": 0,
"status": true,
"item":
"Item_name": "Latte"
,
"restaurant_dish_menus": [

"id": 1,
"res_dish_id": 1318,
"menu_id": 3,
"createdAt": "2018-11-13T04:28:17.000Z",
"updatedAt": "2018-11-13T04:28:17.000Z"

]

],


And when the user select a certain menu it needs to be filtered through it,



Each dish objects may have more than one menu_id,



i attempted using array.filter but i am having trouble figuring out how to filter from the Dish array through the sub array within.



the code i attempted (filterBy = 4)



let result = data.filter(function(row) 
row.restaurant_dish_menus.filter(function(i)
return i.menu_id == filterBy;
);
);


console.log(result) gives me an empty array.



if filterBy is = 4 the expected output is




"id": 68,
"proffesional_photo": "",
"top_image": "https://sampleimage.jpg",
"ratings": "1",
"price": 690,
"description": null,
"type": true,
"promo": 0,
"status": true,
"item":
"Item_name": "Dark Chocolate Latte"
,
"restaurant_dish_menus": [

"id": 1,
"res_dish_id": 1318,
"menu_id": 4,
"createdAt": "2018-11-13T04:28:17.000Z",
"updatedAt": "2018-11-13T04:28:17.000Z"
,

"id": 1,
"res_dish_id": 1318,
"menu_id": 3,
"createdAt": "2018-11-13T04:28:17.000Z",
"updatedAt": "2018-11-13T04:28:17.000Z"

]



And if it filterBy is 3 then both objects should be the output










share|improve this question
















I have an array as following



 [
"id": 68,
"proffesional_photo": "",
"top_image": "https://sampleimage.jpg",
"ratings": "1",
"price": 690,
"description": null,
"type": true,
"promo": 0,
"status": true,
"item":
"Item_name": "Dark Chocolate Latte"
,
"restaurant_dish_menus": [

"id": 1,
"res_dish_id": 1318,
"menu_id": 4,
"createdAt": "2018-11-13T04:28:17.000Z",
"updatedAt": "2018-11-13T04:28:17.000Z"
,

"id": 1,
"res_dish_id": 1318,
"menu_id": 3,
"createdAt": "2018-11-13T04:28:17.000Z",
"updatedAt": "2018-11-13T04:28:17.000Z"

]
,

"id": 69,
"proffesional_photo": "",
"top_image": "https://sampleimage2.jpg",
"ratings": "1",
"price": 700,
"description": null,
"type": true,
"promo": 0,
"status": true,
"item":
"Item_name": "Latte"
,
"restaurant_dish_menus": [

"id": 1,
"res_dish_id": 1318,
"menu_id": 3,
"createdAt": "2018-11-13T04:28:17.000Z",
"updatedAt": "2018-11-13T04:28:17.000Z"

]

],


And when the user select a certain menu it needs to be filtered through it,



Each dish objects may have more than one menu_id,



i attempted using array.filter but i am having trouble figuring out how to filter from the Dish array through the sub array within.



the code i attempted (filterBy = 4)



let result = data.filter(function(row) 
row.restaurant_dish_menus.filter(function(i)
return i.menu_id == filterBy;
);
);


console.log(result) gives me an empty array.



if filterBy is = 4 the expected output is




"id": 68,
"proffesional_photo": "",
"top_image": "https://sampleimage.jpg",
"ratings": "1",
"price": 690,
"description": null,
"type": true,
"promo": 0,
"status": true,
"item":
"Item_name": "Dark Chocolate Latte"
,
"restaurant_dish_menus": [

"id": 1,
"res_dish_id": 1318,
"menu_id": 4,
"createdAt": "2018-11-13T04:28:17.000Z",
"updatedAt": "2018-11-13T04:28:17.000Z"
,

"id": 1,
"res_dish_id": 1318,
"menu_id": 3,
"createdAt": "2018-11-13T04:28:17.000Z",
"updatedAt": "2018-11-13T04:28:17.000Z"

]



And if it filterBy is 3 then both objects should be the output







javascript arrays array-filter arrayobject






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 5:14







Naveed Sheriffdeen

















asked Nov 13 '18 at 5:03









Naveed SheriffdeenNaveed Sheriffdeen

436315




436315












  • Are you trying to filter an array of objects like the one in your first code snippet?

    – Mark Meyer
    Nov 13 '18 at 5:07











  • @MarkMeyer yeah the main array object should be filtered depending on if the restaurant_dish_menus array contains the passed ID

    – Naveed Sheriffdeen
    Nov 13 '18 at 5:10











  • What is your expected outcome? Do you expect an array of dishes or an array of menu items that contain the dish?

    – Andrew
    Nov 13 '18 at 5:11

















  • Are you trying to filter an array of objects like the one in your first code snippet?

    – Mark Meyer
    Nov 13 '18 at 5:07











  • @MarkMeyer yeah the main array object should be filtered depending on if the restaurant_dish_menus array contains the passed ID

    – Naveed Sheriffdeen
    Nov 13 '18 at 5:10











  • What is your expected outcome? Do you expect an array of dishes or an array of menu items that contain the dish?

    – Andrew
    Nov 13 '18 at 5:11
















Are you trying to filter an array of objects like the one in your first code snippet?

– Mark Meyer
Nov 13 '18 at 5:07





Are you trying to filter an array of objects like the one in your first code snippet?

– Mark Meyer
Nov 13 '18 at 5:07













@MarkMeyer yeah the main array object should be filtered depending on if the restaurant_dish_menus array contains the passed ID

– Naveed Sheriffdeen
Nov 13 '18 at 5:10





@MarkMeyer yeah the main array object should be filtered depending on if the restaurant_dish_menus array contains the passed ID

– Naveed Sheriffdeen
Nov 13 '18 at 5:10













What is your expected outcome? Do you expect an array of dishes or an array of menu items that contain the dish?

– Andrew
Nov 13 '18 at 5:11





What is your expected outcome? Do you expect an array of dishes or an array of menu items that contain the dish?

– Andrew
Nov 13 '18 at 5:11












5 Answers
5






active

oldest

votes


















3














how about this



var data = [
"id": 68,
"proffesional_photo": "",
"top_image": "https://sampleimage.jpg",
"ratings": "1",
"price": 690,
"description": null,
"type": true,
"promo": 0,
"status": true,
"item":
"Item_name": "Dark Chocolate Latte"
,
"restaurant_dish_menus": [

"id": 1,
"res_dish_id": 1318,
"menu_id": 4,
"createdAt": "2018-11-13T04:28:17.000Z",
"updatedAt": "2018-11-13T04:28:17.000Z"
,

"id": 1,
"res_dish_id": 1318,
"menu_id": 3,
"createdAt": "2018-11-13T04:28:17.000Z",
"updatedAt": "2018-11-13T04:28:17.000Z"

]
];

var result = data.filter(function(m)
return m.restaurant_dish_menus.some(function(d)
return d.menu_id === 4;
);
)





share|improve this answer






























    2














    .filter expects the function passed to return a boolean. In your case, the function returns nothing (or undefined) which is always falsy.



    One option is to use .find in the nested filter, and return a boolean depending on whether the result is undefined.



    Here's a snippet.






    let data = [
    "id": 68,
    "proffesional_photo": "",
    "top_image": "https://sampleimage.jpg",
    "ratings": "1",
    "price": 690,
    "description": null,
    "type": true,
    "promo": 0,
    "status": true,
    "item":
    "Item_name": "Dark Chocolate Latte"
    ,
    "restaurant_dish_menus": [
    "id": 1,
    "res_dish_id": 1318,
    "menu_id": 4,
    "createdAt": "2018-11-13T04:28:17.000Z",
    "updatedAt": "2018-11-13T04:28:17.000Z"
    ,

    "id": 1,
    "res_dish_id": 1318,
    "menu_id": 3,
    "createdAt": "2018-11-13T04:28:17.000Z",
    "updatedAt": "2018-11-13T04:28:17.000Z"

    ]
    ,
    "id": 69,
    "proffesional_photo": "",
    "top_image": "https://sampleimage.jpg",
    "ratings": "1",
    "price": 690,
    "description": null,
    "type": true,
    "promo": 0,
    "status": true,
    "item":
    "Item_name": "Dark Chocolate Latte"
    ,
    "restaurant_dish_menus": [
    "id": 1,
    "res_dish_id": 1318,
    "menu_id": 6,
    "createdAt": "2018-11-13T04:28:17.000Z",
    "updatedAt": "2018-11-13T04:28:17.000Z"
    ,

    "id": 1,
    "res_dish_id": 1318,
    "menu_id": 5,
    "createdAt": "2018-11-13T04:28:17.000Z",
    "updatedAt": "2018-11-13T04:28:17.000Z"

    ]
    , ]

    let filterBy = 4;

    let result = data.filter(function(row)
    return row.restaurant_dish_menus.find(function(i)
    return i.menu_id == filterBy;
    ) !== undefined;
    );

    console.log(result);








    share|improve this answer






























      1














      You can use "filter" as below






      var data = [
      "id": 68,
      "proffesional_photo": "",
      "top_image": "https://sampleimage.jpg",
      "ratings": "1",
      "price": 690,
      "description": null,
      "type": true,
      "promo": 0,
      "status": true,
      "item":
      "Item_name": "Dark Chocolate Latte"
      ,
      "restaurant_dish_menus": [

      "id": 1,
      "res_dish_id": 1318,
      "menu_id": 4,
      "createdAt": "2018-11-13T04:28:17.000Z",
      "updatedAt": "2018-11-13T04:28:17.000Z"
      ,

      "id": 1,
      "res_dish_id": 1318,
      "menu_id": 3,
      "createdAt": "2018-11-13T04:28:17.000Z",
      "updatedAt": "2018-11-13T04:28:17.000Z"

      ]
      ,

      "id": 69,
      "proffesional_photo": "",
      "top_image": "https://sampleimage2.jpg",
      "ratings": "1",
      "price": 700,
      "description": null,
      "type": true,
      "promo": 0,
      "status": true,
      "item":
      "Item_name": "Latte"
      ,
      "restaurant_dish_menus": [

      "id": 1,
      "res_dish_id": 1318,
      "menu_id": 3,
      "createdAt": "2018-11-13T04:28:17.000Z",
      "updatedAt": "2018-11-13T04:28:17.000Z"

      ]

      ]

      function filterBy(f)
      return data.filter(d => d.restaurant_dish_menus.some(( menu_id ) => menu_id == f))


      console.log(filterBy(4))

      console.log(filterBy(3))








      share|improve this answer
































        0














        You can also use grep function



        var menus= 
        "id": 68,
        "proffesional_photo": "",
        "top_image": "https://sampleimage.jpg",
        "ratings": "1",
        "price": 690,
        "description": null,
        "type": true,
        "promo": 0,
        "status": true,
        "item":
        "Item_name": "Dark Chocolate Latte"
        ,
        "restaurant_dish_menus": [

        "id": 1,
        "res_dish_id": 1318,
        "menu_id": 4,
        "createdAt": "2018-11-13T04:28:17.000Z",
        "updatedAt": "2018-11-13T04:28:17.000Z"
        ,

        "id": 1,
        "res_dish_id": 1318,
        "menu_id": 3,
        "createdAt": "2018-11-13T04:28:17.000Z",
        "updatedAt": "2018-11-13T04:28:17.000Z"

        ]
        ;

        var found_names = $.grep(menus.restaurant_dish_menus, function(v)
        return v.menu_id ===4;
        );

        console.log(found_names);


        http://jsfiddle.net/ejPV4/






        share|improve this answer






























          0














          Your question a bit unclear of final goal, but if you want to filter top level objects, i.e. if top level object has to be present if and only if it has dish with menu_id === filterBy, then:



          let result = data.filter(row => 
          return row.restaurant_dish_menus.some((menu_id) => menu_id === filterBy);
          );


          Above will only filter your rows if restaurant_dish_menus has items with menu_id === filterBy. But restaurant_dish_menus, of such objects remain unfiltered.



          Result:



          [
          "id": 68,
          // skipped
          "item":
          "Item_name": "Dark Chocolate Latte"
          ,
          "restaurant_dish_menus": [

          "id": 1,
          "res_dish_id": 1318,
          "menu_id": 4,
          // skipped
          ,

          "id": 1,
          "res_dish_id": 1318,
          "menu_id": 3,
          // skipped

          ]
          ]


          But if you want to filter top level and filter restaurant_dish_menus also, i.e. modify top level object, then:



          let result = data.filter(row => 
          return row.restaurant_dish_menus.some((menu_id) => menu_id === filterBy);
          ).map(row =>
          return ...row, restaurant_dish_menus: row.restaurant_dish_menus.filter(i => i.menu_id === filterBy);
          );


          This will first filter the row objects which has menu_id === filterBy, and then also filter restaurant_dish_menus, to include only once menu_id === filterBy, effectively modifying the row object, i.e. map.



          Result:



          [
          "id": 68,
          // skipped
          "item":
          "Item_name": "Dark Chocolate Latte"
          ,
          "restaurant_dish_menus": [

          "id": 1,
          "res_dish_id": 1318,
          "menu_id": 4,
          // skipped

          ]
          ]





          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%2f53274128%2fjs-filter-array-by-array-within%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            5 Answers
            5






            active

            oldest

            votes








            5 Answers
            5






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            3














            how about this



            var data = [
            "id": 68,
            "proffesional_photo": "",
            "top_image": "https://sampleimage.jpg",
            "ratings": "1",
            "price": 690,
            "description": null,
            "type": true,
            "promo": 0,
            "status": true,
            "item":
            "Item_name": "Dark Chocolate Latte"
            ,
            "restaurant_dish_menus": [

            "id": 1,
            "res_dish_id": 1318,
            "menu_id": 4,
            "createdAt": "2018-11-13T04:28:17.000Z",
            "updatedAt": "2018-11-13T04:28:17.000Z"
            ,

            "id": 1,
            "res_dish_id": 1318,
            "menu_id": 3,
            "createdAt": "2018-11-13T04:28:17.000Z",
            "updatedAt": "2018-11-13T04:28:17.000Z"

            ]
            ];

            var result = data.filter(function(m)
            return m.restaurant_dish_menus.some(function(d)
            return d.menu_id === 4;
            );
            )





            share|improve this answer



























              3














              how about this



              var data = [
              "id": 68,
              "proffesional_photo": "",
              "top_image": "https://sampleimage.jpg",
              "ratings": "1",
              "price": 690,
              "description": null,
              "type": true,
              "promo": 0,
              "status": true,
              "item":
              "Item_name": "Dark Chocolate Latte"
              ,
              "restaurant_dish_menus": [

              "id": 1,
              "res_dish_id": 1318,
              "menu_id": 4,
              "createdAt": "2018-11-13T04:28:17.000Z",
              "updatedAt": "2018-11-13T04:28:17.000Z"
              ,

              "id": 1,
              "res_dish_id": 1318,
              "menu_id": 3,
              "createdAt": "2018-11-13T04:28:17.000Z",
              "updatedAt": "2018-11-13T04:28:17.000Z"

              ]
              ];

              var result = data.filter(function(m)
              return m.restaurant_dish_menus.some(function(d)
              return d.menu_id === 4;
              );
              )





              share|improve this answer

























                3












                3








                3







                how about this



                var data = [
                "id": 68,
                "proffesional_photo": "",
                "top_image": "https://sampleimage.jpg",
                "ratings": "1",
                "price": 690,
                "description": null,
                "type": true,
                "promo": 0,
                "status": true,
                "item":
                "Item_name": "Dark Chocolate Latte"
                ,
                "restaurant_dish_menus": [

                "id": 1,
                "res_dish_id": 1318,
                "menu_id": 4,
                "createdAt": "2018-11-13T04:28:17.000Z",
                "updatedAt": "2018-11-13T04:28:17.000Z"
                ,

                "id": 1,
                "res_dish_id": 1318,
                "menu_id": 3,
                "createdAt": "2018-11-13T04:28:17.000Z",
                "updatedAt": "2018-11-13T04:28:17.000Z"

                ]
                ];

                var result = data.filter(function(m)
                return m.restaurant_dish_menus.some(function(d)
                return d.menu_id === 4;
                );
                )





                share|improve this answer













                how about this



                var data = [
                "id": 68,
                "proffesional_photo": "",
                "top_image": "https://sampleimage.jpg",
                "ratings": "1",
                "price": 690,
                "description": null,
                "type": true,
                "promo": 0,
                "status": true,
                "item":
                "Item_name": "Dark Chocolate Latte"
                ,
                "restaurant_dish_menus": [

                "id": 1,
                "res_dish_id": 1318,
                "menu_id": 4,
                "createdAt": "2018-11-13T04:28:17.000Z",
                "updatedAt": "2018-11-13T04:28:17.000Z"
                ,

                "id": 1,
                "res_dish_id": 1318,
                "menu_id": 3,
                "createdAt": "2018-11-13T04:28:17.000Z",
                "updatedAt": "2018-11-13T04:28:17.000Z"

                ]
                ];

                var result = data.filter(function(m)
                return m.restaurant_dish_menus.some(function(d)
                return d.menu_id === 4;
                );
                )






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 13 '18 at 5:21









                AndrewAndrew

                3,94111836




                3,94111836























                    2














                    .filter expects the function passed to return a boolean. In your case, the function returns nothing (or undefined) which is always falsy.



                    One option is to use .find in the nested filter, and return a boolean depending on whether the result is undefined.



                    Here's a snippet.






                    let data = [
                    "id": 68,
                    "proffesional_photo": "",
                    "top_image": "https://sampleimage.jpg",
                    "ratings": "1",
                    "price": 690,
                    "description": null,
                    "type": true,
                    "promo": 0,
                    "status": true,
                    "item":
                    "Item_name": "Dark Chocolate Latte"
                    ,
                    "restaurant_dish_menus": [
                    "id": 1,
                    "res_dish_id": 1318,
                    "menu_id": 4,
                    "createdAt": "2018-11-13T04:28:17.000Z",
                    "updatedAt": "2018-11-13T04:28:17.000Z"
                    ,

                    "id": 1,
                    "res_dish_id": 1318,
                    "menu_id": 3,
                    "createdAt": "2018-11-13T04:28:17.000Z",
                    "updatedAt": "2018-11-13T04:28:17.000Z"

                    ]
                    ,
                    "id": 69,
                    "proffesional_photo": "",
                    "top_image": "https://sampleimage.jpg",
                    "ratings": "1",
                    "price": 690,
                    "description": null,
                    "type": true,
                    "promo": 0,
                    "status": true,
                    "item":
                    "Item_name": "Dark Chocolate Latte"
                    ,
                    "restaurant_dish_menus": [
                    "id": 1,
                    "res_dish_id": 1318,
                    "menu_id": 6,
                    "createdAt": "2018-11-13T04:28:17.000Z",
                    "updatedAt": "2018-11-13T04:28:17.000Z"
                    ,

                    "id": 1,
                    "res_dish_id": 1318,
                    "menu_id": 5,
                    "createdAt": "2018-11-13T04:28:17.000Z",
                    "updatedAt": "2018-11-13T04:28:17.000Z"

                    ]
                    , ]

                    let filterBy = 4;

                    let result = data.filter(function(row)
                    return row.restaurant_dish_menus.find(function(i)
                    return i.menu_id == filterBy;
                    ) !== undefined;
                    );

                    console.log(result);








                    share|improve this answer



























                      2














                      .filter expects the function passed to return a boolean. In your case, the function returns nothing (or undefined) which is always falsy.



                      One option is to use .find in the nested filter, and return a boolean depending on whether the result is undefined.



                      Here's a snippet.






                      let data = [
                      "id": 68,
                      "proffesional_photo": "",
                      "top_image": "https://sampleimage.jpg",
                      "ratings": "1",
                      "price": 690,
                      "description": null,
                      "type": true,
                      "promo": 0,
                      "status": true,
                      "item":
                      "Item_name": "Dark Chocolate Latte"
                      ,
                      "restaurant_dish_menus": [
                      "id": 1,
                      "res_dish_id": 1318,
                      "menu_id": 4,
                      "createdAt": "2018-11-13T04:28:17.000Z",
                      "updatedAt": "2018-11-13T04:28:17.000Z"
                      ,

                      "id": 1,
                      "res_dish_id": 1318,
                      "menu_id": 3,
                      "createdAt": "2018-11-13T04:28:17.000Z",
                      "updatedAt": "2018-11-13T04:28:17.000Z"

                      ]
                      ,
                      "id": 69,
                      "proffesional_photo": "",
                      "top_image": "https://sampleimage.jpg",
                      "ratings": "1",
                      "price": 690,
                      "description": null,
                      "type": true,
                      "promo": 0,
                      "status": true,
                      "item":
                      "Item_name": "Dark Chocolate Latte"
                      ,
                      "restaurant_dish_menus": [
                      "id": 1,
                      "res_dish_id": 1318,
                      "menu_id": 6,
                      "createdAt": "2018-11-13T04:28:17.000Z",
                      "updatedAt": "2018-11-13T04:28:17.000Z"
                      ,

                      "id": 1,
                      "res_dish_id": 1318,
                      "menu_id": 5,
                      "createdAt": "2018-11-13T04:28:17.000Z",
                      "updatedAt": "2018-11-13T04:28:17.000Z"

                      ]
                      , ]

                      let filterBy = 4;

                      let result = data.filter(function(row)
                      return row.restaurant_dish_menus.find(function(i)
                      return i.menu_id == filterBy;
                      ) !== undefined;
                      );

                      console.log(result);








                      share|improve this answer

























                        2












                        2








                        2







                        .filter expects the function passed to return a boolean. In your case, the function returns nothing (or undefined) which is always falsy.



                        One option is to use .find in the nested filter, and return a boolean depending on whether the result is undefined.



                        Here's a snippet.






                        let data = [
                        "id": 68,
                        "proffesional_photo": "",
                        "top_image": "https://sampleimage.jpg",
                        "ratings": "1",
                        "price": 690,
                        "description": null,
                        "type": true,
                        "promo": 0,
                        "status": true,
                        "item":
                        "Item_name": "Dark Chocolate Latte"
                        ,
                        "restaurant_dish_menus": [
                        "id": 1,
                        "res_dish_id": 1318,
                        "menu_id": 4,
                        "createdAt": "2018-11-13T04:28:17.000Z",
                        "updatedAt": "2018-11-13T04:28:17.000Z"
                        ,

                        "id": 1,
                        "res_dish_id": 1318,
                        "menu_id": 3,
                        "createdAt": "2018-11-13T04:28:17.000Z",
                        "updatedAt": "2018-11-13T04:28:17.000Z"

                        ]
                        ,
                        "id": 69,
                        "proffesional_photo": "",
                        "top_image": "https://sampleimage.jpg",
                        "ratings": "1",
                        "price": 690,
                        "description": null,
                        "type": true,
                        "promo": 0,
                        "status": true,
                        "item":
                        "Item_name": "Dark Chocolate Latte"
                        ,
                        "restaurant_dish_menus": [
                        "id": 1,
                        "res_dish_id": 1318,
                        "menu_id": 6,
                        "createdAt": "2018-11-13T04:28:17.000Z",
                        "updatedAt": "2018-11-13T04:28:17.000Z"
                        ,

                        "id": 1,
                        "res_dish_id": 1318,
                        "menu_id": 5,
                        "createdAt": "2018-11-13T04:28:17.000Z",
                        "updatedAt": "2018-11-13T04:28:17.000Z"

                        ]
                        , ]

                        let filterBy = 4;

                        let result = data.filter(function(row)
                        return row.restaurant_dish_menus.find(function(i)
                        return i.menu_id == filterBy;
                        ) !== undefined;
                        );

                        console.log(result);








                        share|improve this answer













                        .filter expects the function passed to return a boolean. In your case, the function returns nothing (or undefined) which is always falsy.



                        One option is to use .find in the nested filter, and return a boolean depending on whether the result is undefined.



                        Here's a snippet.






                        let data = [
                        "id": 68,
                        "proffesional_photo": "",
                        "top_image": "https://sampleimage.jpg",
                        "ratings": "1",
                        "price": 690,
                        "description": null,
                        "type": true,
                        "promo": 0,
                        "status": true,
                        "item":
                        "Item_name": "Dark Chocolate Latte"
                        ,
                        "restaurant_dish_menus": [
                        "id": 1,
                        "res_dish_id": 1318,
                        "menu_id": 4,
                        "createdAt": "2018-11-13T04:28:17.000Z",
                        "updatedAt": "2018-11-13T04:28:17.000Z"
                        ,

                        "id": 1,
                        "res_dish_id": 1318,
                        "menu_id": 3,
                        "createdAt": "2018-11-13T04:28:17.000Z",
                        "updatedAt": "2018-11-13T04:28:17.000Z"

                        ]
                        ,
                        "id": 69,
                        "proffesional_photo": "",
                        "top_image": "https://sampleimage.jpg",
                        "ratings": "1",
                        "price": 690,
                        "description": null,
                        "type": true,
                        "promo": 0,
                        "status": true,
                        "item":
                        "Item_name": "Dark Chocolate Latte"
                        ,
                        "restaurant_dish_menus": [
                        "id": 1,
                        "res_dish_id": 1318,
                        "menu_id": 6,
                        "createdAt": "2018-11-13T04:28:17.000Z",
                        "updatedAt": "2018-11-13T04:28:17.000Z"
                        ,

                        "id": 1,
                        "res_dish_id": 1318,
                        "menu_id": 5,
                        "createdAt": "2018-11-13T04:28:17.000Z",
                        "updatedAt": "2018-11-13T04:28:17.000Z"

                        ]
                        , ]

                        let filterBy = 4;

                        let result = data.filter(function(row)
                        return row.restaurant_dish_menus.find(function(i)
                        return i.menu_id == filterBy;
                        ) !== undefined;
                        );

                        console.log(result);








                        let data = [
                        "id": 68,
                        "proffesional_photo": "",
                        "top_image": "https://sampleimage.jpg",
                        "ratings": "1",
                        "price": 690,
                        "description": null,
                        "type": true,
                        "promo": 0,
                        "status": true,
                        "item":
                        "Item_name": "Dark Chocolate Latte"
                        ,
                        "restaurant_dish_menus": [
                        "id": 1,
                        "res_dish_id": 1318,
                        "menu_id": 4,
                        "createdAt": "2018-11-13T04:28:17.000Z",
                        "updatedAt": "2018-11-13T04:28:17.000Z"
                        ,

                        "id": 1,
                        "res_dish_id": 1318,
                        "menu_id": 3,
                        "createdAt": "2018-11-13T04:28:17.000Z",
                        "updatedAt": "2018-11-13T04:28:17.000Z"

                        ]
                        ,
                        "id": 69,
                        "proffesional_photo": "",
                        "top_image": "https://sampleimage.jpg",
                        "ratings": "1",
                        "price": 690,
                        "description": null,
                        "type": true,
                        "promo": 0,
                        "status": true,
                        "item":
                        "Item_name": "Dark Chocolate Latte"
                        ,
                        "restaurant_dish_menus": [
                        "id": 1,
                        "res_dish_id": 1318,
                        "menu_id": 6,
                        "createdAt": "2018-11-13T04:28:17.000Z",
                        "updatedAt": "2018-11-13T04:28:17.000Z"
                        ,

                        "id": 1,
                        "res_dish_id": 1318,
                        "menu_id": 5,
                        "createdAt": "2018-11-13T04:28:17.000Z",
                        "updatedAt": "2018-11-13T04:28:17.000Z"

                        ]
                        , ]

                        let filterBy = 4;

                        let result = data.filter(function(row)
                        return row.restaurant_dish_menus.find(function(i)
                        return i.menu_id == filterBy;
                        ) !== undefined;
                        );

                        console.log(result);





                        let data = [
                        "id": 68,
                        "proffesional_photo": "",
                        "top_image": "https://sampleimage.jpg",
                        "ratings": "1",
                        "price": 690,
                        "description": null,
                        "type": true,
                        "promo": 0,
                        "status": true,
                        "item":
                        "Item_name": "Dark Chocolate Latte"
                        ,
                        "restaurant_dish_menus": [
                        "id": 1,
                        "res_dish_id": 1318,
                        "menu_id": 4,
                        "createdAt": "2018-11-13T04:28:17.000Z",
                        "updatedAt": "2018-11-13T04:28:17.000Z"
                        ,

                        "id": 1,
                        "res_dish_id": 1318,
                        "menu_id": 3,
                        "createdAt": "2018-11-13T04:28:17.000Z",
                        "updatedAt": "2018-11-13T04:28:17.000Z"

                        ]
                        ,
                        "id": 69,
                        "proffesional_photo": "",
                        "top_image": "https://sampleimage.jpg",
                        "ratings": "1",
                        "price": 690,
                        "description": null,
                        "type": true,
                        "promo": 0,
                        "status": true,
                        "item":
                        "Item_name": "Dark Chocolate Latte"
                        ,
                        "restaurant_dish_menus": [
                        "id": 1,
                        "res_dish_id": 1318,
                        "menu_id": 6,
                        "createdAt": "2018-11-13T04:28:17.000Z",
                        "updatedAt": "2018-11-13T04:28:17.000Z"
                        ,

                        "id": 1,
                        "res_dish_id": 1318,
                        "menu_id": 5,
                        "createdAt": "2018-11-13T04:28:17.000Z",
                        "updatedAt": "2018-11-13T04:28:17.000Z"

                        ]
                        , ]

                        let filterBy = 4;

                        let result = data.filter(function(row)
                        return row.restaurant_dish_menus.find(function(i)
                        return i.menu_id == filterBy;
                        ) !== undefined;
                        );

                        console.log(result);






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Nov 13 '18 at 5:15









                        Ayush GuptaAyush Gupta

                        3,0561643




                        3,0561643





















                            1














                            You can use "filter" as below






                            var data = [
                            "id": 68,
                            "proffesional_photo": "",
                            "top_image": "https://sampleimage.jpg",
                            "ratings": "1",
                            "price": 690,
                            "description": null,
                            "type": true,
                            "promo": 0,
                            "status": true,
                            "item":
                            "Item_name": "Dark Chocolate Latte"
                            ,
                            "restaurant_dish_menus": [

                            "id": 1,
                            "res_dish_id": 1318,
                            "menu_id": 4,
                            "createdAt": "2018-11-13T04:28:17.000Z",
                            "updatedAt": "2018-11-13T04:28:17.000Z"
                            ,

                            "id": 1,
                            "res_dish_id": 1318,
                            "menu_id": 3,
                            "createdAt": "2018-11-13T04:28:17.000Z",
                            "updatedAt": "2018-11-13T04:28:17.000Z"

                            ]
                            ,

                            "id": 69,
                            "proffesional_photo": "",
                            "top_image": "https://sampleimage2.jpg",
                            "ratings": "1",
                            "price": 700,
                            "description": null,
                            "type": true,
                            "promo": 0,
                            "status": true,
                            "item":
                            "Item_name": "Latte"
                            ,
                            "restaurant_dish_menus": [

                            "id": 1,
                            "res_dish_id": 1318,
                            "menu_id": 3,
                            "createdAt": "2018-11-13T04:28:17.000Z",
                            "updatedAt": "2018-11-13T04:28:17.000Z"

                            ]

                            ]

                            function filterBy(f)
                            return data.filter(d => d.restaurant_dish_menus.some(( menu_id ) => menu_id == f))


                            console.log(filterBy(4))

                            console.log(filterBy(3))








                            share|improve this answer





























                              1














                              You can use "filter" as below






                              var data = [
                              "id": 68,
                              "proffesional_photo": "",
                              "top_image": "https://sampleimage.jpg",
                              "ratings": "1",
                              "price": 690,
                              "description": null,
                              "type": true,
                              "promo": 0,
                              "status": true,
                              "item":
                              "Item_name": "Dark Chocolate Latte"
                              ,
                              "restaurant_dish_menus": [

                              "id": 1,
                              "res_dish_id": 1318,
                              "menu_id": 4,
                              "createdAt": "2018-11-13T04:28:17.000Z",
                              "updatedAt": "2018-11-13T04:28:17.000Z"
                              ,

                              "id": 1,
                              "res_dish_id": 1318,
                              "menu_id": 3,
                              "createdAt": "2018-11-13T04:28:17.000Z",
                              "updatedAt": "2018-11-13T04:28:17.000Z"

                              ]
                              ,

                              "id": 69,
                              "proffesional_photo": "",
                              "top_image": "https://sampleimage2.jpg",
                              "ratings": "1",
                              "price": 700,
                              "description": null,
                              "type": true,
                              "promo": 0,
                              "status": true,
                              "item":
                              "Item_name": "Latte"
                              ,
                              "restaurant_dish_menus": [

                              "id": 1,
                              "res_dish_id": 1318,
                              "menu_id": 3,
                              "createdAt": "2018-11-13T04:28:17.000Z",
                              "updatedAt": "2018-11-13T04:28:17.000Z"

                              ]

                              ]

                              function filterBy(f)
                              return data.filter(d => d.restaurant_dish_menus.some(( menu_id ) => menu_id == f))


                              console.log(filterBy(4))

                              console.log(filterBy(3))








                              share|improve this answer



























                                1












                                1








                                1







                                You can use "filter" as below






                                var data = [
                                "id": 68,
                                "proffesional_photo": "",
                                "top_image": "https://sampleimage.jpg",
                                "ratings": "1",
                                "price": 690,
                                "description": null,
                                "type": true,
                                "promo": 0,
                                "status": true,
                                "item":
                                "Item_name": "Dark Chocolate Latte"
                                ,
                                "restaurant_dish_menus": [

                                "id": 1,
                                "res_dish_id": 1318,
                                "menu_id": 4,
                                "createdAt": "2018-11-13T04:28:17.000Z",
                                "updatedAt": "2018-11-13T04:28:17.000Z"
                                ,

                                "id": 1,
                                "res_dish_id": 1318,
                                "menu_id": 3,
                                "createdAt": "2018-11-13T04:28:17.000Z",
                                "updatedAt": "2018-11-13T04:28:17.000Z"

                                ]
                                ,

                                "id": 69,
                                "proffesional_photo": "",
                                "top_image": "https://sampleimage2.jpg",
                                "ratings": "1",
                                "price": 700,
                                "description": null,
                                "type": true,
                                "promo": 0,
                                "status": true,
                                "item":
                                "Item_name": "Latte"
                                ,
                                "restaurant_dish_menus": [

                                "id": 1,
                                "res_dish_id": 1318,
                                "menu_id": 3,
                                "createdAt": "2018-11-13T04:28:17.000Z",
                                "updatedAt": "2018-11-13T04:28:17.000Z"

                                ]

                                ]

                                function filterBy(f)
                                return data.filter(d => d.restaurant_dish_menus.some(( menu_id ) => menu_id == f))


                                console.log(filterBy(4))

                                console.log(filterBy(3))








                                share|improve this answer















                                You can use "filter" as below






                                var data = [
                                "id": 68,
                                "proffesional_photo": "",
                                "top_image": "https://sampleimage.jpg",
                                "ratings": "1",
                                "price": 690,
                                "description": null,
                                "type": true,
                                "promo": 0,
                                "status": true,
                                "item":
                                "Item_name": "Dark Chocolate Latte"
                                ,
                                "restaurant_dish_menus": [

                                "id": 1,
                                "res_dish_id": 1318,
                                "menu_id": 4,
                                "createdAt": "2018-11-13T04:28:17.000Z",
                                "updatedAt": "2018-11-13T04:28:17.000Z"
                                ,

                                "id": 1,
                                "res_dish_id": 1318,
                                "menu_id": 3,
                                "createdAt": "2018-11-13T04:28:17.000Z",
                                "updatedAt": "2018-11-13T04:28:17.000Z"

                                ]
                                ,

                                "id": 69,
                                "proffesional_photo": "",
                                "top_image": "https://sampleimage2.jpg",
                                "ratings": "1",
                                "price": 700,
                                "description": null,
                                "type": true,
                                "promo": 0,
                                "status": true,
                                "item":
                                "Item_name": "Latte"
                                ,
                                "restaurant_dish_menus": [

                                "id": 1,
                                "res_dish_id": 1318,
                                "menu_id": 3,
                                "createdAt": "2018-11-13T04:28:17.000Z",
                                "updatedAt": "2018-11-13T04:28:17.000Z"

                                ]

                                ]

                                function filterBy(f)
                                return data.filter(d => d.restaurant_dish_menus.some(( menu_id ) => menu_id == f))


                                console.log(filterBy(4))

                                console.log(filterBy(3))








                                var data = [
                                "id": 68,
                                "proffesional_photo": "",
                                "top_image": "https://sampleimage.jpg",
                                "ratings": "1",
                                "price": 690,
                                "description": null,
                                "type": true,
                                "promo": 0,
                                "status": true,
                                "item":
                                "Item_name": "Dark Chocolate Latte"
                                ,
                                "restaurant_dish_menus": [

                                "id": 1,
                                "res_dish_id": 1318,
                                "menu_id": 4,
                                "createdAt": "2018-11-13T04:28:17.000Z",
                                "updatedAt": "2018-11-13T04:28:17.000Z"
                                ,

                                "id": 1,
                                "res_dish_id": 1318,
                                "menu_id": 3,
                                "createdAt": "2018-11-13T04:28:17.000Z",
                                "updatedAt": "2018-11-13T04:28:17.000Z"

                                ]
                                ,

                                "id": 69,
                                "proffesional_photo": "",
                                "top_image": "https://sampleimage2.jpg",
                                "ratings": "1",
                                "price": 700,
                                "description": null,
                                "type": true,
                                "promo": 0,
                                "status": true,
                                "item":
                                "Item_name": "Latte"
                                ,
                                "restaurant_dish_menus": [

                                "id": 1,
                                "res_dish_id": 1318,
                                "menu_id": 3,
                                "createdAt": "2018-11-13T04:28:17.000Z",
                                "updatedAt": "2018-11-13T04:28:17.000Z"

                                ]

                                ]

                                function filterBy(f)
                                return data.filter(d => d.restaurant_dish_menus.some(( menu_id ) => menu_id == f))


                                console.log(filterBy(4))

                                console.log(filterBy(3))





                                var data = [
                                "id": 68,
                                "proffesional_photo": "",
                                "top_image": "https://sampleimage.jpg",
                                "ratings": "1",
                                "price": 690,
                                "description": null,
                                "type": true,
                                "promo": 0,
                                "status": true,
                                "item":
                                "Item_name": "Dark Chocolate Latte"
                                ,
                                "restaurant_dish_menus": [

                                "id": 1,
                                "res_dish_id": 1318,
                                "menu_id": 4,
                                "createdAt": "2018-11-13T04:28:17.000Z",
                                "updatedAt": "2018-11-13T04:28:17.000Z"
                                ,

                                "id": 1,
                                "res_dish_id": 1318,
                                "menu_id": 3,
                                "createdAt": "2018-11-13T04:28:17.000Z",
                                "updatedAt": "2018-11-13T04:28:17.000Z"

                                ]
                                ,

                                "id": 69,
                                "proffesional_photo": "",
                                "top_image": "https://sampleimage2.jpg",
                                "ratings": "1",
                                "price": 700,
                                "description": null,
                                "type": true,
                                "promo": 0,
                                "status": true,
                                "item":
                                "Item_name": "Latte"
                                ,
                                "restaurant_dish_menus": [

                                "id": 1,
                                "res_dish_id": 1318,
                                "menu_id": 3,
                                "createdAt": "2018-11-13T04:28:17.000Z",
                                "updatedAt": "2018-11-13T04:28:17.000Z"

                                ]

                                ]

                                function filterBy(f)
                                return data.filter(d => d.restaurant_dish_menus.some(( menu_id ) => menu_id == f))


                                console.log(filterBy(4))

                                console.log(filterBy(3))






                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Nov 13 '18 at 6:35

























                                answered Nov 13 '18 at 5:28









                                Nitish NarangNitish Narang

                                2,9401815




                                2,9401815





















                                    0














                                    You can also use grep function



                                    var menus= 
                                    "id": 68,
                                    "proffesional_photo": "",
                                    "top_image": "https://sampleimage.jpg",
                                    "ratings": "1",
                                    "price": 690,
                                    "description": null,
                                    "type": true,
                                    "promo": 0,
                                    "status": true,
                                    "item":
                                    "Item_name": "Dark Chocolate Latte"
                                    ,
                                    "restaurant_dish_menus": [

                                    "id": 1,
                                    "res_dish_id": 1318,
                                    "menu_id": 4,
                                    "createdAt": "2018-11-13T04:28:17.000Z",
                                    "updatedAt": "2018-11-13T04:28:17.000Z"
                                    ,

                                    "id": 1,
                                    "res_dish_id": 1318,
                                    "menu_id": 3,
                                    "createdAt": "2018-11-13T04:28:17.000Z",
                                    "updatedAt": "2018-11-13T04:28:17.000Z"

                                    ]
                                    ;

                                    var found_names = $.grep(menus.restaurant_dish_menus, function(v)
                                    return v.menu_id ===4;
                                    );

                                    console.log(found_names);


                                    http://jsfiddle.net/ejPV4/






                                    share|improve this answer



























                                      0














                                      You can also use grep function



                                      var menus= 
                                      "id": 68,
                                      "proffesional_photo": "",
                                      "top_image": "https://sampleimage.jpg",
                                      "ratings": "1",
                                      "price": 690,
                                      "description": null,
                                      "type": true,
                                      "promo": 0,
                                      "status": true,
                                      "item":
                                      "Item_name": "Dark Chocolate Latte"
                                      ,
                                      "restaurant_dish_menus": [

                                      "id": 1,
                                      "res_dish_id": 1318,
                                      "menu_id": 4,
                                      "createdAt": "2018-11-13T04:28:17.000Z",
                                      "updatedAt": "2018-11-13T04:28:17.000Z"
                                      ,

                                      "id": 1,
                                      "res_dish_id": 1318,
                                      "menu_id": 3,
                                      "createdAt": "2018-11-13T04:28:17.000Z",
                                      "updatedAt": "2018-11-13T04:28:17.000Z"

                                      ]
                                      ;

                                      var found_names = $.grep(menus.restaurant_dish_menus, function(v)
                                      return v.menu_id ===4;
                                      );

                                      console.log(found_names);


                                      http://jsfiddle.net/ejPV4/






                                      share|improve this answer

























                                        0












                                        0








                                        0







                                        You can also use grep function



                                        var menus= 
                                        "id": 68,
                                        "proffesional_photo": "",
                                        "top_image": "https://sampleimage.jpg",
                                        "ratings": "1",
                                        "price": 690,
                                        "description": null,
                                        "type": true,
                                        "promo": 0,
                                        "status": true,
                                        "item":
                                        "Item_name": "Dark Chocolate Latte"
                                        ,
                                        "restaurant_dish_menus": [

                                        "id": 1,
                                        "res_dish_id": 1318,
                                        "menu_id": 4,
                                        "createdAt": "2018-11-13T04:28:17.000Z",
                                        "updatedAt": "2018-11-13T04:28:17.000Z"
                                        ,

                                        "id": 1,
                                        "res_dish_id": 1318,
                                        "menu_id": 3,
                                        "createdAt": "2018-11-13T04:28:17.000Z",
                                        "updatedAt": "2018-11-13T04:28:17.000Z"

                                        ]
                                        ;

                                        var found_names = $.grep(menus.restaurant_dish_menus, function(v)
                                        return v.menu_id ===4;
                                        );

                                        console.log(found_names);


                                        http://jsfiddle.net/ejPV4/






                                        share|improve this answer













                                        You can also use grep function



                                        var menus= 
                                        "id": 68,
                                        "proffesional_photo": "",
                                        "top_image": "https://sampleimage.jpg",
                                        "ratings": "1",
                                        "price": 690,
                                        "description": null,
                                        "type": true,
                                        "promo": 0,
                                        "status": true,
                                        "item":
                                        "Item_name": "Dark Chocolate Latte"
                                        ,
                                        "restaurant_dish_menus": [

                                        "id": 1,
                                        "res_dish_id": 1318,
                                        "menu_id": 4,
                                        "createdAt": "2018-11-13T04:28:17.000Z",
                                        "updatedAt": "2018-11-13T04:28:17.000Z"
                                        ,

                                        "id": 1,
                                        "res_dish_id": 1318,
                                        "menu_id": 3,
                                        "createdAt": "2018-11-13T04:28:17.000Z",
                                        "updatedAt": "2018-11-13T04:28:17.000Z"

                                        ]
                                        ;

                                        var found_names = $.grep(menus.restaurant_dish_menus, function(v)
                                        return v.menu_id ===4;
                                        );

                                        console.log(found_names);


                                        http://jsfiddle.net/ejPV4/







                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Nov 13 '18 at 5:12









                                        Jeswin RebilJeswin Rebil

                                        360514




                                        360514





















                                            0














                                            Your question a bit unclear of final goal, but if you want to filter top level objects, i.e. if top level object has to be present if and only if it has dish with menu_id === filterBy, then:



                                            let result = data.filter(row => 
                                            return row.restaurant_dish_menus.some((menu_id) => menu_id === filterBy);
                                            );


                                            Above will only filter your rows if restaurant_dish_menus has items with menu_id === filterBy. But restaurant_dish_menus, of such objects remain unfiltered.



                                            Result:



                                            [
                                            "id": 68,
                                            // skipped
                                            "item":
                                            "Item_name": "Dark Chocolate Latte"
                                            ,
                                            "restaurant_dish_menus": [

                                            "id": 1,
                                            "res_dish_id": 1318,
                                            "menu_id": 4,
                                            // skipped
                                            ,

                                            "id": 1,
                                            "res_dish_id": 1318,
                                            "menu_id": 3,
                                            // skipped

                                            ]
                                            ]


                                            But if you want to filter top level and filter restaurant_dish_menus also, i.e. modify top level object, then:



                                            let result = data.filter(row => 
                                            return row.restaurant_dish_menus.some((menu_id) => menu_id === filterBy);
                                            ).map(row =>
                                            return ...row, restaurant_dish_menus: row.restaurant_dish_menus.filter(i => i.menu_id === filterBy);
                                            );


                                            This will first filter the row objects which has menu_id === filterBy, and then also filter restaurant_dish_menus, to include only once menu_id === filterBy, effectively modifying the row object, i.e. map.



                                            Result:



                                            [
                                            "id": 68,
                                            // skipped
                                            "item":
                                            "Item_name": "Dark Chocolate Latte"
                                            ,
                                            "restaurant_dish_menus": [

                                            "id": 1,
                                            "res_dish_id": 1318,
                                            "menu_id": 4,
                                            // skipped

                                            ]
                                            ]





                                            share|improve this answer





























                                              0














                                              Your question a bit unclear of final goal, but if you want to filter top level objects, i.e. if top level object has to be present if and only if it has dish with menu_id === filterBy, then:



                                              let result = data.filter(row => 
                                              return row.restaurant_dish_menus.some((menu_id) => menu_id === filterBy);
                                              );


                                              Above will only filter your rows if restaurant_dish_menus has items with menu_id === filterBy. But restaurant_dish_menus, of such objects remain unfiltered.



                                              Result:



                                              [
                                              "id": 68,
                                              // skipped
                                              "item":
                                              "Item_name": "Dark Chocolate Latte"
                                              ,
                                              "restaurant_dish_menus": [

                                              "id": 1,
                                              "res_dish_id": 1318,
                                              "menu_id": 4,
                                              // skipped
                                              ,

                                              "id": 1,
                                              "res_dish_id": 1318,
                                              "menu_id": 3,
                                              // skipped

                                              ]
                                              ]


                                              But if you want to filter top level and filter restaurant_dish_menus also, i.e. modify top level object, then:



                                              let result = data.filter(row => 
                                              return row.restaurant_dish_menus.some((menu_id) => menu_id === filterBy);
                                              ).map(row =>
                                              return ...row, restaurant_dish_menus: row.restaurant_dish_menus.filter(i => i.menu_id === filterBy);
                                              );


                                              This will first filter the row objects which has menu_id === filterBy, and then also filter restaurant_dish_menus, to include only once menu_id === filterBy, effectively modifying the row object, i.e. map.



                                              Result:



                                              [
                                              "id": 68,
                                              // skipped
                                              "item":
                                              "Item_name": "Dark Chocolate Latte"
                                              ,
                                              "restaurant_dish_menus": [

                                              "id": 1,
                                              "res_dish_id": 1318,
                                              "menu_id": 4,
                                              // skipped

                                              ]
                                              ]





                                              share|improve this answer



























                                                0












                                                0








                                                0







                                                Your question a bit unclear of final goal, but if you want to filter top level objects, i.e. if top level object has to be present if and only if it has dish with menu_id === filterBy, then:



                                                let result = data.filter(row => 
                                                return row.restaurant_dish_menus.some((menu_id) => menu_id === filterBy);
                                                );


                                                Above will only filter your rows if restaurant_dish_menus has items with menu_id === filterBy. But restaurant_dish_menus, of such objects remain unfiltered.



                                                Result:



                                                [
                                                "id": 68,
                                                // skipped
                                                "item":
                                                "Item_name": "Dark Chocolate Latte"
                                                ,
                                                "restaurant_dish_menus": [

                                                "id": 1,
                                                "res_dish_id": 1318,
                                                "menu_id": 4,
                                                // skipped
                                                ,

                                                "id": 1,
                                                "res_dish_id": 1318,
                                                "menu_id": 3,
                                                // skipped

                                                ]
                                                ]


                                                But if you want to filter top level and filter restaurant_dish_menus also, i.e. modify top level object, then:



                                                let result = data.filter(row => 
                                                return row.restaurant_dish_menus.some((menu_id) => menu_id === filterBy);
                                                ).map(row =>
                                                return ...row, restaurant_dish_menus: row.restaurant_dish_menus.filter(i => i.menu_id === filterBy);
                                                );


                                                This will first filter the row objects which has menu_id === filterBy, and then also filter restaurant_dish_menus, to include only once menu_id === filterBy, effectively modifying the row object, i.e. map.



                                                Result:



                                                [
                                                "id": 68,
                                                // skipped
                                                "item":
                                                "Item_name": "Dark Chocolate Latte"
                                                ,
                                                "restaurant_dish_menus": [

                                                "id": 1,
                                                "res_dish_id": 1318,
                                                "menu_id": 4,
                                                // skipped

                                                ]
                                                ]





                                                share|improve this answer















                                                Your question a bit unclear of final goal, but if you want to filter top level objects, i.e. if top level object has to be present if and only if it has dish with menu_id === filterBy, then:



                                                let result = data.filter(row => 
                                                return row.restaurant_dish_menus.some((menu_id) => menu_id === filterBy);
                                                );


                                                Above will only filter your rows if restaurant_dish_menus has items with menu_id === filterBy. But restaurant_dish_menus, of such objects remain unfiltered.



                                                Result:



                                                [
                                                "id": 68,
                                                // skipped
                                                "item":
                                                "Item_name": "Dark Chocolate Latte"
                                                ,
                                                "restaurant_dish_menus": [

                                                "id": 1,
                                                "res_dish_id": 1318,
                                                "menu_id": 4,
                                                // skipped
                                                ,

                                                "id": 1,
                                                "res_dish_id": 1318,
                                                "menu_id": 3,
                                                // skipped

                                                ]
                                                ]


                                                But if you want to filter top level and filter restaurant_dish_menus also, i.e. modify top level object, then:



                                                let result = data.filter(row => 
                                                return row.restaurant_dish_menus.some((menu_id) => menu_id === filterBy);
                                                ).map(row =>
                                                return ...row, restaurant_dish_menus: row.restaurant_dish_menus.filter(i => i.menu_id === filterBy);
                                                );


                                                This will first filter the row objects which has menu_id === filterBy, and then also filter restaurant_dish_menus, to include only once menu_id === filterBy, effectively modifying the row object, i.e. map.



                                                Result:



                                                [
                                                "id": 68,
                                                // skipped
                                                "item":
                                                "Item_name": "Dark Chocolate Latte"
                                                ,
                                                "restaurant_dish_menus": [

                                                "id": 1,
                                                "res_dish_id": 1318,
                                                "menu_id": 4,
                                                // skipped

                                                ]
                                                ]






                                                share|improve this answer














                                                share|improve this answer



                                                share|improve this answer








                                                edited Nov 13 '18 at 5:31

























                                                answered Nov 13 '18 at 5:15









                                                muradmmuradm

                                                864519




                                                864519



























                                                    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.




                                                    draft saved


                                                    draft discarded














                                                    StackExchange.ready(
                                                    function ()
                                                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53274128%2fjs-filter-array-by-array-within%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

                                                    Use pre created SQLite database for Android project in kotlin

                                                    Darth Vader #20

                                                    Ondo