Filtering array with objects inside an object

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
7
down vote

favorite
1












I have a problem with filtering an array with nested objects.



[
"firstName": "Kevin",
"lastName": "Smith",
"expenses":
"drink1": 25,
"drink2": 20

,

"firstName": "John",
"lastName": "Rambo",
"expenses":
"coffe": 10,
"cake": 20


]


I want to get the objects where the sum of all expenses is > 35. How to get inside expenses? Or maybe filter is not a proper way here.










share|improve this question























  • This should give you an idea: Sum all properties in object. "Or maybe filter is not a proper way here." It's absolutely the right way. You just need to sum the values of expenses inside the filter callback.
    – Felix Kling
    2 hours ago











  • const result = arr.filter(obj => Object.values(obj.expenses).every(expense => expense > 45))
    – Pavlo
    2 hours ago











  • What code are you using to evaluate?
    – Mister Positive
    2 hours ago











  • Change it to 25. How to iterate and get values of all expenses and filter that object?
    – Kamil Staszewski
    2 hours ago






  • 1




    I want to receive objects where the sum of values inside a expenses object is above 25. That the example. Your code doesn't work : /
    – Kamil Staszewski
    2 hours ago














up vote
7
down vote

favorite
1












I have a problem with filtering an array with nested objects.



[
"firstName": "Kevin",
"lastName": "Smith",
"expenses":
"drink1": 25,
"drink2": 20

,

"firstName": "John",
"lastName": "Rambo",
"expenses":
"coffe": 10,
"cake": 20


]


I want to get the objects where the sum of all expenses is > 35. How to get inside expenses? Or maybe filter is not a proper way here.










share|improve this question























  • This should give you an idea: Sum all properties in object. "Or maybe filter is not a proper way here." It's absolutely the right way. You just need to sum the values of expenses inside the filter callback.
    – Felix Kling
    2 hours ago











  • const result = arr.filter(obj => Object.values(obj.expenses).every(expense => expense > 45))
    – Pavlo
    2 hours ago











  • What code are you using to evaluate?
    – Mister Positive
    2 hours ago











  • Change it to 25. How to iterate and get values of all expenses and filter that object?
    – Kamil Staszewski
    2 hours ago






  • 1




    I want to receive objects where the sum of values inside a expenses object is above 25. That the example. Your code doesn't work : /
    – Kamil Staszewski
    2 hours ago












up vote
7
down vote

favorite
1









up vote
7
down vote

favorite
1






1





I have a problem with filtering an array with nested objects.



[
"firstName": "Kevin",
"lastName": "Smith",
"expenses":
"drink1": 25,
"drink2": 20

,

"firstName": "John",
"lastName": "Rambo",
"expenses":
"coffe": 10,
"cake": 20


]


I want to get the objects where the sum of all expenses is > 35. How to get inside expenses? Or maybe filter is not a proper way here.










share|improve this question















I have a problem with filtering an array with nested objects.



[
"firstName": "Kevin",
"lastName": "Smith",
"expenses":
"drink1": 25,
"drink2": 20

,

"firstName": "John",
"lastName": "Rambo",
"expenses":
"coffe": 10,
"cake": 20


]


I want to get the objects where the sum of all expenses is > 35. How to get inside expenses? Or maybe filter is not a proper way here.







javascript arrays






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 hours ago









Felix Kling

535k121835885




535k121835885










asked 2 hours ago









Kamil Staszewski

1009




1009











  • This should give you an idea: Sum all properties in object. "Or maybe filter is not a proper way here." It's absolutely the right way. You just need to sum the values of expenses inside the filter callback.
    – Felix Kling
    2 hours ago











  • const result = arr.filter(obj => Object.values(obj.expenses).every(expense => expense > 45))
    – Pavlo
    2 hours ago











  • What code are you using to evaluate?
    – Mister Positive
    2 hours ago











  • Change it to 25. How to iterate and get values of all expenses and filter that object?
    – Kamil Staszewski
    2 hours ago






  • 1




    I want to receive objects where the sum of values inside a expenses object is above 25. That the example. Your code doesn't work : /
    – Kamil Staszewski
    2 hours ago
















  • This should give you an idea: Sum all properties in object. "Or maybe filter is not a proper way here." It's absolutely the right way. You just need to sum the values of expenses inside the filter callback.
    – Felix Kling
    2 hours ago











  • const result = arr.filter(obj => Object.values(obj.expenses).every(expense => expense > 45))
    – Pavlo
    2 hours ago











  • What code are you using to evaluate?
    – Mister Positive
    2 hours ago











  • Change it to 25. How to iterate and get values of all expenses and filter that object?
    – Kamil Staszewski
    2 hours ago






  • 1




    I want to receive objects where the sum of values inside a expenses object is above 25. That the example. Your code doesn't work : /
    – Kamil Staszewski
    2 hours ago















