1

I have a ASP.Net hidden field which has data in JSON format as shown below

[
  {
    "RegionName": "USA",
    "Contact": {
      "LegalName": "somethinglegal",
      "StreetAddress": "hello",
      "City": "Test",
      "State": "Test",
      "Zip": "8888",
      "Country": "USA",
      "VAT": "VAT"
    },
    "EntityContact": {
      "LegalName": "Test",
      "Email": "[email protected]",
      "Phone": "9998887777"
    }
  },
  {
    "RegionName": "Mexico",
    "Contact": {
      "LegalName": "somethinglegal",
      "StreetAddress": "hello",
      "City": "Test",
      "State": "Test",
      "Zip": "33333",
      "Country": "Mexico",
      "VAT": "VAT"
    },
    "EntityContact": {
      "LegalName": "Amex",
      "Email": "[email protected]",
      "Phone": "9998887777"
    }
  }  
]

which is read in Javascript using below code

var value = $('#countryInvoice')[0].defaultValue;

Now I want to search this JSON using javascript based on Region name and delete the record from the hidden field. So I want to remove the data point for say USA so only the below remains

[    
      {
        "RegionName": "Mexico",
        "Contact": {
          "LegalName": "somethinglegal",
          "StreetAddress": "hello",
          "City": "Test",
          "State": "Test",
          "Zip": "33333",
          "Country": "Mexico",
          "VAT": "VAT"
        },
        "EntityContact": {
          "LegalName": "Amex",
          "Email": "[email protected]",
          "Phone": "9998887777"
        }
      }  
    ]

Can someone please tell me how to do it in JQuery or Javascript.

Thanks

3
  • 3
    Take a look at filter(). Commented Nov 6, 2019 at 1:28
  • var result = yourArray.filter(function(o){ return o.RegionName === 'Mexico'; }); or const result = yourArray.filter(o => o.regionName === 'Mexico');. The later is less backward compatible. Commented Nov 6, 2019 at 1:35
  • @StackSlave - The filter condition should be o.regionName !== "USA", not o.regionName === 'Mexico'. Your code ignores including other elements which aren't USA but also not Mexico. Commented Nov 6, 2019 at 14:17

2 Answers 2

2
//ES5
var res = value.filter(function(e) { return e["RegionName"] != "USA"; })

//ES6
var res = value.filter(e => e["RegionName"] != "USA")

Note: The Arrow function is ES6 syntax.

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

Comments

0

You just need to filter it out using Array.prototype.filter().

Check out the MDN Docs here

The snippet below filters out USA

let value = [{
    "RegionName": "USA",
    "Contact": {
      "LegalName": "somethinglegal",
      "StreetAddress": "hello",
      "City": "Test",
      "State": "Test",
      "Zip": "8888",
      "Country": "USA",
      "VAT": "VAT"
    },
    "EntityContact": {
      "LegalName": "Test",
      "Email": "[email protected]",
      "Phone": "9998887777"
    }
  },
  {
    "RegionName": "Mexico",
    "Contact": {
      "LegalName": "somethinglegal",
      "StreetAddress": "hello",
      "City": "Test",
      "State": "Test",
      "Zip": "33333",
      "Country": "Mexico",
      "VAT": "VAT"
    },
    "EntityContact": {
      "LegalName": "Amex",
      "Email": "[email protected]",
      "Phone": "9998887777"
    }
  }
];

let newArray = value.filter(arr => arr.RegionName !== 'USA');
console.log(newArray);

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.