Angular - How to create 2d array in angular dynamically? - javascript

I Have been trying to figure out how can I make this array dynamically and send it to my api. The structure of array is given below.
Photos[image][0] = "a.png"
Photos[image][1] = "b.png"
Photos[image][2] = "c.png"
How can I do this in controller I am stuck every time I apply some solution I get this error Cannot set property '0' of undefined angular array. I think I still dont know what kind of array is this. so far I have implement this solution but God knows why is this headache.
I have three files in this object
$scope.files = [file,file,file].
and i need to put them in the array in the required format I mentioned above. This is my code.
for (var i = 0; i < $scope.file.length; i++) {
Photos[image] = {};
Photos[image][i]= $scope.file[i];
}
console.log(Photos);
Please elaborate my mistake.

I would check out the documentation for angular foreach.
Angular.forEach
This will allow you to iterate over each file in $scope.files. Each file can then be added to your Photos array as you see fit.

Related

Understanding how to pull info from array

I have a previously created script that is doing API calls to get various info. Using json fetch. Its used to look up(GET) properties of users, groups, etc.
Here is how it prints. console.log(myArray):
[{user={conversion_id=smitht, ship_id=14.0, ship=Mountain , id=989, name=Smith, Todd, id=8745335.0, system_id=796663, login_id=todd.smith#domain.com,, created_at=3055-08-10, //keeps continuing for all users in this account
If I wanted to search the array and return only "name". Is there a better way to accomplish than this? This will print out just the names
for (let i = 0; i < myArray.length; i++){
console.log(myArray[i]['user']['name'])
I'm trying to learn what's possible and how to interact. Any other options to search through array? Most examples have sample arrays written out since mine comes from a response its been difficult to follow those examples.
use find https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
function findByLoginId(loginId) {
const result = myArray.find(user => user.login_id === loginId);
return result?.name;
}
Check to make sure that the fetched data is valid JSON. You should be able to use JSON.parse on the returned data.

Get value inside nested object using jQuery

I have the following bit of code
for (var i = 0; i < $scope.d.results.length; i++) {
console.log($scope.d.results[i]);
}
which returns this:
I would like to get the value of the Name field out of this object but I can't think of the syntax to get at it. I've tried a few things with no luck. Can someone explain how to dig down 2 levels and get at it? Thanks!
You can use
$scope.d.results[i].Name
As, $scope.d.results[i] returns an object which contains the Name, you can use above syntax to get it from the object.

Iterating over Lua table in javascript with lua.vm.js

A while ago I posted a THIS question, regarding interaction between lua.vm.js and javascript.
Now I have a slightly different problem:
Lets assume that I have the following code in the REPL
model = {}
model['test1'] = 1
model['test3'] = 2
model['test4'] = 3
model['test5'] = 4
model['test6'] = 5
local window = js.global
window.model = model
Because I'm using lua.vm.js, I now that from javascript I can get all the values by doing
window.model.get('test1')
window.model.get('test2')
window.model.get('test3')
But the problem with that is that I have to know beforehand that the entries at the table are "test1", "test2", etc.
Is there a way in javascript(or maybe by adding some more Lua code) that I can iterate all over the table without knowing all the entries?
Thanks in advance!
I think the easiest way to get the list of keys in model would be to use Lua directly.
From what I can see the lua.vm.js table object does not support iteration, but you can do that from JavaScript:
my_keys = L.execute('local r = {}; for k in pairs(model) do r[#r+1] = k end; return unpack(r)')
Then you can loop like this:
for (var i in my_keys) {
console.log(my_keys[i], window.model.get(my_keys[i]));
}

Knockout Mapping: JSON grows when mapping and saving multiple times

When I take a knockout object and serialize it to JSON by doing ko.toJSON, and then unserialize it from JSON using ko.mapping.fromJSON multiple times, the loaded object has this __ko_mapping__ property that recursively grows.
Sample code:
var joe = new Person();
for (var i = 0; i < 10; i++) {
var json = ko.toJSON(joe);
joe = ko.mapping.fromJSON(json);
}
Simple JSFiddle that reproduces it:
http://jsfiddle.net/Gc89Q/1/
How can I load and save multiple times without having the serialized json form grow to be gigantic?
I was considering just setting the __ko_mapping__ property to undefined when loading, but am wondering if there is a better way or something I am missing.
Is this a bug I file an issue for?
Don't overwrite the model. Instead, pass it to fromJSON so the model is updated:
ko.mapping.fromJSON(json, joe);
http://jsfiddle.net/Gc89Q/2/

Create an array of JavaScript objects from an existing array of objects

my first time posting a question here, so please let me know if I'm missing any information that is needed. Updated to include desired output.
I'm working on a google app script (basically javascript) and am trying to pull objects from an array of objects and create a new array of objects. I'm using the google base functions for getRowData (these can be found at : https://developers.google.com/apps-script/guides/sheets) to create my initial array of objects. This gives me a row of data similar (A JIRA export if anyone is wondering, with information cut):
{summary=Internal - Fix PuppetFile Jenkins Jobs, progress=1.0, issueType=Story, resolution=Done, timeSpent=3600.0, key=XXXXX-646, watchers=0.0, remainingEstimate=0.0, numberOfComments=1.0, status=Resolved, assignee=XXXXXXXX}
When I run my function:
for (var i = 0; i < issueList.length; i++){
rankList[i] = [issueList[i].summary,issueList[i].storyPoints,issueList[i].epicLink,issueList[i].fixVersions];
}
I get:
[Internal - Fix PuppetFile Jenkins Jobs, 3.0, null, null]
But what I want is:
{summary=Internal - Fix PuppetFile Jenkins Jobs, storyPoints=1.0, epicLink=StoryName, fixVersions=Done}
I'm not getting the key for the value, and I don't understand how the objects are constructed quite well enough to get it to transfer over. I looked at some examples of manipulating the key/value pairs but when I tried it on my own I just got a bunch of undefined. Thank you for any help you can provide.
What you want is probably something like this:
rankList = [];
for (var i = 0; i < issueList.length; i++) {
issue = issueList[i];
rankList.push({
summary: issue.summary,
storyPoints: issue.progress,
epicLink: issue.IDONTKNOW,
fixVersions: issue.resolution
});
}
I don't know what field goes in epicLink, since it wasn't obvious from your example. And I was just guessing about the other fields. But this is the general structure, you just need to make all the correct correspondences.
Use jquery each instead it's much easier to get the keys and value at the same time:
var myarray = [];
$.each(issueList,function(key,value){
console.log(key);
console.log(value);
value.key = key;
myarray.push(value);
});
console.log(myarray);

Categories

Resources