0

I'm having the below list and I would like to add only these property names PRODUCT_TYPE, PRODUCT_TERM, PRODUCT_ID in myProduct. I want to ignore rest of the properties - I've around 100 properties and want to filter only a few of them from myProduct

Please find my code below:

const obj = {
  myProduct: [
    {
      name: "PRODUCT_PRICE",
      value: "234.324",
    },

    {
      name: "PRODUCT_NAME",
      value: "Insurance",
    },
    {
      name: "PRODUCT_TYPE",
      value: "Life",
    },
    {
      name: "PRODUCT_TERM",
      value: "Long",
    },
    {
      name: "PRODUCT_ID",
      value: "AP3232343JKD",
    },
    {
      name: "PRODUCT_ENABLED",
      value: "TRUE",
    },
  ],
};

const allowedNames = [
  'PRODUCT_TYPE',
  'PRODUCT_TERM',
  'PRODUCT_ID'
];

const updateCertainProperties = {
  PRODUCT_ID: "app.productID",
  PRODUCT_ENABLED: "app.product.enabled"
};

const productName = "testProduct_3234dfasfdk3msldf23";
const environment = obj.myProduct.map((o) => {
  obj.myProduct.filter(product => allowedNames.includes(product.name));
  if (updateCertainProperties[o.name]) o.name = updateCertainProperties[o.name];
  if (o.name === "PRODUCT_NAME") o.value = productName;    
  return obj.myProduct;
});

console.log(obj.myProduct)

Expected output:

[
  { name: 'PRODUCT_NAME', value: 'testProduct_3234dfasfdk3msldf23' },
  { name: 'PRODUCT_TYPE', value: 'Life' },
  { name: 'PRODUCT_TERM', value: 'Long' },
  { name: 'app.productID', value: 'AP3232343JKD' },
  { name: 'app.product.enabled', value: 'TRUE' }
]

Can someone please help me how can I achieve this? Appreciated your help in advance!

JSON Derulo
  • 9,780
  • 7
  • 39
  • 56
Mike Marsh
  • 387
  • 3
  • 15
  • Many duplicated answers for this. https://stackoverflow.com/questions/21659888/javascript-find-and-remove-object-in-array-based-on-key-value – hotcakedev Nov 23 '21 at 14:31

2 Answers2

1

You can create an array of allowed names and filter them out using includes()

css just for prettier output

UPDATE
added updateCertainProperties object values into allowedNames array and moved filter outside environment map.

const obj = {
    myProduct: [
        {
            name: "PRODUCT_PRICE",
            value: "234.324",
        },

        {
            name: "PRODUCT_NAME",
            value: "Insurance",
        },
        {
            name: "PRODUCT_TYPE",
            value: "Life",
        },
        {
            name: "PRODUCT_TERM",
            value: "Long",
        },
        {
            name: "PRODUCT_ID",
            value: "AP3232343JKD",
        },
        {
            name: "PRODUCT_ENABLED",
            value: "TRUE",
        },
    ],
};

const allowedNames = [
    'PRODUCT_TYPE',
    'PRODUCT_TERM',
    'PRODUCT_NAME'
];

const updateCertainProperties = {
    PRODUCT_ID: "app.productID",
    PRODUCT_ENABLED: "app.product.enabled"
};

allowedNames.push(...Object.values(updateCertainProperties));

const productName = "testProduct_3234dfasfdk3msldf23";

const environment = obj.myProduct.map((o) => {
    if (updateCertainProperties[o.name]) o.name = updateCertainProperties[o.name];
    if (o.name === "PRODUCT_NAME") o.value = productName;
    return obj.myProduct;
});

obj.myProduct = obj.myProduct.filter(product => allowedNames.includes(product.name));

console.log(obj.myProduct)
.as-console-wrapper {
  max-height: unset !important;
  top: 0;
}
ciekals11
  • 2,032
  • 1
  • 10
  • 24
  • Thanks @ciekals11. Your answer looks good - I forgot to update a few scenarios in my original post. Dynamically I'm changing few property names for example PRODUCT_ID -> app.productID and other use case is changing the value of the PRODUCT_NAME. I've updated the code in my original post, on top of that I want to include your filter logic but something is going wrong there -could pls help me with your filter logic in my existing use case. Appreciated! – Mike Marsh Nov 23 '21 at 14:58
  • @MikeMarsh I have updated my answer. Had to add just one line after `updateCertainProperties` object declaration. – ciekals11 Nov 24 '21 at 08:06
0

It sounds like you're describing filtering an array, not "excluding properties". You have an array of objects, with each object consisting of a name property and value property. And you only want objects with specific values in their name property.

Using .filter on the array, it might look something like this:

obj.myProduct = obj.myProduct.filter(p => (
  p.name === 'PRODUCT_TYPE' ||
  p.name === 'PRODUCT_TERM' ||
  p.name === 'PRODUCT_ID'));

This would filter out all elements of the array which don't match the supplied condition.

David
  • 208,112
  • 36
  • 198
  • 279