0

In my angular 4 application I have the following array :

let sampleArray1 = [
  {
    "name":"Raman",
    "prdList":[
      {
        "p_code":"20",
        "crtList":[
          {
            "c_code":"087"
          }
        ]
      }
    ]
  },
  {
    "name":"Laxman",
    "prdList":[
      {
        "p_code":"10"
      }
    ]
  },
  {
    "name":"raj",
    "prdList":[
      {
        "p_code":"202"
      }
    ]
  },
  {
    "name":"raghav",
    "prdList":[
      {
        "p_code":"30",
        "crtList":[
          {
            "c_code":"97"
          }
        ]
      }
    ]
  }
]

In this array some of the objects are missing crtList. I need to filter out all such objects and need to have the array which must and should has crtList.

So my result sampleArray2 should have the following result :

 [
      {
        "name":"Raman",
        "prdList":[
          {
            "p_code":"20",
            "crtList":[
              {
                "c_code":"087"
              }
            ]
          }
        ]
      },
      {
        "name":"raghav",
        "prdList":[
          {
            "p_code":"30",
            "crtList":[
              {
                "c_code":"97"
              }
            ]
          }
        ]

} ]

How can I achieve this using lodash?

2
  • 1
    If you want to be able to do this sort of manipulation on your own, without asking for help, learn how to here. Commented Jul 3, 2017 at 10:09
  • @evolutionxbox Very useful link. I learnt data transformation and functional JS programming myself from the same place. Commented Jul 3, 2017 at 10:17

2 Answers 2

1

Actually, to get sampleArray2, you don't need lodash. A simple Array.prototype.filter is enough:

let sampleArray1 = [
  {
    "name":"Raman",
    "prdList":[
      {
        "p_code":"20",
        "crtList":[
          {
            "c_code":"087"
          }
        ]
      }
    ]
  },
  {
    "name":"Laxman",
    "prdList":[
      {
        "p_code":"10"
      }
    ]
  },
  {
    "name":"raj",
    "prdList":[
      {
        "p_code":"202"
      }
    ]
  },
  {
    "name":"raghav",
    "prdList":[
      {
        "p_code":"30",
        "crtList":[
          {
            "c_code":"97"
          }
        ]
      }
    ]
  }
];

let result = sampleArray1.filter(function(x) {
  return hasCrtList(x.prdList);
});

function hasCrtList(prdList) {
  for (var i in prdList) {
    if (prdList[i].crtList) {
      return true;
    }
  }
  return false;
}

console.log(result);

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

Comments

0

I know it's a bit too late, but I think it's useful for other developers. You can achieve this easily in just one line of code using Lodash.

_.differenceWith(fullArrayOfObjects, subsetArrayOfObjects, _.isEqual)

This function will return missing array of objects that is not in subsetArrayOfObjects. Hope it helps.

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.