仅在属性未定义的情况下才将属性添加到对象

时间:2018-10-17 10:21:12

标签: javascript angular

我有以下对象:

   let params = {
            limit: limit,
            offset: 15,
            status: "completed",
            product_id: this.route.snapshot.queryParams.product_id,
        };

以上对象包含用于过滤我的数据源的参数。

this.route.snapshot.queryParams.product_id是一个查询/获取参数

  

?product_id = 123

但是,如果不存在此值,则未定义。因此,如果此值未定义,我不想将product_id成员添加到参数对象。

如果未定义,则对象应类似于:

   let params = {
            limit: limit,
            offset: 15,
            status: "completed",
        };

12 个答案:

答案 0 :(得分:2)

仅在未定义product_id时添加

let params = {
        limit: limit,
        offset: 15,
        status: "completed",
    };

if(this.route.snapshot.queryParams.product_id)
   params['product_id'] = this.route.snapshot.queryParams.product_id;

答案 1 :(得分:2)

内联解决方案:

[[TP  FP][FN  TN]]

或更短:

let params = {
  limit: limit,
  offset: 15,
  status: "completed",
  ...this.route.snapshot.queryParams.product_id && {product_id: this.route.snapshot.queryParams.product_id},
};

答案 2 :(得分:1)

您也可以尝试:

let param = { product_id:this.route.snapshot.queryParams.product_id }
params = this.route.snapshot.queryParams.product_id ? {...params, ...param} : params ;

答案 3 :(得分:0)

我认为您不能内联执行此操作,但是

let params:object;
if ( this.route.snapshot.queryParams.product_id){
    params = {
            limit: limit,
            offset: 15,
            status: "completed",
            product_id: this.route.snapshot.queryParams.product_id,
        };
        }
else{
    params = {
            limit: limit,
            offset: 15,
            status: "completed",
        };  
}

将完成任务。

答案 4 :(得分:0)

您可以通过以下方法进行对象分解

var param = {
            limit: 5,
            offset: 15,
            status: "completed",
            product_id: undefined,
        };
var { product_id, ...rest } = param

param = { ...(product_id && { product_id }), ...rest }

答案 5 :(得分:0)

过去,当我这样做时,我将使用类似于以下示例的内容。我觉得可能有一种更好的方法,但是我还没有找到。

const removeUndefinedValues = objectIn => Object.keys(objectIn)
  .reduce((prev, curr) => {
    return objectIn[curr] !== undefined ?
      {
        ...prev,
        [curr]: objectIn[curr]
      } : prev
  }, {})

let paramsOne = {
    limit: 0,
    offset: 15,
    status: "completed",
    product_id: undefined,
}

let paramsTwo = {
    limit: 0,
    offset: 15,
    status: "completed",
    product_id: "A value",
}

console.dir(removeUndefinedValues(paramsOne))
console.dir(removeUndefinedValues(paramsTwo))

答案 6 :(得分:0)

我个人认为您应该让product_id处于未定义状态,然后检查它是否在后面,但是您可以执行以下操作:

let params = {
  limit: limit,
  offset: 15,
  status: "completed",
};

if(this.route.snapshot.queryParams.product_id) {
  params['product_id'] = this.route.snapshot.queryParams.product_id;
}

答案 7 :(得分:0)

您对以下内容的看法

const constantParams= {
   limit: limit,
   offset: 15,
   status: "completed",
}
let yourDynamicObject = this.route.snapshot.queryParams.product_id 
? {...constantParams, product_id: this.route.snapshot.queryParams.product_id}
: this.paramsWichAreAlwaysThere

答案 8 :(得分:0)

有多种方法可以实现这一目标。我会指出其中两个。 您可以简单地使用if语句添加product_id字段

let product_id = this.route.snapshot.queryParams.product_id;
if(product_id) {
    params.product_id = product_id;
}

您可以使用拆分运算符

let product_id = this.route.snapshot.queryParams.product_id;
let params = {
    limit: limit,
    offset: 15,
    status: "completed",
    ...product_id ? {product_id} : {},
};

答案 9 :(得分:0)

这应该可以完成工作:

let params = {
        limit: limit,
        offset: 15,
        status: "completed",
    };

if (this.route.snapshot.queryParams.product_id) {
   params["product_id"] = this.route.snapshot.queryParams.product_id
}

答案 10 :(得分:-1)

尝试一下

  const product_id = this.route.snapshot.queryParams.product_id;

  let params = Object.assign({
            limit: limit,
            offset: 15,
            status: "completed"
        }, product_id ? {product_id} : {});

答案 11 :(得分:-1)

如何构造具有3个成员并随后根据条件附加的params对象。

let params = {
  limit: limit,
  offset: 15,
  status: "completed",
};
var value = this.route.snapshot.queryParams.product_id;
if (value) {
  params.product_id = value
}