3

In the data below. I would like to get it an object with some of the values as keys , like this:

FROM HERE: link to the whole data HERE (this.localObject)

const employees = [{
    "EmployeeID": "100A",
    "FirstName": "Downs",
    "aval": [
       {"start": "11-19", "end": "2", "ava": "30", "health": "4"},
       {"start": "11-20", "end": "2", "ava": "40", "health": "4"},
       {"start": "11-21", "end": "2", "ava": "50", "health": "4"},
       {"start": "11-22", "end": "2", "ava": "60", "health": "4"}
    ]
  },
  {
    "EmployeeID": "100B",
    "FirstName": "Mckenzie",
    "aval": [
       {"start": "11-19", "end": "2", "ava": "1", "health": "4"},
       {"start": "11-20", "end": "2", "ava": "2", "health": "4"},
       {"start": "11-21", "end": "2", "ava": "3", "health": "4"},
       {"start": "11-22", "end": "2", "ava": "4", "health": "4"}
    ]
  },
]

TO HERE:

const employees = [
   { "EmployeeID": "100A", "11-19": "30"},
   { "EmployeeID": "100A", "11-20": "40"},
   { "EmployeeID": "100A", "11-21": "50"},
   { "EmployeeID": "100A", "11-22": "60"},
   { "EmployeeID": "100B", "11-19": "1"},
   { "EmployeeID": "100B", "11-20": "2"},
   { "EmployeeID": "100B", "11-21": "3"},
   { "EmployeeID": "100B", "11-22": "4"}
]

//and so on...

So far I've tried:

this.localData = this.localObject.employees;
const firstLevel = this.localData.map(x => x.aval);

firsLevel will give me an array of objects which is closer to what I need, now I need to put the "start" value as a key and the "ava" value as a value property inside their own object containers

2 Answers 2

4

You need to iterate the inner arrays as well and build a single array.

var array = [{ EmployeeID: "100A", FirstName: "Downs", aval: [{ start: "11-19", end: "2", ava: "30", health: "4" }, { start: "11-20", end: "2", ava: "40", health: "4" }, { start: "11-21", end: "2", ava: "50", health: "4" }, { start: "11-22", end: "2", ava: "60", health: "4" }] }, { EmployeeID: "100B", FirstName: "Mckenzie", aval: [{ start: "11-19", end: "2", ava: "1", health: "4" }, { start: "11-20", end: "2", ava: "2", health: "4" }, { start: "11-21", end: "2", ava: "3", health: "4" }, { start: "11-22", end: "2", ava: "4", health: "4" }] }],
    result = array.reduce((r, { EmployeeID, aval }) => [
        ...r,
        ...aval.map(({ start, ava }) => ({ EmployeeID, [start]: ava }))
    ], []);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

2 Comments

Nice, may I ask any good sources to learn reduce() ?. it's not my forte
you could start here: Array#reduce, but i have no other resource than this. just try often to use it ...
2

You can use Array.flatMap and iterate over the aval inside it using Array.maplike below

const employees = [{
    "EmployeeID": "100A",
    "FirstName": "Downs",
    "aval": [
       {"start": "11-19", "end": "2", "ava": "30", "health": "4"},
       {"start": "11-20", "end": "2", "ava": "40", "health": "4"},
       {"start": "11-21", "end": "2", "ava": "50", "health": "4"},
       {"start": "11-22", "end": "2", "ava": "60", "health": "4"}
    ]
  },
  {
    "EmployeeID": "100B",
    "FirstName": "Mckenzie",
    "aval": [
       {"start": "11-19", "end": "2", "ava": "1", "health": "4"},
       {"start": "11-20", "end": "2", "ava": "2", "health": "4"},
       {"start": "11-21", "end": "2", "ava": "3", "health": "4"},
       {"start": "11-22", "end": "2", "ava": "4", "health": "4"}
    ]
  },
]

let res = employees.flatMap(({ EmployeeID, aval }) => 
                    aval.map(({ start, ava }) => ({ EmployeeID, [start]: ava })))

console.log(res)

6 Comments

it looks like, you are a great fan of flatMap which does not work actually in edge ...
Oh yes I am (after understanding it's power). Given 'Edge' case, I doubt it supports other ESNext features as well at least some. But we should never miss any new feature because of just Edge. :)
man, I love this answer, but the framework I'm using has a problem with ES6 and doesnt play well with FlatMap, they are working on the solution tho
No problem @brohymn. :)
nice one... flatMap is trivial to polyfill
|

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.