Gopi Rain
Gopi Rain

Reputation: 37

Iterate the array and remove the id only

{
  "productGroupVariantss": [
    {
      "id": 1378,
      "name": "No oF Poles",
      "variantsAttributeses": [
        {
          "id": 391,
          "variantsId": null,
          "variantsValue": "1p"
        },
        {
          "id": 392,
          "variantsId": null,
          "variantsValue": "2p"
        },
        {
          "id": 393,
          "variantsId": null,
          "variantsValue": "5p"
        },
        {
          "id": 394,
          "variantsId": null,
          "variantsValue": "4p"
        },
        {
          "id": 395,
          "variantsId": null,
          "variantsValue": "6p"
        }
      ]
    }
  ]
}

In this json object i need to iterate and omit only the id form both arrays productGroupVariantss and variantsAttributeses . Asap now I tried to map the array and omit it was not returning the same json ??

CloneData.productGroupVariantss = []
      var Variant = _.map(model.productGroupVariantss, function(object) {
        _.omit(object, ['id']);
            _.map(object.variantsAttributeses,function(data){
              var p = _.pull(object.variantsAttributeses, 'id');
                var s= _.omit(data, ['id']);
                CloneData.productGroupVariantss.push({
                  name:p,
                  calue:s
                })
                })
      });

My Expected Output is

{
  "productGroupVariantss": [
    {
      "name": "No oF Poles",
      "variantsAttributeses": [
        {

          "variantsId": null,
          "variantsValue": "1p"
        },
        {

          "variantsId": null,
          "variantsValue": "2p"
        },
        {

          "variantsId": null,
          "variantsValue": "5p"
        },
        {

          "variantsId": null,
          "variantsValue": "4p"
        },
        {

          "variantsId": null,
          "variantsValue": "6p"
        }
      ]
    }
  ]
}

Upvotes: 0

Views: 45

Answers (2)

th3n3wguy
th3n3wguy

Reputation: 3747

// I think this is what your data is supposed to look like and I think the copy-paste just was bad.
const data = {
  "productGroupVariantss": [
    {
      "id": 1378,
      "name": "No oF Poles",
      "variantsAttributeses": [
        {
          "id": 391,
          "variantsId": null,
          "variantsValue": "1p"
        },
        {
          "id": 392,
          "variantsId": null,
          "variantsValue": "2p"
        },
        {
          "id": 393,
          "variantsId": null,
          "variantsValue": "5p"
        },
        {
          "id": 394,
          "variantsId": null,
          "variantsValue": "4p"
        },
        {
          "id": 395,
          "variantsId": null,
          "variantsValue": "6p"
        }
      ]
    }
  ]
};

const newOutput = data.productGroupVariantss.map((product) => {
  return _.assign({}, _.omit(product, 'id'), {
    variantsAttributeses: _.map(product.variantsAttributeses, (variant) => _.omit(variant, 'id'))
  });
});

console.log(newOutput);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js"></script>

Upvotes: 1

Napoli
Napoli

Reputation: 1403

You could try this, it's a pretty simple solution.

productGroupVariantss.forEach(function(o, i) {
    if (o.id) {
        delete o.id;
    }
    if (o.variantsAttributeses) {
        o.variantsAttributeses.forEach(function(child) {
            if (child.id) {
                delete child.id;
            }
        });
    }
});

console.log( productGroupVariantss );

Upvotes: 1

Related Questions