繁体   English   中英

如何在对象数组中插入 object

[英]How can I insert an object in an array of objects

我想转换我的 object 状态字段,该字段将被专门修改。 我找到了答案,但没有完成我的目标,这就是我更新问题的原因

我在下面有这样的对象:

   Items = [
  {
    "id": 9,
    "alias": "5cbe5c1c-e36b-422d-beb3-225a8e549bf1",
    "name": "sfasf",
    "status": 1
  },
  {
    "id": 5,
    "alias": "ed8a6921-c2c2-4a49-8893-5bf5c2bc0d98",
    "name": "Test",
    "status": 2
  },
  {
    "id": 6,
    "alias": "ed8a921-c2c2-4a49-8893-5bf5c2bc0d98",
    "name": "Test",
    "status": 3
  }
]

我需要像下面那样转换我的 object 或者我需要像下面这样打印:

    [
  {
    "id": 9,
    "alias": "5cbe5c1c-e36b-422d-beb3-225a8e549bf1",
    "name": "sfasf",
    "status": {
      "1": "ACTIVE"
    }
  },
  {
    "id": 5,
    "alias": "ed8a6921-c2c2-4a49-8893-5bf5c2bc0d98",
    "name": "Test",
    "status": {
      "2": "INACTIVE"
    }
  },
  {
    "id": 6,
    "alias": "ed8a921-c2c2-4a49-8893-5bf5c2bc0d98",
    "name": "Test",
    "status": {
      "3": "DELETED"
    }
  }
]

例如:

const possibleStatus = {
  1: 'ACTIVE',
  2: 'INACTIVE'
}

Items.map(item => ({...item, status: {[item.status]: possibleStatus[item.status]}}))

更新:通过possibleStatus添加了查找


如果无效合并运算符?? 可用,我会为未定义的状态添加一个后备:

Items.map(item => ({...item, status: {[item.status]: possibleStatus[item.status] ?? `UNDEFINED STATUS ${item.status}`}}))

但前提是这是显示逻辑。 在业务逻辑的情况下,我宁愿检查有效值并抛出异常,例如封装在 function 中,将状态字符串映射到 object。

 let statusTable = { 1: "ACTIVE", 2: "INACTIVE", 3: "DELETED" } let Items = [ { "id": 9, "alias": "5cbe5c1c-e36b-422d-beb3-225a8e549bf1", "name": "sfasf", "status": 1 }, { "id": 5, "alias": "ed8a6921-c2c2-4a49-8893-5bf5c2bc0d98", "name": "Test", "status": 2 }, { "id": 6, "alias": "ed8a921-c2c2-4a49-8893-5bf5c2bc0d98", "name": "Test", "status": 3 } ] let result = Items.map(el => { el.status = { [el.status]: statusTable[el.status] } return el; }) console.log(result);

如果这是 json,首先你可能想用 JSON.parse 解析它,如下所示:

let parse = JSON.parse(yourJsonObj)

接下来,您将获得需要修改的数组。 您可以使用 map 方法并返回包含所需数据的新数组:

let newData = parse.map(item => {
  item.status = { [item.status]: "INACTIVE" };
  return item;
});

然后,您可以返回 go 并在需要时使用 JSON.stringify(newData) 将其字符串化。

你设置 INACTIVE 或 ACTIVE 的规则我不知道,但这是它的要点。

我希望这个解决方案对你有用。

Items = [
  {
    "id": 9,
    "alias": "5cbe5c1c-e36b-422d-beb3-225a8e549bf1",
    "name": "sfasf",
    "status": 1
  },
  {
    "id": 5,
    "alias": "ed8a6921-c2c2-4a49-8893-5bf5c2bc0d98",
    "name": "Test",
    "status": 2
  }
]

Items = Items.map(item => {
item.status = item.status == 1 ?  { "1": "ACTIVE" } : { "2": "INACTIVE" }
return item;
} )

console.log(Items);

正如其他人指出的那样, map()是通往 go 的方式。

我在这里做了一些改动,这样如果引入了第三个属性,系统就不会产生意想不到的后果。 switch 语句仅在状态为 1 或 2 时显式地改变事物,否则不做任何事情。

不过,给出的其他示例可能适合您的用例。

 var items = [ { "id": 9, "alias": "5cbe5c1c-e36b-422d-beb3-225a8e549bf1", "name": "sfasf", "status": 1 }, { "id": 5, "alias": "ed8a6921-c2c2-4a49-8893-5bf5c2bc0d98", "name": "Test", "status": 2 }, { "id": 6, "alias": "ed8a921-c2c2-4a49-8893-5bf5c2bc0d98", "name": "Test", "status": 3 } ]; const process = () => { // render original const orig = document.querySelector('.original'); orig.innerHTML = ''; items.forEach(i => orig.innerHTML += `<li>${i.status}</li>`); var result = items.map(i => { switch(i.status) { case(1): i.status = {"1": "ACTIVE"} break; case(2): i.status = {"2": "INACTIVE"} break; case(3): i.status = {"3": "DELETED"} break; default: // if status is not 1, 2 or 3, do nothing to the object break; } // return the object return i }) // render processed const res = document.querySelector('.results'); res.innerHTML = ''; items.forEach(i => res.innerHTML += `<li>${JSON.stringify(i.status)}</li>`); } process()
 <div class="cols"> <div> <p>Original</p> <ul class="original"> </ul> </div> <div> <p>Result</p> <ul class="results"></ul> </div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM