0

I have my html like

 <input type="text" class="form-control PaperSupply">` 
 <input type="text" class="form-control PaperSupply">`
 <input type="text" class="form-control PaperConsum">
 <input type="text" class="form-control PC">

multiple inputs for same value papersupply and PaperConsum, in javascript i am creating an array for same value using map function like this

 var paperSupply = $('.PaperSupply').map(function(){return $(this).val();}).get(); 
 var PaperConsum= $('.PaperConsum').map(function(){return $(this).val();}).get(); 

now in json i want if any element of these array is null remove that element from array.

for example:

{"paperSupply":["","2","3","","5"],"paperConsum":["1","","4","5","6"]}

if this is json from above code, then i want above json like this,

{"paperSupply":["2","3","5"],"paperConsum":["","4","6"]}

if any index of paperSupply is null it should be null then same index for second array should also be remove .

this is demo fiddle DEMO

Smern
19.1k22 gold badges77 silver badges93 bronze badges
asked May 9, 2015 at 9:57

2 Answers 2

2

You can return undefined from .map() to ignore the empty values.

Within the callback function, this refers to the current DOM element for each iteration. The function can return an individual data item or an array of data items to be inserted into the resulting set. If an array is returned, the elements inside the array are inserted into the set. If the function returns null or undefined, no element will be inserted.

var paperSupply = $('.PaperSupply').map(function () {
 return $(this).val().trim() || undefined; //use trim only if you want to discard inputs with space only
}).get();

Demo: Fiddle


Update

var PaperConsum = [],
 $PaperConsums = $('.PaperConsum');
var paperSupply = $('.PaperSupply').map(function (i, el) {
 var value = $(this).val().trim(); //use trim only if you want to discard inputs with space only
 if (value) {
 PaperConsum.push($PaperConsums.eq(i).val() || '');
 return value;
 }
}).get();
console.log(paperSupply, PaperConsum)

Demo: Fiddle

answered May 9, 2015 at 9:59
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for answer, but you are only removing element from array, not removing second array element from same index. if paperSupply[0] is null then paperConsum[0] should also be null
0

You can add filter:

$('.PaperSupply').map(function(){
 return $(this).val().trim();
}).get().filter(Boolean);
answered May 9, 2015 at 10:00

1 Comment

thanks for answer, but you are only removing element from array, not removing second array element from same index. if paperSupply[0] is null then paperConsum[0] should also be null

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.