0

Let's imagine I have the following JSON object:

[
    {
        "name": "productA",
        "prices": [
            {
                "currency": "USD",
                "price": 10.0
            },
            {
                "currency": "EUR",
                "price": 9.0
            }
        ]
    },
    {
        "name": "productB",
        "prices": [
            {
                "currency": "GBP",
                "price": 18.0
            },
            {
                "currency": "EUR",
                "price": 20.0
            }
        ]
    },
    ...
]

I'd like to use Lodash to check if there's any object having the price with "GBP" currency, so I wrote the following:

_.some(products, ['prices.currency', 'GBP'])

However, it always returns false.

My guess is that it is not able to get the prices.currency property since prices is an array of objects, so Lodash doesn't know which of the objects to check. I know I could do something like prices[0].currency, but it would work only in this specific case, where GBP is the first price.

Is there a sort of "wildcard" to say "any of the array items" (like prices[x].currency), or I need to extract the inner objects first and then use _.some(xxx)?

4
  • Do you have to use lodash? You could achieve this easily in standard Javascript Commented Jul 26, 2018 at 9:05
  • No, I don't need to, but I'm using it to write some tests in Postman and I'd like to use it to keep the "style" used for other tests. However if that's not possible/not readable, I can switch back to vanilla JS Commented Jul 26, 2018 at 9:07
  • JSON is a textual notation for data exchange. (More here.) If you're dealing with JavaScript source code, and not dealing with a string, you're not dealing with JSON. Commented Jul 26, 2018 at 9:08
  • @T.J.Crowder yes, ok, it was a notation abuse. I'm dealing with a JS object corresponding to the JSON I posted Commented Jul 26, 2018 at 9:10

1 Answer 1

3

You could do it without lodash using too Array#some :

let result = data.some(e => e.prices.some(p => p.currency == 'GBP'));
console.log(result);

Demo:

const data = [
    {
        "name": "productA",
        "prices": [
            {
                "currency": "USD",
                "price": 10.0
            },
            {
                "currency": "EUR",
                "price": 9.0
            }
        ]
    },
    {
        "name": "productB",
        "prices": [
            {
                "currency": "GBP",
                "price": 18.0
            },
            {
                "currency": "EUR",
                "price": 20.0
            }
        ]
    }
];

let result = data.some(e => e.prices.some(p => p.currency == 'GBP'));
console.log(result);

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

1 Comment

Exactly how I'd do it.

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.