1

I have some json and would like to return some nested objects, this is the json:

{
  "existingPackage": {
    "primaryBundle": {
      "id": "2031",
      "serviceId": "114297251",
      "name": "TV - Entertainment, Drama, Movies",
      "products": [
        {
          "name": "Entertainment",
          "id": "100",
          "price": 2600,
          "gifted": false
        },
        {
          "name": "Drama",
          "id": "104",
          "price": 2000,
          "gifted": false,
          "swappableProducts": [
            {
              "name": "Sport",
              "id": "107",
              "price": 2500,
              "gifted": false
            }
          ]
        },
        {
          "name": "Movies",
          "id": "105",
          "price": 2000,
          "gifted": false,
          "swappableProducts": [
            {
              "name": "Sport",
              "id": "107",
              "price": 2500,
              "gifted": false
            }
          ]
        }
      ]
    }
  }
}

The goal is to return only items from the productsarray which have the swappableProducts property and have a certain id. So for example when I an productId = 105 then I would like to return:

{
          "name": "Movies",
          "id": "105",
          "price": 2000,
          "gifted": false,
          "swappableProducts": [
            {
              "name": "Sport",
              "id": "107",
              "price": 2500,
              "gifted": false
            }
          ]
        }
}

How can I return this with lodash?

1

3 Answers 3

1

Here's my approach:

_.filter(
  products,
  i => _.every([
    _.has(i, 'swappableProducts'),
    _.eq(_.result(i, 'id'), '105')
  ])
);

The idea is to pass both predicates to every(). The first uses has() to check if the swappableProducts property exists. The second predicate combines eq() and result() to check the id value.

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

Comments

0

You could do something like this

_.filter(data.existingPackage.primaryBundle.products, 
        function(o) { 
            return o.swappableProducts !== undefined && o.id==="105"; 
        });

Comments

0

Here's a function that returns desired result

const getProducts = obj => obj.existingPackage.primaryBundle.products;

const withSwappable = _.curry((myId, obj) => _.result(obj, 'swappableProducts[0].id', false) == myId)

function getProductsWithId(data, myId) {
  return _.filter(getProducts(data), withSwappable(myId))
}

const data = {
  "existingPackage": {
    "primaryBundle": {
      "id": "2031",
      "serviceId": "114297251",
      "name": "TV - Entertainment, Drama, Movies",
      "products": [{
          "name": "Entertainment",
          "id": "100",
          "price": 2600,
          "gifted": false
        },
        {
          "name": "Drama",
          "id": "104",
          "price": 2000,
          "gifted": false,
          "swappableProducts": [{
            "name": "Sport",
            "id": "107",
            "price": 2500,
            "gifted": false
          }]
        },
        {
          "name": "Movies",
          "id": "105",
          "price": 2000,
          "gifted": false,
          "swappableProducts": [{
            "name": "Sport",
            "id": "107",
            "price": 2500,
            "gifted": false
          }]
        }
      ]
    }
  }
}


console.log(getProductsWithId(data, "107"));
console.log(getProductsWithId(data, "100"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>

Comments

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.