根据字段对数组中的对象之一进行操作

Operating on one of the objects in the array based on a field

有一个具有上述结构的对象。使用此对象,我需要操作 "prop21" 对象数组中存在的对象之一。

let obj = {
    prop: {
        prop21: [
            {
                field: "val1",
                value1: "val2"
            }
        ]
    }
}

以下场景:

每当我将 "ack" 传递给函数时

1) 我需要创建一个格式为 { field: "ack" , value : true } 的对象并将其推送到 prop21 数组,以防对象具有 { field: "ack" , value : true } 不存在。

2) 如果存在 { field: "ack" , value : false },将 value 转换为 true

当我将 "unack" 传递给函数时

1) 我需要创建一个格式为 { field: "ack" , value : false } 的对象并将其推送到 prop21 数组,以防对象具有 { field: "ack" , value : false } 不存在。

2) 如果存在 { field: "ack" , value : true },将 value 转换为 false

当我将 "all" 传递给函数时

它应该基本上删除对象 { field :"ack" , value : true} 或 {field: "ack" , value: false} 如果存在

function manipulate(val) {

    let newObj = { field: "ack", operator: "=", value: true }
    if (value === "ack") {
        // change the "value" field of object with field:"ack" to true if its present, else create a new one with format of "newObj" with value true and push it
    }
    else if (value === "unack") {
        // change the "value" field of object with field:"ack" to false if its present, else create a new one with format of "newObj" with valye false and push it
    }
    else {
        //this is case for value === "all" , hence remove the object with field with value "ack"
    }
}

很简单。解释在评论

let obj = {
    prop: {
        prop21: [
            {
                field: "val1",
                value1: "val2"
            },
            {
                "field": "ack",
                "operator": "=",
                "value": null
            }
        ]
    }
}

function manipulate(value) {
    const newobj = { field: "ack", operator: "=", value: false };
    if (value === "ack") toggleAck(newobj, true);
    else if (value === "unack") toggleAck(newobj, false);
    else removeAck();
}

function toggleAck(newobj, val) {
    newobj.value = val; //modify value of new ack object based on ack/unack
    const ackItem = obj.prop.prop21.find(i => i.field == "ack"); //look for object with ack field
    if (ackItem) ackItem.value = val; //if exists, change this object value
    else obj.prop.prop21.push(newobj); //else, push new obj from template
}

function removeAck() {
    const ackItemIdx = obj.prop.prop21.findIndex(i => i.field == "ack");  //look for object with ack field
    obj.prop.prop21.splice(ackItemIdx, 1); //remove item from array
}

manipulate("ack");
console.log(obj.prop.prop21);
manipulate("unack");
console.log(obj.prop.prop21);
manipulate("all");
console.log(obj.prop.prop21);

我做了单独的功能,这样更容易阅读。你可以optimize/refactor这对你的出价

在代码本身中添加注释以进行解释。希望这有帮助。

let obj = {
  prop: {
    prop21: [
      {
        field: "val1",
        value1: "val2"
      }
    ]
  }
};

function manipulate(val) {
  // value = true for ack and false for unack.
  const newObj = { field: "ack", value: val === "ack" };
  // first remove field: ack for all val cases ack, unack, all.
  obj.prop.prop21 = obj.prop.prop21.filter(prop => !prop.field.includes("ack"));
  // ack and unack will contain 'ack', so checking for ack.
  if (val.includes("ack")) {
    // add it for ack/unack cases.
    obj.prop.prop21.push(newObj);
  }
}

manipulate("ack");
console.log(obj);
manipulate("unack");
console.log(obj);
manipulate("all");
console.log(obj);

let obj = {
  prop: {
    prop21: [{
      field: "val1",
      value1: "val2"
    }]
  }
}


function manipulate(value) {
  let newObj = {
    field: "ack",
    operator: "=",
    value: true
  }
  let isAck = false;
  let index = -1;
  
  let myarr = obj.prop.prop21;
  for(let i =0;i< myarr.length;i++){
   if (myarr[i].field === 'ack') {
      isAck = true;
      index = i;
      break;
    }
  }
  if (value === "ack") {

    if (isAck) {
      let prop21obj = obj.prop.prop21[index];
      obj.prop.prop21[index] = Object.assign(prop21obj, newObj);
    } else {
      obj.prop.prop21.push(newObj);
    }
    // change the "value" field of object with field:"ack" to true if its present, else create a new one with format of "newObj" with value true and push it
  } else if (value === "unack") {


    newobj[value] = false;

    if (isAck) {
      let prop21obj = obj.prop.prop21[index];

      obj.prop.prop21[index] = Object.assign(prop21obj, newObj);
    } else {
      obj.prop.prop21.push(newObj);
    }

    // change the "value" field of object with field:"ack" to false if its present, else create a new one with format of "newObj" with valye false and push it
  } else {

    if (isAck) {

      obj.prop.prop21.splice(index, 1);
    }
    //this is case for value === "all" , hence remove the object with field with value "ack"
  }
}

manipulate('ack');
console.log(obj);

您可以通过查找对象并使用 switch 作为更新类型来采取直接的方法。

function change(array, type) {
    var index = array.findIndex(({ field }) => field === 'ack'),
        temp = array[index] || { field: "ack", value: type === "ack" };

    switch (type) {
        case 'ack':
        case 'unack':
            if (index !== -1) temp.value = type === "ack";
            else array.push(temp);
            break;
        case 'all':
            if (index !== -1) array.splice(index, 1);
    }
}

let obj = { prop: { prop21: [{ field: "val1", value1: "val2" }] } };

change(obj.prop.prop21, 'ack');
console.log(obj.prop.prop21);

change(obj.prop.prop21, 'unack');
console.log(obj.prop.prop21);

change(obj.prop.prop21, 'ack');
console.log(obj.prop.prop21);

change(obj.prop.prop21, 'all');
console.log(obj.prop.prop21);
.as-console-wrapper { max-height: 100% !important; top: 0; }