Monday, 12 June 2017

NodeJS delete JSON object [duplicate]

This question already has an answer here:

I'm working with JSON in NodeJS, I need to delete from array objects, but with delete [key] will add to array 'null' element. I tried many things, but when I'm trying to replace from string null element, then the string will start with '{"userItems":[,{item},]}', and that is not valid JSON String. If you have any ideas, or you know where I do mistake, please reply.

My code(I hope that this is the all what you need):

userPageInv = JSON.parse(data[0].page_inv);
var deleteItems = [];
for(var i = 0; i < withdrawInv.userItems.length; i++) {
    for(var x=0; x < userPageInv.userItems.length; x++) {
        if(userPageInv.userItems[x].assetid != withdrawInv.userItems[i].assetid) {
            deleteItems.push(x);
            break; 
        }
    }
}
for(var c=0; c<deleteItems.length; c++) {
    delete userPageInv.userItems[deleteItems[c]];
}
var inv_query = JSON.stringify(userPageInv);
inv_query = inv_query.replace('null', '');
inv_query = inv_query.replace(/,+/g,',');
inv_query = inv_query.replace('{"userItems":[' + /,+/g,'{"userItems":[');
inv_query = inv_query.replace(/,+/g + ']}',']}');
console.log(inv_query);

Where data[0].page_inv is for example

{
"userItems": [{
    "item": "XM1014 | CaliCamo",
    "assetid": "10582594938",
    "market": "XM1014 | CaliCamo (Minimal Wear)",
    "icon": "-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgporrf0e1Y07PvRTitD_tW1lY2EqPPxIL7DglRd4cJ5nqeZrN-ki1ayrUE9MmrxLY-Xeg85ZlzS-gW-xOjug8S_vJucm3IysyF0-z-DyLOku5tY"
}, {
    "item": "Sealed Graffiti | ELEAGUE | Atlanta 2017",
    "assetid": "10582594882",
    "market": "Sealed Graffiti | ELEAGUE | Atlanta 2017",
    "icon": "-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXQ9QVcJY8gulRcWEDRSfCshZ-CBBJxKQFWsa-gFABy0vXOeClLotrlwtjalqagMOODzz1SsJQoj7nCpI2s3Vex-xE9YT3yJ4XHcwFqMgvOug_prUrQxxY"
}, {
    "item": "SCAR-20 | Storm",
    "assetid": "10582556730",
    "market": "SCAR-20 | Storm (Field-Tested)",
    "icon": "-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpopbmkOVUw7ODHTi5W4867kpKEmePLPr7Vn35cpsdwi-_Hoo-g2wOx-0NoYW31LITAdAU9YA3Z-lnvkO_vjcLptcybwXV9-n51LW6fRVs"
}]}

withrawInv:

{
"userItems": [{
    "item": "Sealed Graffiti | ELEAGUE | Atlanta 2017",
    "assetid": "10582594882",
    "market": "Sealed Graffiti | ELEAGUE | Atlanta 2017",
    "icon": "-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXQ9QVcJY8gulRcWEDRSfCshZ-CBBJxKQFWsa-gFABy0vXOeClLotrlwtjalqagMOODzz1SsJQoj7nCpI2s3Vex-xE9YT3yJ4XHcwFqMgvOug_prUrQxxY"
}, {
    "item": "SCAR-20 | Storm",
    "assetid": "10582556730",
    "market": "SCAR-20 | Storm (Field-Tested)",
    "icon": "-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpopbmkOVUw7ODHTi5W4867kpKEmePLPr7Vn35cpsdwi-_Hoo-g2wOx-0NoYW31LITAdAU9YA3Z-lnvkO_vjcLptcybwXV9-n51LW6fRVs"
}]}



via Alex

No comments:

Post a Comment