wisnuaryadipa
wisnuaryadipa

Reputation: 760

Remove square bracket([ ]) in json file node JS

I want to remove square bracket "[ ]" in json object or concatenate some array of object to one array of object.

This code for counting current stock

currentStock : ( req, res, next) => {

    var listed_item = _(arr_items).map( (z) => {

        var result = module.exports.getTransactionAfterDate(z.created_at, z.item_id);

        return result;

    })

    var jsonContent = JSON.stringify(listed_item);

    return res.send(jsonContent);
},


getTransactionAfterDate : (date, item_id) => {

    var result = _(db_transaction).filter( (x) => {

        return x.created_at > date && x.item_id == item_id;

    });

    return result;

}

this is "listed_item" json result

[
    [{
        "id": 30608,
        "item_id": "A01001",
        "quantity": 150,
        "status": "OUT",
        "created_at": "2020-02-10 16:11:51",
    }],
    [],
    [{
        "id": 30412,
        "item_id": "A02001",
        "quantity": 53,
        "status": "OUT",
        "created_at": "2020-02-06 14:44:20",
    }, {
        "id": 30482,
        "item_id": "A02001",
        "quantity": 33,
        "created_at": "2020-02-07 15:26:50",
        "updated_at": "2020-02-07 15:26:50",
    }]
]

what i want is like this

[
    {
        "id": 30608,
        "item_id": "A01001",
        "quantity": 150,
        "status": "OUT",
        "created_at": "2020-02-10 16:11:51",
    },
    {
        "id": 30412,
        "item_id": "A02001",
        "quantity": 53,
        "status": "OUT",
        "created_at": "2020-02-06 14:44:20",
    }, {
        "id": 30482,
        "item_id": "A02001",
        "quantity": 33,
        "created_at": "2020-02-07 15:26:50",
        "updated_at": "2020-02-07 15:26:50",
    }
]

note i tried concatenate "getTransactionAfterDate" inside "arr_items" map to join all array inside

var temp = [];
var listed_item = _(arr_items).map( (z) => {

    temp = _(temp).concat(module.exports.getTransactionAfterDate(z.created_at, z.item_id));
    var result = temp;

    return result;

})

but the result is empty array

[ [], [], [] ]

Upvotes: 0

Views: 3431

Answers (3)

HubballiHuli
HubballiHuli

Reputation: 777

this can be done with the combination of _.flatten (for this lodash is required) and Array.prototype.filter like shown below

let newArray = _.flatten(listed_item.filter(e => e.length > 0))

Upvotes: 1

calarin
calarin

Reputation: 236

You can 'flatten' the array with lodash, which will just merge all the arrays into a single one:

_(arr).flatten()

That should then give you a single array of your objects, with no empty arrays either.

.flatten() in the lodash docs here: https://lodash.com/docs/#flatten

So it would be something like this (edited based on comments):

currentStock : ( req, res, next) => {

    var listed_item = _(arr_items).map( (z) => {

        var result = module.exports.getTransactionAfterDate(z.created_at, z.item_id);

        return result;

    })

    var flattened = _(listed_item).flatten();

    var jsonContent = JSON.stringify(flattened);

    return res.send(jsonContent);
},

Nice and simple! (if I correctly understood what you are trying to do :-)

Upvotes: 1

x7R5fQ
x7R5fQ

Reputation: 1029

Try this

currentStock : ( req, res, next) => {
    var items = [];

    _(arr_items).forEach( (z) => {

        var result = module.exports.getTransactionAfterDate(z.created_at, z.item_id);

        result.forEach( item => {
          items.push(item);
        }

    })

    var jsonContent = JSON.stringify(items);

    return res.send(jsonContent);
},

Upvotes: 1

Related Questions