0

I am working on an angular app. I have data as follows.

data = [
{
  "sampleData": [{
    "id":"1"
  }],
  "status": "completed"
},
{
  "sampleData": [{
    "id":"1"
  }],
  "status": "not completed"
},
{
  "sampleData": [],
  "status": "completed"
}
]

Above one is the sample array.. Like this at run time my array can 200-300 records. I want to add a attribute/field "category" to each element on the basis of following condition.

  • if length of sample data is greater than 0, then check for status. If status is completed than attribute/field "category": "completed" will be added. If status is not completed than attribute/field "category": "not completed" is added.

  • If length of sample data is equal to 0, then "category": "on hold" attribute/field is added to array.

So, above array after manupulation should look like as follows:

data = [
{
  "sampleData": [{
    "id":"1"
  }],
  "status": "completed",
  "category": "completed"
},
{
  "sampleData": [{
    "id":"1"
  }],
  "status": "not completed"
  "category": "not completed"
},
{
  "sampleData": [],
  "status": "completed"
  "category": "on hole"
}
]

Similarly I can have 200-300 elements in an array at runtime and I need to add category to each and every element on the basis of above condition and make a final array. How can I do it in a good and efficient way?

2 Answers 2

1

Loop through the array and check whether the item's sampleData property length is greater than 0, and if so, set the item's category property to the item's status property. Otherwise, set it to "on hold".

const data = [{
    "sampleData": [{
      "id": "1"
    }],
    "status": "completed",
  },
  {
    "sampleData": [{
      "id": "1"
    }],
    "status": "not completed"
  },
  {
    "sampleData": [],
    "status": "completed"
  }
]


data.forEach(e => e.category = e.sampleData.length > 0 ? e.status : "on hold")

console.log(data)

The same logic can be implemented if you don't want to mutate the original by using Array.map:

const data = [{
    "sampleData": [{
      "id": "1"
    }],
    "status": "completed",
  },
  {
    "sampleData": [{
      "id": "1"
    }],
    "status": "not completed"
  },
  {
    "sampleData": [],
    "status": "completed"
  }
]


const res = data.map(e => ({ ...e,
  category: e.sampleData.length > 0 ? e.status : "on hold"
}))

console.log(res)

With an if statement (as per OP's request):

const data = [{
    "sampleData": [{
      "id": "1"
    }],
    "status": "completed",
  },
  {
    "sampleData": [{
      "id": "1"
    }],
    "status": "not completed"
  },
  {
    "sampleData": [],
    "status": "completed"
  }
]


data.forEach(e => e.category = e.sampleData.length > 0 ? e.status : "on hold")

console.log(data)

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

2 Comments

could you please also given an example how I can add similar attribute to each element using if.. else if. Please keep the above example as well. I just want to try to both ways
@Rahul I've updated my answer.
0

You can use Array.map():

function addCategory(source) {
  return source.map( item => ({
    ...item,
    category: item.sampleData.length === 0
      ? 'on hold'
      : item.status === 'completed'
      ? 'completed'
      : 'non completed';
  });
}
.
.
.
const withCategory = addCategory( getMeSomeSourceData() );

That's about as easy as it gets.

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.