I am trying to remove an item from an array and return the entire array list but for some reason, .filter() or _.filter() is clearing my main object "itemsList" and leaving only values from the array.
var arr = {"itemsList":[{"id":11000,"name":"Demo","description":"Demo Description"},{"id":"222010","name":"test item","description":"test item desc\n"}]};
var resourceId = "222010";
var out = _.filter(arr.itemsList, function(item){
return item.id !== resourceId;
});
var itemsList = [];
var newOut = itemsList.push(out);
document.getElementById('result').value = JSON.stringify(out);
I need to retun my array the same way and keep other items from the list excluding the id variable like:
{"itemsList":[{"id":11000,"name":"Demo","description":"Demo Description"}]}
Feel Free to use my Fiddle https://jsfiddle.net/RicardoAlves/wbnv3040/
Thanks!
I think the issue is that your not formatting your output as desired.
var arr = {
"itemsList": [{
"id": 11000,
"name": "Demo",
"description": "Demo Description"
}, {
"id": "222010",
"name": "test item",
"description": "test item desc\n"
}]
};
var resourceId = "222010";
var out = _.filter(arr.itemsList, function(item) {
return item.id !== resourceId;
});
var itemsList = { "itemList": out };
console.log(JSON.stringify(itemsList));
You are filtering out the unwanted items but only outputting the item
Output:
{"itemList":[{"id":11000,"name":"Demo","description":"Demo Description"}]}
Filter function returns a new array.
var out = _.filter(arr.itemsList, function(item) {
return item.id !== resourceId;
});
arr.itemsList=out
document.getElementById('result').value = JSON.stringify(arr);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.