0

I have a list of data items each containing one of many different nested property values like A or B from the same property like e.g. employeeType. I need to create a new array that only contains objects thats value for employee.employeeType equals "B".

const data = [{
  "id": 80,
  "employee": {
    "employeeType":"A"
  }
}, {
  "id": 250,
  "employee": {
    "employeeType" :"B"
  }
}, {
  "id": 263,
  "employee": {
    "employeeType" :"A"
  }
}, {
  "id": 267,
  "employee": {
    "employeeType" :"A"
  }
}, {
  "id": 272,
  "employee": {
    "employeeType" :"A"
  }
}, {
  "id": 281,
  "employee": {
    "employeeType" :"B"
  }
}];
            

Expected ouput

[{
  "id": 250,
  "employee": {
    "employeeType" :"B"
  }
}, {
  "id": 281,
  "employee": {
    "employeeType" :"B"
  }
}]

I tried this but get an error of filter of undefined

const updatedData = data.map((element) => {
    return {...element, subElements: element.subElements.filter((subElement) => 
    subElement.employeeType === "B")}
 })
Peter Seliger
  • 11,747
  • 3
  • 28
  • 37
  • 2
    What is `element.subElements` supposed to be? There's no `subElements` property in your objects. – Barmar Sep 10 '20 at 16:08
  • struggling to follow your logic here. you have an array of objects, not an object. why don't you just filter and maybe map the array if you want to create a deep copy of the array? if you really want only one iteration use a reduce? – bryan60 Sep 10 '20 at 16:08
  • since you do not change the structure of any list item there is no need for a `map`, from your expected result you obviously juts want to `filter`. – Peter Seliger Sep 10 '20 at 16:08
  • If `subElements` is a variable containing the string `employee`, see https://stackoverflow.com/questions/4244896/dynamically-access-object-property – Barmar Sep 10 '20 at 16:11
  • was suppose to be `employees` i missed changing that in the question – user2965653 Sep 10 '20 at 16:13

2 Answers2

3

You can achieve that using just a filter

data.filter(item => item.employee.employeeType === "B")
Sheldon Oliveira
  • 915
  • 9
  • 15
1

This should be enough?

const results = data.filter(item =>  {
   return item.employee.employeeType === 'B'
 })

const data = [
  {
    "id": 80,
    "employee": {
      "employeeType":"A"
    }
  },
  {
    "id": 250,
    "employee": {
      "employeeType" :"B"
    }
  },
  {
    "id": 263,
    "employee": {
      "employeeType" :"A"
    }
  },
  {
    "id": 267,
    "employee": {
      "employeeType" :"A"
    }
  },
  {
    "id": 272,
    "employee": {
      "employeeType" :"A"
    }
  },
  {
    "id": 281,
    "employee": {
      "employeeType" :"B"
    }
  }
]

 const results = data.filter(item =>  {
   return item.employee.employeeType === 'B'
 })

 console.log(results)
Bergur
  • 3,962
  • 12
  • 20
  • If the body of an arrow function is just a `return` statement, you should use the shorthand form. – Barmar Sep 10 '20 at 16:10
  • 1
    I would say: "can" use the shorthand form. "should" is too strong. – trincot Sep 10 '20 at 16:11
  • 1
    'should' is maybe a little to strong, but I get your point. I find that some people, specially new programmers can all the shorthands a little bit confusing. So I usually put the whole bracket, especially if they want to edit the code, it becomes less error prone. – Bergur Sep 10 '20 at 16:13