1

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

remove the null array from arraylist

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.

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

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);

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);

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.