0

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

remove the null array from arraylist

Waqas
  • 65
  • 6
  • 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 [ask] and its linked resources. – David Feb 15 '22 at 13:12
  • 4
    *Hint... The `.filter()` method on arrays in JavaScript can be used to filter the elements of an array.* – David Feb 15 '22 at 13:13
  • `array.filter(Boolean)` – savageGoat Feb 15 '22 at 13:13
  • Does this answer your question? [Filter null from an array in JavaScript](https://stackoverflow.com/questions/41346902/filter-null-from-an-array-in-javascript) – evolutionxbox Feb 15 '22 at 13:33

4 Answers4

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

Ceribe
  • 136
  • 2
  • 3
  • 9
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);
ArmNajafi
  • 1
  • 1
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);

Muhoro
  • 1
  • 1
0

More simple and concise solution:

let newArray = array.filter(Boolean);

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

Sheri
  • 1,383
  • 3
  • 10
  • 26