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?

bier hier
  • 20,970
  • 42
  • 97
  • 166
  • Possible duplicate of [How to use lodash to find and return an object from Array?](http://stackoverflow.com/questions/31054021/how-to-use-lodash-to-find-and-return-an-object-from-array) – Rajesh Mar 06 '17 at 08:11

3 Answers3

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.

Adam Boduch
  • 11,023
  • 3
  • 30
  • 38
0

You could do something like this

_.filter(data.existingPackage.primaryBundle.products, 
        function(o) { 
            return o.swappableProducts !== undefined && o.id==="105"; 
        });
remborg
  • 528
  • 3
  • 14
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>
Jonathan Portorreal
  • 2,730
  • 4
  • 21
  • 38