Dave Brady
Dave Brady

Reputation: 203

How to exclude specific string from list using NodeJS

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

TypeScript

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ PRODUCT_ID: string[]; PRODUCT_NAME: string; PRODUCT_PRICE: string; PRODUCT_TYPE: string; }'.
  No index signature with a parameter of type 'string' was found on type '{ PRODUCT_ID: string[]; PRODUCT_NAME: string; PRODUCT_PRICE: string; PRODUCT_TYPE:

product.ts

export interface ProductInput {
  productID: Array<string>;
  productName: string;
  productPrice: string;
  productType: string;
  shippingDate: Date;
  DeliveryDate: Date;
}

export interface ProductOutput {
  status: string;
}

//Lambda function
export const productDetails = async (event: ProductInput):
  Promise<ProductOutput> => {

  var PRODUCT_ID = event.productID;
  var PRODUCT_NAME = event.productName;
  var PRODUCT_PRICE = event.productPrice;
  var PRODUCT_TYPE = event.productType;

  const collection = { PRODUCT_ID, PRODUCT_NAME, PRODUCT_PRICE, PRODUCT_TYPE };

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

    return acc;
  }, {});

  return {
    status: 'success'
  };

};

Upvotes: 0

Views: 947

Answers (3)

DecPK
DecPK

Reputation: 25408

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

EDITED

one-liner

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

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

const collection = { productID, productName, productPrice, productType };
const mapping = {
  produtID: "PRODUCT_ID",
  productName: "PRODUCT_NAME",
  productPrice: "PRODUCT_PRICE",
  productType: "PRODUCT_TYPE",
};
const params = Object.keys(collection).reduce((acc, curr) => {
  if (collection[curr]) acc.push(mapping[curr]);
  return acc;
}, []);
console.log("param value", params);

Upvotes: 1

Abheeth Kotelawala
Abheeth Kotelawala

Reputation: 142

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)

Upvotes: 0

Brandon Pi&#241;a
Brandon Pi&#241;a

Reputation: 893

/* 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

Upvotes: 0

Related Questions