0

This is the code i need to delete the second element in completely using nodejs

[
{
    "sno": 1,
    "brandName": "EPIDOSIN 8 MG INJECTION",
    "price": "Rs. 17",
    "packagingOfProduct": "1 vial(s) (1 ML injection each)",
    },
{
    "sno": 2,
    "brandName": "ALTACEF 1.5 GM INJECTION",
    "price": "Rs. 327",
    "packagingOfProduct": "1 vial(s) (1 injection each)",

}]
2

3 Answers 3

1

You can use splice function of Array

var data = [
{
    "sno": 1,
    "brandName": "EPIDOSIN 8 MG INJECTION",
    "price": "Rs. 17",
    "packagingOfProduct": "1 vial(s) (1 ML injection each)",
    },
{
    "sno": 2,
    "brandName": "ALTACEF 1.5 GM INJECTION",
    "price": "Rs. 327",
    "packagingOfProduct": "1 vial(s) (1 injection each)",

}]

data.splice(1, 1);

where first argument is index and second argument is number of element need to remove

Ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

Update: If you want to delete particular attribute of object, here is example to delete brandName from each object inside array

data.forEach( obj => delete obj.brandName);

Sign up to request clarification or add additional context in comments.

4 Comments

If it really is JSON, then you'd need to parse it into a java object first, then do your manipulations, and then (if you want), dump it back out to a JSON string.
You are right for Java but question asked for NodeJS and we do not need to convert in NodeJS until json data in string.
if there are plenty of data if want to delete particular element how to do ??
You have array of object. Are you trying to delete particular attribute of object ?
0
let tab = [ {
    "sno": 1,
    "brandName": "EPIDOSIN 8 MG INJECTION",
    "price": "Rs. 17",
    "packagingOfProduct": "1 vial(s) (1 ML injection each)",
    }, {
    "sno": 2,
    "brandName": "ALTACEF 1.5 GM INJECTION",
    "price": "Rs. 327",
    "packagingOfProduct": "1 vial(s) (1 injection each)",

}]

to remove the second element try using :

tab.pop(tab[1])

or

delete(tab[1])

Comments

0

You can use lodash.js for nodejs.

var _ = require('lodash');
var data = [
{
        "sno": 1,
            "brandName": "EPIDOSIN 8 MG INJECTION",
                "price": "Rs. 17",
                    "packagingOfProduct": "1 vial(s) (1 ML injection each)",
                        },
{
        "sno": 2,
            "brandName": "ALTACEF 1.5 GM INJECTION",
                "price": "Rs. 327",
                    "packagingOfProduct": "1 vial(s) (1 injection each)",

}];
_.remove(data, {
        sno: 2
});
console.log(data);

Please refer below link for more details

How can I remove an element from a list, with lodash?

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.