i have an object with some key:value pairs. I would like to push the object into an array but without the empty object value.
my fiddle: https://jsfiddle.net/howw1fj7/
var myData = [];
var myObj = {
product: "phone",
quantity: 100,
color: "red",
secondColor: '',
imei: "43904325"
};
myData.push(myObj); //push only not empty key:values
$('pre').html(JSON.stringify(myData, null, 4));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<pre></pre>
i have tried something like this but this is not correct:
$.each(myObj, function(key, value) {
if(value.length !=0) {
var myNewObj = {key:value};
myData.push(myNewObj);
}
});