javascript - How to not include field in object if value is undefined - Stack Overflow

I have the following functionconst exampleFunction = (var1, var2, var3) => {return const targetObjec

I have the following function

const exampleFunction = (var1, var2, var3) => {
    return const targetObject = {
        var1,
        var2,
        var3,
    },
};

var2 and var3 are optional variables.

If all 3 variables were send to this function, then I need to return object with 3 fields.

If var2 is undefined, I need to return object with 2 fields only. If var3 is undefined, I need to return object with 2 fields only.

If var2 and var3 are undefined, I need to return object with 1 field only.

I have the following function

const exampleFunction = (var1, var2, var3) => {
    return const targetObject = {
        var1,
        var2,
        var3,
    },
};

var2 and var3 are optional variables.

If all 3 variables were send to this function, then I need to return object with 3 fields.

If var2 is undefined, I need to return object with 2 fields only. If var3 is undefined, I need to return object with 2 fields only.

If var2 and var3 are undefined, I need to return object with 1 field only.

Share Improve this question edited Aug 23, 2018 at 5:53 Mario 4,9963 gold badges42 silver badges62 bronze badges asked Aug 22, 2018 at 5:28 D.MarkD.Mark 5972 gold badges11 silver badges24 bronze badges 3
  • 1 Create the object. Add properties if-they-are-defined. In this case (if var2 is always defined 'before' var3, eg, it would be simpler) one could also create a few different 'defined' permutations. Also, maybe just an array..? – user2864740 Commented Aug 22, 2018 at 5:30
  • With your If var2 is undefined, I need to return object with 2 fields only it sounds as if undefined is specifically being passed as an argument? Eg exampleFunction('foo', undefined, 'bar')? Seems odd – CertainPerformance Commented Aug 22, 2018 at 5:32
  • @CertainPerformance When the value es from another variable, it's not odd. Say for example, var1,2,3 es from form elements, or from an API call in NodeJs – Pubudu Dodangoda Commented Aug 22, 2018 at 6:26
Add a ment  | 

4 Answers 4

Reset to default 2

try this:

const exampleFunction = (var1, var2, var3) => {
    const targetObject = {};
    if (var1) targetObject.var1 = var1;
    if (var2) targetObject.var2 = var2;
    if (var3) targetObject.var3 = var3;

    return targetObject;
};

Use JSON.parse together with JSON.stringify since JSON.stringify will skip undefined, Function, and Symbol during the conversion. If those types are in an array, they will be automatically censored to null in that array by JSON.stringify.

const exampleFunction = (var1, var2, var3) => JSON.parse(JSON.stringify({
  var1,
  var2,
  var3
}))

const outputs = exampleFunction(undefined, 2, undefined)
console.log(outputs)

Outputs: { var2: 2 }

I would suggest you to build the object and then get rid of the undefined values

const targetObj = {var1, var2, var3}
Object.keys(targetObj).forEach((key) => (targetObj[key] == null) && delete targetObj[key]);

Using JSON stringify and parse looks good if you are guaranteed that var1,2,3 are primitive variables. Otherwise they will be converted to strings(For instance Date Objects).

If it is not an inconvenience to use the traditional function statement you could try

function anotherExampleFunction(var1, var2, var3) {
    return Object.assign({}, Array.from(arguments).filter(argument => argument !== undefined));
}

const result1 = anotherExampleFunction(1, 2, 3);
const result2 = anotherExampleFunction(1, 2, undefined);
const result3 = anotherExampleFunction(1, undefined, 3);
const result4 = anotherExampleFunction(1, undefined, undefined);

console.log(result1, result2, result3, result4)

output

{ '0': 1, '1': 2, '2': 3 } { '0': 1, '1': 2 } { '0': 1, '1': 3 } { '0': 1 }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744628452a4584718.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信