Home > Net >  How to exclude specific string from list using NodeJS
How to exclude specific string from list using NodeJS

Time:10-26

I've use case to generate the list of strings dynamically using NodeJS, if the given string is null I should exclude that string from the list. For example: I've productID, productName, productPrice and productType. If productID is null then productID field should be excluded from the list, if productName is null then productName should be exclude and so on. If productID and productName are null then both the field should excluded from the list.

I've sample code below but this is not a efficient way of doing it, can someone please suggest how can we implement this use case in a better way using NodeJS.

const test = async () => {

    console.log('generate params')

    var params;

    var productID;
    var productName = "APPLE";
    var productPrice = "12";
    var productType = "Electronics"


    if (productID == null) {
        params = {
            "PRODUCT_NAME": productName,
            "PRODUCT_PRICE": productPrice,
            "PRODUCT_TYPE": productType
        }
    } else if (productName == null) {
        params = {
            "PRODUCT_ID": productID,
            "PRODUCT_PRICE": productPrice,
            "PRODUCT_TYPE": productType
        }
    }
    else if (productPrice == null) {
        params = {
            "PRODUCT_ID": productID,
            "PRODUCT_NAME": productName,
            "PRODUCT_TYPE": productType
        }
    }
    else if (productType == null) {
        params = {
            "PRODUCT_ID": productID,
            "PRODUCT_NAME": productName,
            "PRODUCT_PRICE": productPrice,
        }
    }
    console.log("param value", params)
}

test()

CodePudding user response:

/* if first value is falsy, use the second value */
params = {
"PRODUCT_ID": productName | null,
"PRODUCT_NAME": productName | null,
"PRODUCT_PRICE": productPrice | null,
"PRODUCT_TYPE": productType | null
}

/*
and if you want to remove keys that are null 
( i don't know why you would want to do this) 
import omitBy and isNull from lodash and do the following
*/

return( _.omitBy( params, _.isNull ));

forgot that you need to escape underscores in markdown so I had to edit

CodePudding user response:

You can simply create a data-structure (object in this case) and add all properties in it

const collection = { productID, productName, productPrice, productType };

Then you can use reduce to collect it into single object if it is not null

const params = Object.keys(collection).reduce((acc, curr) => {
    if (collection[curr]) acc[curr] = collection[curr];
    return acc;
  }, {});

one-liner

const params = Object.keys(collection).reduce((acc, curr) => (collection[curr] ? { ...acc, [curr]: collection[curr] }: acc), {});

const test = async () => {
  console.log("generate params");

  var productID;
  var productName = "APPLE";
  var productPrice = "12";
  var productType = "Electronics";

  const collection = { productID, productName, productPrice, productType };

  const params = Object.keys(collection).reduce((acc, curr) => {
    if (collection[curr]) acc[curr] = collection[curr];
    return acc;
  }, {});
  console.log("param value", params);
};

test();
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

Shortest way for ES6

params = {
  "PRODUCT_ID": null,
  "PRODUCT_NAME": 'productName',
  "PRODUCT_PRICE": 'productPrice',
  "PRODUCT_TYPE": 'productType' 
}

console.log(params)

//Removes only null
params = Object.entries(params).reduce((a,[k,v]) => (v === null ? a : (a[k]=v, a)), {})

console.log(params)
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related