我如何从对象中删除数组属性

时间:2018-03-28 14:16:20

标签: javascript arrays json object

我想删除属性period_toperiod_from,但如果我使用delete.period_to或delete.period_to[0]则不会删除。

function someData(data)
{
    var formkey = [];
    var formval = [];
    var ch = data;
    var clen = ch.length;
    for(var i =0; i < clen; i++){
        formkey.push(ch[i].name);
        formval.push(ch[i].value);
    }
    var result = {};
    formkey.forEach((key, i) => result[key] = formval[i]);

    delete result.table;
    delete result.redirect_to;
    delete result.dbres;
    delete result.period_to;
    delete result.period_from;
    //console.log(result);
    //
    return result;
}

- chrome console

{name: "qwerty", client_id: "1", user_id: "1", period_from[2]: "11", period_from[1]: "01", …}
client_id: "1"
name: "qwerty"
period_from[0]: "11"
period_from[1]: "01"
period_from[2]: "11"
period_to[0]: "111"
period_to[1]: "09"
period_to[2]: "11"
user_id: "1"
__proto__: Object

5 个答案:

答案 0 :(得分:1)

很少有观察结果:

  • 如果您的obj与-- chrome console中的OP部分类似。然后,您应该删除具有确切名称的对象属性。

    <强>样本

&#13;
&#13;
var obj = {
  "client_id": "1",
  "name": "qwerty",
  "period_from[0]": "11",
  "period_from[1]": "01",
  "period_from[2]": "11",
  "period_to[0]": "111",
  "period_to[1]": "09",
  "period_to[2]": "11",
  "user_id": "1"
};

delete obj["period_to[0]"];
delete obj["period_from[0]"];

console.log(obj);
&#13;
&#13;
&#13;

  • 如果您的对象是这样的:

     var obj = {
      "client_id": "1",
      "name": "qwerty",
      "period_from": ["11","01","11"],
      "period_to[0]": ["111","09","11"],
      "user_id": "1"
    };
    

    然后尝试以下代码

&#13;
&#13;
var obj = {
  "client_id": "1",
  "name": "qwerty",
  "period_from": ["11","01","11"],
  "period_to": ["111","09","11"],
  "user_id": "1"
};

delete obj.period_to;
delete obj.period_from;

console.log(obj);
&#13;
&#13;
&#13;       

答案 1 :(得分:0)

删除是一个操作符而不是一个函数。删除句点并替换为空格。

delete data.period_to

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete

答案 2 :(得分:0)

例如,如果您尝试删除period_from[2],则必须在密钥名称周围加上引号,因为它当前无效。然后,您可以使用括号表示法删除属性。

const obj = {
  name: "qwerty",
  client_id: "1",
  user_id: "1",
  "period_from[2]": "11",
  "period_from[1]": "01"
}

delete obj['period_from[2]'];
console.log(obj)

答案 3 :(得分:0)

你可以把它的第一部分削减到单个reduce函数,应该使它更易读,更容易排除故障。

此外,您可以预先测试属性并仅添加所需的属性,而不是之后尝试进行一系列删除。

var propRgx = /^(table|redirect_to|dbres|period_to|period_from)/

function allowProperty(prop){
  return !propRgx.test(prop) //if the property matches any of those, return false
}

function someData(data)
    {
        //All of the assignment can be replaced with this reduce, and instead of deleting afterward, you can test the properties inside of here:
        var result = data.reduce(function(obj, t){
          if(allowProperty(t.name)) obj[t.name] = t.value
          return obj
        }, {})
        
        return result;
    }

答案 4 :(得分:-1)

您可以使用restdestructuring

var obj = {name: "1", client_id: "1", user_id: "1", period_from: [], period_to: []};

var period_to, period_from;

({period_to, period_from, ...obj} = obj);

console.log(obj);