0

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

5 Answers 5

4

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, e.g. encapsulated in a function mapping the status string to the object.

Sign up to request clarification or add additional context in comments.

Comments

3

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

3 Comments

I found your answer useful, but I have also answer, please clarify me which is better Items.some(function(obj) { if (obj.status === 1) { obj.status = { 1: 'ACTIVE' } } if (obj.status === 2) { obj.status = { 2: 'INACTIVE' } } if (obj.status === 3) { obj.status = { 3: 'DELETED' } } })
Well the answer from sebastian is almost the same like mine, but works the same. His syntax is just shorter, i think this is the best way to go with
@Haronur well going with 3 if` statements is.. well yea... it doesnt look nice for me
2

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

Comments

2

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.

2 Comments

Why are you always using 2 as the key? It looks like the key should be the original numeric status value.
As I'm sure you know, many people misuse the term JSON to refer to the object/array literals, not strings. And the code that was in the question made it clear what they were working with.
0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.