This should give you an idea: Sum all properties in object. "Or maybe filter is not a proper way here." It's absolutely the right way. You just need to sum the values of expenses inside the filter callback.
– Felix Kling
2 hours ago





This should give you an idea: Sum all properties in object. "Or maybe filter is not a proper way here." It's absolutely the right way. You just need to sum the values of expenses inside the filter callback.
– Felix Kling
2 hours ago













const result = arr.filter(obj => Object.values(obj.expenses).every(expense => expense > 45))
– Pavlo
2 hours ago





const result = arr.filter(obj => Object.values(obj.expenses).every(expense => expense > 45))
– Pavlo
2 hours ago













What code are you using to evaluate?
– Mister Positive
2 hours ago





What code are you using to evaluate?
– Mister Positive
2 hours ago













Change it to 25. How to iterate and get values of all expenses and filter that object?
– Kamil Staszewski
2 hours ago




Change it to 25. How to iterate and get values of all expenses and filter that object?
– Kamil Staszewski
2 hours ago




1




1




I want to receive objects where the sum of values inside a expenses object is above 25. That the example. Your code doesn't work : /
– Kamil Staszewski
2 hours ago




I want to receive objects where the sum of values inside a expenses object is above 25. That the example. Your code doesn't work : /
– Kamil Staszewski
2 hours ago












5 Answers
5






active

oldest

votes

















up vote
9
down vote













Just filter it, with a condition using reduce to sum the expenses! Pretty straight forward :)






const input = [
"firstName": "Kevin",
"lastName": "Smith",
"expenses":
"drink1": 26,
"drink2": 20

,

"firstName": "John",
"lastName": "Rambo",
"expenses":
"coffe": 10,
"cake": 20


];

const output = input.filter(user => Object.values(user.expenses).reduce((acc, expense) => acc + expense) > 45);
console.log(output);








share|improve this answer




















  • We have a winner. Thank you!
    – Kamil Staszewski
    2 hours ago










  • Glad it helps ;) Don't forget to mark the answer as accepted so that it can help the rest of the community :)
    – sjahan
    2 hours ago










  • I didn't knew about Object.values. Nice!
    – Donny Verduijn
    2 hours ago

















up vote
0
down vote













You can try something like this






var data = [
"firstName": "Kevin",
"lastName": "Smith",
"expenses":
"drink1": 25,
"drink2": 20

,

"firstName": "John",
"lastName": "Rambo",
"expenses":
"coffe": 10,
"cake": 20


]

var filtered = data.filter(c => Object.keys(c.expenses)
.reduce(function(p, e)
return p + c.expenses[e]
, 0) >= 35
);

console.log(filtered);








share|improve this answer





























    up vote
    0
    down vote













    You can access object at index i inside array arr with expression arr[i]
    What you need to do is to loop over your array. Inside your loop you access each object with expression i have mentioned: arr[i] and then on this object you can access expenses following way arr[i].expenses after this - if i am understanding correctly you sum contents of your arr[i].expenses object and select those objects which satisfy your condition.
    Please, See code below:



    var expensesAbove35Arr = ;

    var yourArray = [

    "firstName": "Kevin",
    "lastName": "Smith",
    "expenses":
    "drink1": 26,
    "drink2": 20

    ,

    "firstName": "John",
    "lastName": "Rambo",
    "expenses":
    "coffe": 10,
    "cake": 20


    ];

    for(var i=0; i<yourArray.length; i++)
    if(yourArray[i].expenses.coffe + yourArray[i].expenses.cake > 35 )
    expensesAbove35Arr.push(yourArray[i]);




    You have got your result inside array expensesAbove35Arr






    share|improve this answer



























      up vote
      0
      down vote













      I'm assuming that you need to keep you array of users the way it is, but with the expenses filtered.



      (I assumed wrong as pointed in the comment, keeping this answer just in case someone sees any value on it)



      Probably this can be optmized or simplified, but here it goes:



      arr.reduce((acc, user) => [...acc,
      Object.keys(user).reduce((userResult, key) =>
      if (key === 'expenses')
      return
      ...userResult,
      expenses: Object.entries(elem.expenses)
      .filter(([product, value]) => value > 35)
      // now "reversing" the object.entries
      .reduce((acc, [product, value]) => ( [product]: value ), )


      return
      ...userResult,
      [key]: elem[key]

      , user) // starts with the user
      ], ) //starts with empty array





      share|improve this answer


















      • 1




        The OP want's to get all user objects whose sum of the expenses is larger than N. They don't want to filter out expenses.
        – Felix Kling
        2 hours ago

















      up vote
      -2
      down vote













      A possible solution might be:



      arr.filter(function(v)
      for(expense in v.expenses)
      if(v.expenses[expense] > 10)
      return true;
      else
      return false;


      )






      share|improve this answer


















      • 1




        Why should the OP try this? How does this solve their problem?
        – Felix Kling
        2 hours ago






      • 1




        On the contrary OP's problem is getting to the "expenses" array, "How to get inside expenses", I don't think SO is a coding service
        – Velimir Tchatchevsky
        2 hours ago






      • 1




        I want iterate over all values inside expenses object
        – Kamil Staszewski
        2 hours ago






      • 3




        So, if they are asking how to sum all property values, you are showing how to access a single property, because actually answering the question would be a too much? O_o
        – Felix Kling
        2 hours ago






      • 1




        I agree. But if you only say "try this" then that seems to imply that you are providing a full fledged solution to their problem. But you are not. Your code example is somewhat related but doesn't produce the result they want. So at the very least you should explain what you are doing and how this helps them get to solving their problem.
        – Felix Kling
        2 hours ago











      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',
      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%2f53195799%2ffiltering-array-with-objects-inside-an-object%23new-answer', 'question_page');

      );

      Post as a guest






























      5 Answers
      5






      active

      oldest

      votes








      5 Answers
      5






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      9
      down vote













      Just filter it, with a condition using reduce to sum the expenses! Pretty straight forward :)






      const input = [
      "firstName": "Kevin",
      "lastName": "Smith",
      "expenses":
      "drink1": 26,
      "drink2": 20

      ,

      "firstName": "John",
      "lastName": "Rambo",
      "expenses":
      "coffe": 10,
      "cake": 20


      ];

      const output = input.filter(user => Object.values(user.expenses).reduce((acc, expense) => acc + expense) > 45);
      console.log(output);








      share|improve this answer




















      • We have a winner. Thank you!
        – Kamil Staszewski
        2 hours ago










      • Glad it helps ;) Don't forget to mark the answer as accepted so that it can help the rest of the community :)
        – sjahan
        2 hours ago










      • I didn't knew about Object.values. Nice!
        – Donny Verduijn
        2 hours ago














      up vote
      9
      down vote













      Just filter it, with a condition using reduce to sum the expenses! Pretty straight forward :)






      const input = [
      "firstName": "Kevin",
      "lastName": "Smith",
      "expenses":
      "drink1": 26,
      "drink2": 20

      ,

      "firstName": "John",
      "lastName": "Rambo",
      "expenses":
      "coffe": 10,
      "cake": 20


      ];

      const output = input.filter(user => Object.values(user.expenses).reduce((acc, expense) => acc + expense) > 45);
      console.log(output);








      share|improve this answer




















      • We have a winner. Thank you!
        – Kamil Staszewski
        2 hours ago










      • Glad it helps ;) Don't forget to mark the answer as accepted so that it can help the rest of the community :)
        – sjahan
        2 hours ago










      • I didn't knew about Object.values. Nice!
        – Donny Verduijn
        2 hours ago












      up vote
      9
      down vote










      up vote
      9
      down vote









      Just filter it, with a condition using reduce to sum the expenses! Pretty straight forward :)






      const input = [
      "firstName": "Kevin",
      "lastName": "Smith",
      "expenses":
      "drink1": 26,
      "drink2": 20

      ,

      "firstName": "John",
      "lastName": "Rambo",
      "expenses":
      "coffe": 10,
      "cake": 20


      ];

      const output = input.filter(user => Object.values(user.expenses).reduce((acc, expense) => acc + expense) > 45);
      console.log(output);








      share|improve this answer












      Just filter it, with a condition using reduce to sum the expenses! Pretty straight forward :)






      const input = [
      "firstName": "Kevin",
      "lastName": "Smith",
      "expenses":
      "drink1": 26,
      "drink2": 20

      ,

      "firstName": "John",
      "lastName": "Rambo",
      "expenses":
      "coffe": 10,
      "cake": 20


      ];

      const output = input.filter(user => Object.values(user.expenses).reduce((acc, expense) => acc + expense) > 45);
      console.log(output);








      const input = [
      "firstName": "Kevin",
      "lastName": "Smith",
      "expenses":
      "drink1": 26,
      "drink2": 20

      ,

      "firstName": "John",
      "lastName": "Rambo",
      "expenses":
      "coffe": 10,
      "cake": 20


      ];

      const output = input.filter(user => Object.values(user.expenses).reduce((acc, expense) => acc + expense) > 45);
      console.log(output);





      const input = [
      "firstName": "Kevin",
      "lastName": "Smith",
      "expenses":
      "drink1": 26,
      "drink2": 20

      ,

      "firstName": "John",
      "lastName": "Rambo",
      "expenses":
      "coffe": 10,
      "cake": 20


      ];

      const output = input.filter(user => Object.values(user.expenses).reduce((acc, expense) => acc + expense) > 45);
      console.log(output);






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered 2 hours ago









      sjahan

      2,8041722




      2,8041722











      • We have a winner. Thank you!
        – Kamil Staszewski
        2 hours ago










      • Glad it helps ;) Don't forget to mark the answer as accepted so that it can help the rest of the community :)
        – sjahan
        2 hours ago










      • I didn't knew about Object.values. Nice!
        – Donny Verduijn
        2 hours ago
















      • We have a winner. Thank you!
        – Kamil Staszewski
        2 hours ago










      • Glad it helps ;) Don't forget to mark the answer as accepted so that it can help the rest of the community :)
        – sjahan
        2 hours ago










      • I didn't knew about Object.values. Nice!
        – Donny Verduijn
        2 hours ago















      We have a winner. Thank you!
      – Kamil Staszewski
      2 hours ago




      We have a winner. Thank you!
      – Kamil Staszewski
      2 hours ago












      Glad it helps ;) Don't forget to mark the answer as accepted so that it can help the rest of the community :)
      – sjahan
      2 hours ago




      Glad it helps ;) Don't forget to mark the answer as accepted so that it can help the rest of the community :)
      – sjahan
      2 hours ago












      I didn't knew about Object.values. Nice!
      – Donny Verduijn
      2 hours ago




      I didn't knew about Object.values. Nice!
      – Donny Verduijn
      2 hours ago












      up vote
      0
      down vote













      You can try something like this






      var data = [
      "firstName": "Kevin",
      "lastName": "Smith",
      "expenses":
      "drink1": 25,
      "drink2": 20

      ,

      "firstName": "John",
      "lastName": "Rambo",
      "expenses":
      "coffe": 10,
      "cake": 20


      ]

      var filtered = data.filter(c => Object.keys(c.expenses)
      .reduce(function(p, e)
      return p + c.expenses[e]
      , 0) >= 35
      );

      console.log(filtered);








      share|improve this answer


























        up vote
        0
        down vote













        You can try something like this






        var data = [
        "firstName": "Kevin",
        "lastName": "Smith",
        "expenses":
        "drink1": 25,
        "drink2": 20

        ,

        "firstName": "John",
        "lastName": "Rambo",
        "expenses":
        "coffe": 10,
        "cake": 20


        ]

        var filtered = data.filter(c => Object.keys(c.expenses)
        .reduce(function(p, e)
        return p + c.expenses[e]
        , 0) >= 35
        );

        console.log(filtered);








        share|improve this answer
























          up vote
          0
          down vote










          up vote
          0
          down vote









          You can try something like this






          var data = [
          "firstName": "Kevin",
          "lastName": "Smith",
          "expenses":
          "drink1": 25,
          "drink2": 20

          ,

          "firstName": "John",
          "lastName": "Rambo",
          "expenses":
          "coffe": 10,
          "cake": 20


          ]

          var filtered = data.filter(c => Object.keys(c.expenses)
          .reduce(function(p, e)
          return p + c.expenses[e]
          , 0) >= 35
          );

          console.log(filtered);








          share|improve this answer














          You can try something like this






          var data = [
          "firstName": "Kevin",
          "lastName": "Smith",
          "expenses":
          "drink1": 25,
          "drink2": 20

          ,

          "firstName": "John",
          "lastName": "Rambo",
          "expenses":
          "coffe": 10,
          "cake": 20


          ]

          var filtered = data.filter(c => Object.keys(c.expenses)
          .reduce(function(p, e)
          return p + c.expenses[e]
          , 0) >= 35
          );

          console.log(filtered);








          var data = [
          "firstName": "Kevin",
          "lastName": "Smith",
          "expenses":
          "drink1": 25,
          "drink2": 20

          ,

          "firstName": "John",
          "lastName": "Rambo",
          "expenses":
          "coffe": 10,
          "cake": 20


          ]

          var filtered = data.filter(c => Object.keys(c.expenses)
          .reduce(function(p, e)
          return p + c.expenses[e]
          , 0) >= 35
          );

          console.log(filtered);





          var data = [
          "firstName": "Kevin",
          "lastName": "Smith",
          "expenses":
          "drink1": 25,
          "drink2": 20

          ,

          "firstName": "John",
          "lastName": "Rambo",
          "expenses":
          "coffe": 10,
          "cake": 20


          ]

          var filtered = data.filter(c => Object.keys(c.expenses)
          .reduce(function(p, e)
          return p + c.expenses[e]
          , 0) >= 35
          );

          console.log(filtered);






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 2 hours ago

























          answered 2 hours ago









          Alfredo A.

          81911232




          81911232




















              up vote
              0
              down vote













              You can access object at index i inside array arr with expression arr[i]
              What you need to do is to loop over your array. Inside your loop you access each object with expression i have mentioned: arr[i] and then on this object you can access expenses following way arr[i].expenses after this - if i am understanding correctly you sum contents of your arr[i].expenses object and select those objects which satisfy your condition.
              Please, See code below:



              var expensesAbove35Arr = ;

              var yourArray = [

              "firstName": "Kevin",
              "lastName": "Smith",
              "expenses":
              "drink1": 26,
              "drink2": 20

              ,

              "firstName": "John",
              "lastName": "Rambo",
              "expenses":
              "coffe": 10,
              "cake": 20


              ];

              for(var i=0; i<yourArray.length; i++)
              if(yourArray[i].expenses.coffe + yourArray[i].expenses.cake > 35 )
              expensesAbove35Arr.push(yourArray[i]);




              You have got your result inside array expensesAbove35Arr






              share|improve this answer
























                up vote
                0
                down vote













                You can access object at index i inside array arr with expression arr[i]
                What you need to do is to loop over your array. Inside your loop you access each object with expression i have mentioned: arr[i] and then on this object you can access expenses following way arr[i].expenses after this - if i am understanding correctly you sum contents of your arr[i].expenses object and select those objects which satisfy your condition.
                Please, See code below:



                var expensesAbove35Arr = ;

                var yourArray = [

                "firstName": "Kevin",
                "lastName": "Smith",
                "expenses":
                "drink1": 26,
                "drink2": 20

                ,

                "firstName": "John",
                "lastName": "Rambo",
                "expenses":
                "coffe": 10,
                "cake": 20


                ];

                for(var i=0; i<yourArray.length; i++)
                if(yourArray[i].expenses.coffe + yourArray[i].expenses.cake > 35 )
                expensesAbove35Arr.push(yourArray[i]);




                You have got your result inside array expensesAbove35Arr






                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  You can access object at index i inside array arr with expression arr[i]
                  What you need to do is to loop over your array. Inside your loop you access each object with expression i have mentioned: arr[i] and then on this object you can access expenses following way arr[i].expenses after this - if i am understanding correctly you sum contents of your arr[i].expenses object and select those objects which satisfy your condition.
                  Please, See code below:



                  var expensesAbove35Arr = ;

                  var yourArray = [

                  "firstName": "Kevin",
                  "lastName": "Smith",
                  "expenses":
                  "drink1": 26,
                  "drink2": 20

                  ,

                  "firstName": "John",
                  "lastName": "Rambo",
                  "expenses":
                  "coffe": 10,
                  "cake": 20


                  ];

                  for(var i=0; i<yourArray.length; i++)
                  if(yourArray[i].expenses.coffe + yourArray[i].expenses.cake > 35 )
                  expensesAbove35Arr.push(yourArray[i]);




                  You have got your result inside array expensesAbove35Arr






                  share|improve this answer












                  You can access object at index i inside array arr with expression arr[i]
                  What you need to do is to loop over your array. Inside your loop you access each object with expression i have mentioned: arr[i] and then on this object you can access expenses following way arr[i].expenses after this - if i am understanding correctly you sum contents of your arr[i].expenses object and select those objects which satisfy your condition.
                  Please, See code below:



                  var expensesAbove35Arr = ;

                  var yourArray = [

                  "firstName": "Kevin",
                  "lastName": "Smith",
                  "expenses":
                  "drink1": 26,
                  "drink2": 20

                  ,

                  "firstName": "John",
                  "lastName": "Rambo",
                  "expenses":
                  "coffe": 10,
                  "cake": 20


                  ];

                  for(var i=0; i<yourArray.length; i++)
                  if(yourArray[i].expenses.coffe + yourArray[i].expenses.cake > 35 )
                  expensesAbove35Arr.push(yourArray[i]);




                  You have got your result inside array expensesAbove35Arr







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 2 hours ago









                  Tornike Shavishvili

                  5331922




                  5331922




















                      up vote
                      0
                      down vote













                      I'm assuming that you need to keep you array of users the way it is, but with the expenses filtered.



                      (I assumed wrong as pointed in the comment, keeping this answer just in case someone sees any value on it)



                      Probably this can be optmized or simplified, but here it goes:



                      arr.reduce((acc, user) => [...acc,
                      Object.keys(user).reduce((userResult, key) =>
                      if (key === 'expenses')
                      return
                      ...userResult,
                      expenses: Object.entries(elem.expenses)
                      .filter(([product, value]) => value > 35)
                      // now "reversing" the object.entries
                      .reduce((acc, [product, value]) => ( [product]: value ), )


                      return
                      ...userResult,
                      [key]: elem[key]

                      , user) // starts with the user
                      ], ) //starts with empty array





                      share|improve this answer


















                      • 1




                        The OP want's to get all user objects whose sum of the expenses is larger than N. They don't want to filter out expenses.
                        – Felix Kling
                        2 hours ago














                      up vote
                      0
                      down vote













                      I'm assuming that you need to keep you array of users the way it is, but with the expenses filtered.



                      (I assumed wrong as pointed in the comment, keeping this answer just in case someone sees any value on it)



                      Probably this can be optmized or simplified, but here it goes:



                      arr.reduce((acc, user) => [...acc,
                      Object.keys(user).reduce((userResult, key) =>
                      if (key === 'expenses')
                      return
                      ...userResult,
                      expenses: Object.entries(elem.expenses)
                      .filter(([product, value]) => value > 35)
                      // now "reversing" the object.entries
                      .reduce((acc, [product, value]) => ( [product]: value ), )


                      return
                      ...userResult,
                      [key]: elem[key]

                      , user) // starts with the user
                      ], ) //starts with empty array





                      share|improve this answer


















                      • 1




                        The OP want's to get all user objects whose sum of the expenses is larger than N. They don't want to filter out expenses.
                        – Felix Kling
                        2 hours ago












                      up vote
                      0
                      down vote










                      up vote
                      0
                      down vote









                      I'm assuming that you need to keep you array of users the way it is, but with the expenses filtered.



                      (I assumed wrong as pointed in the comment, keeping this answer just in case someone sees any value on it)



                      Probably this can be optmized or simplified, but here it goes:



                      arr.reduce((acc, user) => [...acc,
                      Object.keys(user).reduce((userResult, key) =>
                      if (key === 'expenses')
                      return
                      ...userResult,
                      expenses: Object.entries(elem.expenses)
                      .filter(([product, value]) => value > 35)
                      // now "reversing" the object.entries
                      .reduce((acc, [product, value]) => ( [product]: value ), )


                      return
                      ...userResult,
                      [key]: elem[key]

                      , user) // starts with the user
                      ], ) //starts with empty array





                      share|improve this answer














                      I'm assuming that you need to keep you array of users the way it is, but with the expenses filtered.



                      (I assumed wrong as pointed in the comment, keeping this answer just in case someone sees any value on it)



                      Probably this can be optmized or simplified, but here it goes:



                      arr.reduce((acc, user) => [...acc,
                      Object.keys(user).reduce((userResult, key) =>
                      if (key === 'expenses')
                      return
                      ...userResult,
                      expenses: Object.entries(elem.expenses)
                      .filter(([product, value]) => value > 35)
                      // now "reversing" the object.entries
                      .reduce((acc, [product, value]) => ( [product]: value ), )


                      return
                      ...userResult,
                      [key]: elem[key]

                      , user) // starts with the user
                      ], ) //starts with empty array






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited 2 hours ago

























                      answered 2 hours ago









                      graciano

                      1289




                      1289







                      • 1




                        The OP want's to get all user objects whose sum of the expenses is larger than N. They don't want to filter out expenses.
                        – Felix Kling
                        2 hours ago












                      • 1




                        The OP want's to get all user objects whose sum of the expenses is larger than N. They don't want to filter out expenses.
                        – Felix Kling
                        2 hours ago







                      1




                      1




                      The OP want's to get all user objects whose sum of the expenses is larger than N. They don't want to filter out expenses.
                      – Felix Kling
                      2 hours ago




                      The OP want's to get all user objects whose sum of the expenses is larger than N. They don't want to filter out expenses.
                      – Felix Kling
                      2 hours ago










                      up vote
                      -2
                      down vote













                      A possible solution might be:



                      arr.filter(function(v)
                      for(expense in v.expenses)
                      if(v.expenses[expense] > 10)
                      return true;
                      else
                      return false;


                      )






                      share|improve this answer


















                      • 1




                        Why should the OP try this? How does this solve their problem?
                        – Felix Kling
                        2 hours ago






                      • 1




                        On the contrary OP's problem is getting to the "expenses" array, "How to get inside expenses", I don't think SO is a coding service
                        – Velimir Tchatchevsky
                        2 hours ago






                      • 1




                        I want iterate over all values inside expenses object
                        – Kamil Staszewski
                        2 hours ago






                      • 3




                        So, if they are asking how to sum all property values, you are showing how to access a single property, because actually answering the question would be a too much? O_o
                        – Felix Kling
                        2 hours ago






                      • 1




                        I agree. But if you only say "try this" then that seems to imply that you are providing a full fledged solution to their problem. But you are not. Your code example is somewhat related but doesn't produce the result they want. So at the very least you should explain what you are doing and how this helps them get to solving their problem.
                        – Felix Kling
                        2 hours ago















                      up vote
                      -2
                      down vote













                      A possible solution might be:



                      arr.filter(function(v)
                      for(expense in v.expenses)
                      if(v.expenses[expense] > 10)
                      return true;
                      else
                      return false;


                      )






                      share|improve this answer


















                      • 1




                        Why should the OP try this? How does this solve their problem?
                        – Felix Kling
                        2 hours ago






                      • 1




                        On the contrary OP's problem is getting to the "expenses" array, "How to get inside expenses", I don't think SO is a coding service
                        – Velimir Tchatchevsky
                        2 hours ago






                      • 1




                        I want iterate over all values inside expenses object
                        – Kamil Staszewski
                        2 hours ago






                      • 3




                        So, if they are asking how to sum all property values, you are showing how to access a single property, because actually answering the question would be a too much? O_o
                        – Felix Kling
                        2 hours ago






                      • 1




                        I agree. But if you only say "try this" then that seems to imply that you are providing a full fledged solution to their problem. But you are not. Your code example is somewhat related but doesn't produce the result they want. So at the very least you should explain what you are doing and how this helps them get to solving their problem.
                        – Felix Kling
                        2 hours ago













                      up vote
                      -2
                      down vote










                      up vote
                      -2
                      down vote









                      A possible solution might be:



                      arr.filter(function(v)
                      for(expense in v.expenses)
                      if(v.expenses[expense] > 10)
                      return true;
                      else
                      return false;


                      )






                      share|improve this answer














                      A possible solution might be:



                      arr.filter(function(v)
                      for(expense in v.expenses)
                      if(v.expenses[expense] > 10)
                      return true;
                      else
                      return false;


                      )







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited 2 hours ago

























                      answered 2 hours ago









                      Velimir Tchatchevsky

                      1,91311117




                      1,91311117







                      • 1




                        Why should the OP try this? How does this solve their problem?
                        – Felix Kling
                        2 hours ago






                      • 1




                        On the contrary OP's problem is getting to the "expenses" array, "How to get inside expenses", I don't think SO is a coding service
                        – Velimir Tchatchevsky
                        2 hours ago






                      • 1




                        I want iterate over all values inside expenses object
                        – Kamil Staszewski
                        2 hours ago






                      • 3




                        So, if they are asking how to sum all property values, you are showing how to access a single property, because actually answering the question would be a too much? O_o
                        – Felix Kling
                        2 hours ago






                      • 1




                        I agree. But if you only say "try this" then that seems to imply that you are providing a full fledged solution to their problem. But you are not. Your code example is somewhat related but doesn't produce the result they want. So at the very least you should explain what you are doing and how this helps them get to solving their problem.
                        – Felix Kling
                        2 hours ago













                      • 1




                        Why should the OP try this? How does this solve their problem?
                        – Felix Kling
                        2 hours ago






                      • 1




                        On the contrary OP's problem is getting to the "expenses" array, "How to get inside expenses", I don't think SO is a coding service
                        – Velimir Tchatchevsky
                        2 hours ago






                      • 1




                        I want iterate over all values inside expenses object
                        – Kamil Staszewski
                        2 hours ago






                      • 3




                        So, if they are asking how to sum all property values, you are showing how to access a single property, because actually answering the question would be a too much? O_o
                        – Felix Kling
                        2 hours ago






                      • 1




                        I agree. But if you only say "try this" then that seems to imply that you are providing a full fledged solution to their problem. But you are not. Your code example is somewhat related but doesn't produce the result they want. So at the very least you should explain what you are doing and how this helps them get to solving their problem.
                        – Felix Kling
                        2 hours ago








                      1




                      1




                      Why should the OP try this? How does this solve their problem?
                      – Felix Kling
                      2 hours ago




                      Why should the OP try this? How does this solve their problem?
                      – Felix Kling
                      2 hours ago




                      1




                      1




                      On the contrary OP's problem is getting to the "expenses" array, "How to get inside expenses", I don't think SO is a coding service
                      – Velimir Tchatchevsky
                      2 hours ago




                      On the contrary OP's problem is getting to the "expenses" array, "How to get inside expenses", I don't think SO is a coding service
                      – Velimir Tchatchevsky
                      2 hours ago




                      1




                      1




                      I want iterate over all values inside expenses object
                      – Kamil Staszewski
                      2 hours ago




                      I want iterate over all values inside expenses object
                      – Kamil Staszewski
                      2 hours ago




                      3




                      3




                      So, if they are asking how to sum all property values, you are showing how to access a single property, because actually answering the question would be a too much? O_o
                      – Felix Kling
                      2 hours ago




                      So, if they are asking how to sum all property values, you are showing how to access a single property, because actually answering the question would be a too much? O_o
                      – Felix Kling
                      2 hours ago




                      1




                      1




                      I agree. But if you only say "try this" then that seems to imply that you are providing a full fledged solution to their problem. But you are not. Your code example is somewhat related but doesn't produce the result they want. So at the very least you should explain what you are doing and how this helps them get to solving their problem.
                      – Felix Kling
                      2 hours ago





                      I agree. But if you only say "try this" then that seems to imply that you are providing a full fledged solution to their problem. But you are not. Your code example is somewhat related but doesn't produce the result they want. So at the very least you should explain what you are doing and how this helps them get to solving their problem.
                      – Felix Kling
                      2 hours ago


















                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53195799%2ffiltering-array-with-objects-inside-an-object%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Displaying single band from multi-band raster using QGIS

                      How many registers does an x86_64 CPU actually have?