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)",

}]
ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
  • Related answer [https://stackoverflow.com/questions/3396088/how-do-i-remove-an-object-from-an-array-with-javascript](https://stackoverflow.com/questions/3396088/how-do-i-remove-an-object-from-an-array-with-javascript) – Siva Rm K May 04 '18 at 06:17
  • Need more information. based on what do you want to remove the element? – AdityaParab May 04 '18 at 06:18

3 Answers3

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);

Arif Khan
  • 5,039
  • 2
  • 16
  • 27
  • 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. – Doug May 04 '18 at 06:22
  • You are right for Java but question asked for NodeJS and we do not need to convert in NodeJS until json data in string. – Arif Khan May 04 '18 at 06:25
  • if there are plenty of data if want to delete particular element how to do ?? – suresh narasimman May 04 '18 at 06:34
  • You have array of object. Are you trying to delete particular attribute of object ? – Arif Khan May 04 '18 at 06:38
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])
mathi
  • 13
  • 3
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?

Thavaprakash Swaminathan
  • 6,226
  • 2
  • 30
  • 31