1

[ { "stockId":2,"vendorId":1,"vendorCode":"Aya - 01","price":2100 }, null, null ]

remove the null array from arraylist

asked Feb 15, 2022 at 13:10
4
  • Welcome to Stack Overflow! You are encouraged to make an attempt to write your code. If you encounter a specific technical problem during that attempt, such as an error or unexpected result, we can help with that. Please provide specific information about that attempt and what didn't work as expected. To learn more about this community and how we can help you, please start with the tour and read How to Ask and its linked resources. Commented Feb 15, 2022 at 13:12
  • 4
    Hint... The .filter() method on arrays in JavaScript can be used to filter the elements of an array. Commented Feb 15, 2022 at 13:13
  • array.filter(Boolean) Commented Feb 15, 2022 at 13:13
  • Does this answer your question? Filter null from an array in JavaScript Commented Feb 15, 2022 at 13:33

4 Answers 4

1

More simple and concise solution:

let newArray = array.filter(Boolean);

just pass javascript Boolean (builtin) function inside filter as callback function.

answered Feb 15, 2022 at 14:48
Sign up to request clarification or add additional context in comments.

Comments

0
arr = [ { "stockId":2,"vendorId":1,"vendorCode":"Aya - 01","price":2100 }, null, null ]
arr = arr.filter(elem => elem != null)

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

answered Feb 15, 2022 at 13:16

Comments

0

For example, if you want to remove null or undefined values:

var array = [ { "stockId":2,"vendorId":1,"vendorCode":"Aya - 01","price":2100 }, null, null ];
var filtered = array.filter(function (el) {
 return el != null;
});
 
console.log(filtered);

answered Feb 15, 2022 at 13:16

Comments

0

If you want to remove false values, do something like this


var array = [
 { stockId: 2, vendo`enter code here`rId: 1, vendorCode: 'Aya - 01', price: 2100 },
 null,
 null,
];
newArray = array.filter(item => !!item);
answered Feb 15, 2022 at 13:52

Comments

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.