2

I want to restructure my array, which i retrieved from php, to make it usable with my ngFor.

Currently my array contains ~2700 elements which contain a object with the following data:

name: "Someone"
shift: 3
week: 1
department_name: "somedepartment"

I want change it to array->week->shift->department.name

In JS this would be easy but in Typescript I cant get it working, my actual code looks like:

test(data): void{
//data contains the actual array
let user = [];
for (let entry of data){
  console.log(entry['week']);
  console.log(entry['shift']);
  console.log(entry['department_name']);
  user[entry['week']][entry['shift']][entry['department_name']] = entry;
}
console.log(user);}

console.log returns the first 3 values: 1, 3, "departmentx"

And then returns:

ERROR Error: Uncaught (in promise): TypeError: Cannot read property '3' of undefined TypeError: Cannot read property '3' of undefined

I have read that I have to set the size of the array before, but it will be every time different and later there will be much more information besides the name that get stored.

3
  • AngularJS or Angular? Can't be both. Commented Apr 28, 2017 at 9:05
  • "ERROR Error: Uncaught (in promise): TypeError: Cannot read property '3' of undefined TypeError: Cannot read property '3' of undefined" is a runtime error, not a TypeScript error. Commented Apr 28, 2017 at 9:15
  • Ty Angular is correct. For me its a typescript error cause the same code works in js. Commented Apr 28, 2017 at 9:46

1 Answer 1

2

If I understood well you want to change the order of the elements within your array, right?

This should work:

    let newArray = [];
    for (let entry in data) {
        let currentObj = {
            [entry.week]: {
              [entry.shift]: entry.department_name
            }
        };
        newArray.push(currentObj);
    }
    console.log(JSON.stringify(newArray));

the output of this would be something like:

   newArray = [
        1: {
            3: "somedepartmentX"
        },
        2: {
            4: "somedepartmentY",
        },
        3: {
            5: "somedepartmentZ",
        },
    ];
Sign up to request clarification or add additional context in comments.

5 Comments

Ty this gets close to what I want and it works. The problem is that I just need it multidimensional. Which means the properties have to be set somehow like this: let currentObj = {entry.week: {entry.shift: {entry.department: entry.name}}} //entry.shift, week and department just contain the key for the new array
@Doomenik Updated, I hope this is closer to what you need, or at least is a good start so you can achieve what you want. hope it helped! ;)
This is exactly what I want but it does not work. In the declaration of currentObj it says at entry. "expected ':' ". The terminal throws than some million error codes :D Terminal first error: Unexpected Token.. entry: .week Seems like Typesript translates it wrong
Perfect! I would never have tought about this... just had to add '[ ]' ty. Now I just have to get away from push so that it combines the element on the the entry.week and entry.shift level. At the moment I still have 2700 elements. But this problem I will get by my own. Can you change entry.shift to [entry.shift], than I can mark your answer as correct.
@Doomenik Changed! Brilliant! I'm really glad it helped you. Now let's keep coding! ;) Good luck! :)

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.