0

Assumed that is the JSON structure:

var myData = [
  {
    "id": 68,
    "country": "US",
  },
  {
    "id": 82,
    "country": "PL",
  },
  {
    "id": 83,
    "country": "US",
  }
];

I want to get all items, where country == US

Following try does not work:

var myResult = _.where (myData, {'country': 'US'});

I get an empty result > myResult []

What is the mistake?

EDIT: Sorry, the use of lodash and underscore together was the problem !

6
  • Take a look at this topic: stackoverflow.com/questions/23720988/… Commented Feb 15, 2016 at 0:28
  • I am looking for a underscore solution, other libraries are out of the scope,.. Commented Feb 15, 2016 at 0:30
  • Can you use a native ES5 JavaScript solution (filter)? Commented Feb 15, 2016 at 0:36
  • There is nothing wrong with the code posted in the OP, see this: jsfiddle.net/cyqzan1a. Something else is wrong here. Post more code. Commented Feb 15, 2016 at 0:37
  • The only thing that may be wrong here is that the OP is actually using the latest version of lodash (v4.3.0) which is sometimes confused with underscore. _.where does not exists in that version of lodash, but does in previous versions. Commented Feb 15, 2016 at 0:42

1 Answer 1

2

I never used underscore.js before, but I have tried your code and it works totally.

Please make sure that you are importing the library.

I have used the next code in the body tag of an empty HTML file:

<script src="http://underscorejs.org/underscore-min.js"></script>
<script>
    var myData = [
      {
        "id": 68,
        "country": "US",
      },
      {
        "id": 82,
        "country": "PL",
      },
      {
        "id": 83,
        "country": "US",
      }
    ];

    var myResult = _.where (myData, {'country': 'US'});
    console.log(myResult);
</script>

And the result is:

Console result

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

Comments

Your Answer

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