0

I want to remove arrays cctype, cctypologycode, and amount when they are empty from array. best way to do it ?

{
  "ccInput": [
    {
      "designSummaryId": 6,
      "CCType": "A",
      "CCTypologyCode": "A",
      "Amount": "1"
    },
    {
      "designSummaryId": 7,
      "CCType": "",
      "CCTypologyCode": "",
      "Amount": ""
    },
  ]
}

ccInput[1] should be removed from the array

1

2 Answers 2

2

If you want to remove some objects from your array, use Array.prototytpe.filter().

You can do it in an immutable way by copying each property of your object using the spread operator and then filter the ccInput property:

const obj = {ccInput:[{designSummaryId:6,CCType:"A",CCTypologyCode:"A",Amount:"1"},{designSummaryId:7,CCType:"",CCTypologyCode:"",Amount:""}]};

const result = { ...obj, ccInput: obj.ccInput.filter(x => x.CCType && x.CCTypologyCode) };

console.log(result);

Or if you want to modify your object in place, simply reassign the ccInput property:

const obj = {ccInput:[{designSummaryId:6,CCType:"A",CCTypologyCode:"A",Amount:"1"},{designSummaryId:7,CCType:"",CCTypologyCode:"",Amount:""}]};

obj.ccInput = obj.ccInput.filter(x => x.CCType && x.CCTypologyCode);

console.log(obj);

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

Comments

0

I don't know much about TypeScript, but I do know you can set the array to []. However, for those of you looking to get rid of only part of an array but not all of it, the simplest way to do so that I know of is to just set each value to 0 for numbers, '' for characters, "" for Strings, etc.

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.