Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

112
Views
Adding attribute/field to and array

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?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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)

about 4 years ago · Juan Pablo Isaza Report

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!