-2

I'm using jQuery.serializeJSON (https://github.com/marioizquierdo/jquery.serializeJSON). Basically, I have a form like:

<input name='store[products][][type]' type='text' value='TableSaw' />
<textarea name='store[products][][description]'></textarea>
<textarea name='store[products][][price]'></textarea>

Which when serialized looks like:

{"store":{"products":[{"type":"TableSaw","description":"Really cool saw, should buy it ","price": "20.99"}]}}

The user can change/delete/ description, price, and other attributes.Basically, what I want to do is test if type is the only key present.

Right now it looks like:

{"store":{"products":[{"type":"TableSaw"}]}} //JUST TYPE IS SENT TO DB. NOT WHAT I WANT

But what I'm trying to achieve is:

{"store":{"products":[]}} //WHAT I WANT TO SEND TO DB.

if only type is set, and nothing else.

the_
  • 1,183
  • 2
  • 30
  • 61
  • There is magic way to do this. You need a simple `if` statement. You've given us the pseudo code for it, so just implement that in real code. – user229044 Jul 27 '14 at 19:15
  • what are u using in backend ?? or u can use if statement incase u r using ajax – bhushya Jul 27 '14 at 19:16
  • @meagar, I guess I didn't make my question clear enough. It'd be simple if I was just going to do something to just see if `description && price` were null, but I have dynamic keys (for example, like Color and Size, like I mentioned in my question). Is there a way to delete if ONLY type is set, and all other key/values are blank? – the_ Jul 27 '14 at 19:27
  • Then, you're just asking how to get the keys for an object, so you can test whether `type` is the only key present. See the duplicate. – user229044 Jul 27 '14 at 19:38
  • @meager, Thanks for the reference, but does this question really count as a duplicate? I'm not asking "how to get the keys for an object", I'm asking "how to loop through an object and test for if it's the only key present". I edited the title to make this question better. – the_ Jul 27 '14 at 19:47
  • @meager, I've edited the question content as well now. – the_ Jul 27 '14 at 19:55

2 Answers2

1

try this:

for (var i=0; i<store.products.length;i++) {
     if (!isValid(store.products[i])) {
          delete store.products[i];
     }
}

function isValid(product) {
   return (product.type && product.description && product.price);
}
Jonathan Crowe
  • 5,793
  • 1
  • 18
  • 28
0

You can do this when you assign the json definition to an object like this

var data = {"store":{"products":[{"type":"TableSaw","description":"Really cool saw, should buy it ","price": "20.99"}]}};

All you need to do is to test the type of the description and the price elements if they don't exist you make the array products empty like this :

if( typeof(data.store.products[0].desctiption) == 'undefined' || typeof(data.store.products[0].price) == 'undefined')
    data.store.products = [];
Khalid
  • 4,730
  • 5
  • 27
  • 50
  • Thanks for the answer. I was really unclear on my question, I've now edited to be more clear. Basically, I am trying to test if type is the ONLY key/value set in the object, and if it is, then delete the entire object. – the_ Jul 27 '14 at 19:59