简体   繁体   中英

How can I insert an object in an array of objects

I want to convert my object status field that is would be modified specifically. I have found the answer but no fill my goal that's why I updated my question

I have objects like this below:

   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
  }
]

I need to convert my object like below or I need to print like belows:

    [
  {
    "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"
    }
  }
]

For example:

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

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

Update: addded lookup via possibleStatus


If nullish coalescing operator ?? is available, I would add a fallback for undefined status:

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

but only if this is display logic. In case of business logic, I'd rather check for valid values and throw an exception, eg encapsulated in a function mapping the status string to the 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);

If this is json, first you might want to parse it with JSON.parse, like following:

let parse = JSON.parse(yourJsonObj)

Next you get your array, which you need to modify. You can use map method and return a new array with the data you need:

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

Then you can go back and stringify it back if needed with JSON.stringify(newData).

The rules by which you set INACTIVE or ACTIVE I don't know, but this is the gist of it.

I hope this solution be useful for you.

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);

As others have indicated, map() is the way to go.

I've stepped things out a little here in such a way that the system wouldn't have unintended consequences if a third property was introduced. The switch statement explicitly only changes things if the status is 1 or 2 and leaves things alone otherwise.

The other examples given are probably fine for your use case though.

 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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