0

I'm trying to filtering multidimensional object using lodash filter object . Below are my sample object to filter data.

    {
  "0": {
    "details": {
      "rating": 2.5,
      "amenities": {
        "airConditioning": true,
      },
    },
    "rates": {
      "packages": [
        {
          "refundable": "Yes",
        }
      ]
    }
  },
  {
  "1": {
    "details": {
      "rating": 3,
      "amenities": {
        "airConditioning": false,
      },
    },
    "rates": {
      "packages": [
        {
          "refundable": "Yes",
        }
      ]
    }
  },
  {
  "2": {
    "details": {
      "rating": 2,
      "amenities": {
        "airConditioning": true,
      },
    },
    "rates": {
      "packages": [
        {
          "refundable": "No",
        }
      ]
    }
  },
}

I have tried

    console.log(_.filter( data, { 'details.rating': '2', 'details.rating': '3' } ));

_.filter(data, function (item) {
  return ['2', '3'].indexOf(item.details.rating) >= 0
})

And followed this too Filtering multiple value with multiple key in json array using lodash

nothing working

1 Answer 1

1

Your JSON has extra { before the key "1" and "2". So remove that and it will work.

    var data = {
      "0": {
        "details": {
          "rating": 2.5,
          "amenities": {
            "airConditioning": true,
          },
        },
        "rates": {
          "packages": [
            {
              "refundable": "Yes",
            }
          ]
        }
      },
      
      "1": {
        "details": {
          "rating": 3,
          "amenities": {
            "airConditioning": false,
          },
        },
        "rates": {
          "packages": [
            {
              "refundable": "Yes",
            }
          ]
        }
      },
      "2": {
        "details": {
          "rating": 2,
          "amenities": {
            "airConditioning": true,
          },
        },
        "rates": {
          "packages": [
            {
              "refundable": "No",
            }
          ]
        }
      }
    };
    var result = _.filter(data, (item) => {
      return [2, 3].indexOf(item.details.rating) >= 0
    });
    console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

Notice the array elements inside the filter [2, 3]. They are integer type as item.details.rating is an integer.

Sign up to request clarification or add additional context in comments.

3 Comments

The first levela array ratings filtering is working , but the amenities sub object filtering is not working.I m trying to filter amenities airconditioing true set array of obejct i have used _some(item.details.amentities,{airConditioning:true}) ..but its return false
I did not understand what you are saying
I mean i'm trying to filter this attribute " item.details.amenities " => {airConditioning:true}.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.