Angular trying to add scope data to an array - json

I am trying to add data from the Scope to an Array.
In my example I have $scope.employee which have several other fields, like name, number, email, etc.
So when I push the data to the Array I use:
array.push($scope.employee);
This works, because it adds the data to the array, but the binding still applies, so if I later on change thing in the scope, the data in the array also changes. I want this array to be static and not change. Just to hold the elements I have passed into it.
I would like to only add the data from the $scope.employee and not the binding.
I know it's possible to:
array.push({name: $employee.name, number: $employee.number});
But the $scope.employee have many fields, therefore I want to push the whole object.

You want to use angular.copy to make a deep copy without the pointers.
array.push(angular.copy($scope.employee));
documentation

Related

What is a good way to retrieve the associated value from a struct's enum property when the associated value might have multiple possible types?

I have an array of structs that has been decoded from a JSON file. Each struct has a stored property that is a variable-dimension array stored in an enum associated value.
Some of the structs have property Enum.array2D([[Float]]) and others have Enum.array3D([[[Float]]])
Is there a simple or elegant way to extract a variable-type associated value from the struct's enum property - maybe with a getter function? Currently, the only way I know how to do this is with an external switch anytime I want to access the underlaying value. For example, somewhere in external code I have to use this anytime I want to get these values and manipulate them:
switch structArray[index].enumProperty {
case .array2D(let array2Val):
// Do stuff with the 2D array
case .array3D(let array3Val):
// Do stuff with the 3D array
}
I have considered adding each of the two possible types as optionals and setting the correct one in the init function with a switch, but that seems inefficient as I’ll have the arrays stored in two places.

Mule how to add to the array used in for each?

I have an array in Mule that I am iterating over. I believe the for each makes a copy of the array I pass it and iterates through that. Is it possible to access that copy by reference and modify it? Based on certain conditions I would like to add extra elements to the array. I have tried to use vars.rootMessage but it does not give me what I am expecting.
No. Just create a new array in a variable and add to it. To provide more context, Mule 4 variables live in Mule Events, which are immutable. That means that a change results in the creation of a new event.

receive Excel data and turn into objects to format a JSON

I have this solution that helps me creating a Wizard to fill some data and turn into JSON, the problem now is that I have to receive a xlsx and turn specific data from it into JSON, not all the data but only the ones I want which are documented in the last link.
In this link: https://stackblitz.com/edit/xlsx-to-json I can access the excel data and turn into object (when I print document.getElementById('output').innerHTML = JSON.parse(dataString); it shows [object Object])
I want to implement this solution and automatically get the specified fields in the config.ts but can't get to work. For now, I have these in my HTML and app-component.ts
https://stackblitz.com/edit/angular-xbsxd9 (It's probably not compiling but it's to show the code only)
It wasn't quite clear what you were asking, but based on the assumption that what you are trying to do is:
Given the data in the spreadsheet that is uploaded
Use a config that holds the list of column names you want returned in the JSON when the user clicks to download
based on this, I've created a fork of your sample here -> Forked Stackbliz
what I've done is:
use the map operator on the array returned from the sheet_to_json method
Within the map, the process is looping through each key of the record (each key being a column in this case).
If a column in the row is defined in the propertymap file (config), then return it.
This approach strips out all columns you don't care about up front. so that by the time the user clicks to download the file, only the columns you want are returned. If you need to maintain the original columns, then you can move this logic somewhere more convenient for you.
I also augmented the property map a little to give you more granular control over how to format the data in the returned JSON. i.e. don't treat numbers as strings in the final output. you can use this as a template if it suites your needs for any additional formatting.
hope it helps.

How do I pass data between tvml views?

I need to pass data between views in my client-server app. For simple string value, I can put them as attributes on the target element and read the value when the select event is triggered on it. From there, I can pass this string value onto the next document pretty easily.
But the problem comes with much more complex data that's in JSON format. I tried doing JSON.stringify(myData) and putting this value in an attribute. But the compiler doesn't like the { in this attribute value.
I could probably try escaping all the different characters that the compiler has problems with. But I don't think that's a good idea.
Is there any way of implementing jQuery's .data() functionality in TVML and TVJS ? Or is there any other way that makes sending data between views a possibility ?
You can pass your data as URL parameters. Then in the new view, get them using Javascript.
EDIT: And I see in the comment above you came to a similar conclusion.
You could keep your data in a semi-global associative array. Store the key in an attribute on the element and use that to get your data structure.
Ex:
var globalData;
function onSelect(e){
var id=e.target.getAttribute("id");
var specificData=globalData[id];
}

How To Pass Multiple 'Datas' To A View?

I know that you can pass data to a view using:
navigator.pushView(views.LoadoutView, list.selectedItem)
But what if I pass that data, and then want to pass another piece of data to the same view using a similar method?
Can I get/set a new property or can I write the current data to an xml file as soon as it is received?
Here is a little diagram I made of what I'm trying to achieve (I spent hours on it :P).
From what I've understood, you need to push multiple objects to a view at once, correct?
The data object can only be a single object or object reference, which means that if you want to push more than just your list.selectedItem, create a new Object (a generic one will do) that contains both your properties and push it, much like the following;
var myDataObject:Object = {firstPieceOfData:list.selectedItem, secondPieceOfData:yourSecondObjectHere};
navigator.pushView(views.LoadoutView, myDataObject);