Understanding how to pull info from array - javascript

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.

Related

How can I get a list of User with their ID (list)?

I am trying to find out how to get a user list via their id in tabular form (array).
I've tried this :
let result = message.guild.members.get(id_list)
But it doesn't work ...
id list :
var id_list = [ '223515154229231616',
'447425491041910807',
'479932358422691840',
'464536970417143828',
'299429099611357185',
'332868897646837762',
'198346639843262464',
'266078466972057600',
'334270225895653379',
'198884536363253761',
'216595784286601216',
'358993909269135371',
'248894149162565647',
'396681438688182284',
'349270173011804171',
'358319021726236672',
'216136890963853313',
'282994966014459915',
'298171616968572930',
'257809919036751872',
'239365876846034944',
'502162147942596609',
'449391921840914444',
'473229490584158208' ]
The following should do the trick
let result = id_list.map(id => client.users.get(id));
Basically, you loop through all the ids you have using .map() and fetch the user associated.
You may want to remove all the undefined results (when your bot "does not know" the user linked to a specific id), in that case you need to add the following after the previous piece of code :
result = result.filter(r => r !== undefined);
Check both the documentation of .map() and .filter() for more information
This will not work as it is for one ID at a time, not an array of them. You would need to iterate over the IDs in the array, I suggest using a for loop for this, using id_list.length as one of the parameters. For example:
for(var i = 0; i <= id_list.length; i++) {
//make the request to get the member information
}
Also, just to make sure, you might be better off using client.users.get('id'); because if the user sends a direct message, there is no guild property so an error will be thrown.
You can use
let result = id_list.map(id => client.users.get(id).tag)
That should give back an array with like
['User#0000',
'NextUser#4821'
]

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

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.

sending a tricky array in a httpclient

I have to integrate data from an array into a webservice call which isn't the most efficient but it is what it is.
I have an array of ids (friend facebook ids).
I need to send these id's as parameters in a http client in titanium.
Due to Titanium having some trouble with passing arrays in webservices, I need to construct the send method of my http client as such:
non_xhr.send('user_id=100005941351187&friend_ids[0]=100000049956179&friend_ids[1]=100005272411678');
Obviousy depending on the user, they will have a different number of results to be stored in the array previously mentioned (of facebook friend ids).
I need help in how to integrate a loop based on the length of array mentioned above in order to construct the parameters needed, as described above.
All help appreciated.
I am using Titanium but for the purposes of this question, it is basically just javascript
How about creating your params like that:
function createParams(userId, friendIds) {
var output = "user_id=" + userId;
for(var i = 0, max = friendIds.length; i < max; i++) {
output += "&friend_ids[" + i + "]=" + friendIds[i];
}
return output;
}
You can find a working fiddle here.

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);

jQuery/Javascript pulling info from an object

I am trying to use the following code to create a list of client names from some json returned from an Ajax call.
The data is as follows:
{"status":1,"data":{"clients":[{"ClientID":"1","AccountID":"1","ClientName":"Access Loan Mitigation","Active":"1"},{"ClientID":"2","AccountID":"1","ClientName":"Big Time Business","Active":"1"},{"ClientID":"3","AccountID":"1","ClientName":"Bill Releford","Active":"1"},{"ClientID":"4","AccountID":"1","ClientName":"Bonnie Silverman","Active":"1"},{"ClientID":"5","AccountID":"1","ClientName":"Dear Holdings","Active":"1"},{"ClientID":"6","AccountID":"1","ClientName":"Calm Dental","Active":"1"},{"ClientID":"7","AccountID":"1","ClientName":"Eva Field","Active":"1"},{"ClientID":"8","AccountID":"1","ClientName":"First Independent Pictures","Active":"1"},{"ClientID":"9","AccountID":"1","ClientName":"Gallery 825","Active":"1"},{"ClientID":"10","AccountID":"1","ClientName":"Greenway Arts Alliance","Active":"1"},{"ClientID":"11","AccountID":"1","ClientName":"International Strategy Group","Active":"1"},{"ClientID":"12","AccountID":"1","ClientName":"Ramtin","Active":"1"},{"ClientID":"13","AccountID":"1","ClientName":"Spabro","Active":"1"},{"ClientID":"14","AccountID":"1","ClientName":"LMGA","Active":"1"},{"ClientID":"15","AccountID":"1","ClientName":"Main Street Business Association","Active":"1"},{"ClientID":"16","AccountID":"1","ClientName":"Rabbit Animation","Active":"1"},{"ClientID":"17","AccountID":"1","ClientName":"Rooms & Gardens","Active":"1"},{"ClientID":"18","AccountID":"1","ClientName":"Summertime","Active":"1"},{"ClientID":"19","AccountID":"1","ClientName":"Sue Shellock","Active":"1"},{"ClientID":"20","AccountID":"1","ClientName":"Susan Gates","Active":"1"},{"ClientID":"21","AccountID":"1","ClientName":"The Park Entertainment","Active":"1"},{"ClientID":"22","AccountID":"1","ClientName":"Unified Dispatch","Active":"1"},{"ClientID":"23","AccountID":"1","ClientName":"Westside Media Group","Active":"1"},{"ClientID":"24","AccountID":"1","ClientName":"YHD","Active":"1"},{"ClientID":"25","AccountID":"1","ClientName":"Discoverfire, Inc.","Active":"1"}]}}
and the code is like so:
for (var Client in o.data.clients) {
$('#list_container').append("<div>"+Client.ClientName+"</div>");
}
Not quite working, and I've tried a few different ways of accessing the ClientName property. Javascript isn't my strongest language, and getting data out of objects just kills me - used to PHP object and arrays.
I'm sure this is simple - can somebody show the right syntax?
Thanks!
Clients is an array so it's better to use jQuery's each on it:
$.each( o.data,clients, function(idx, client) {
// use client.ClientName here
});
That's not quite how the for loop works. An easier, more accurate, and more reliable way to tackle this is to use the traditional for syntax like so:
for (var i = 0; i < o.data.clients.length; i++) {
var client = o.data.clients[i];
$('#list_container').append("<div>"+client.ClientName+"</div>");
}
The for syntax you were using will work, but it's still iterating over indices (not values), and even then it's not limited to just the integer indices in the array — it could also include other properties defined on the array prototype, or even on the particular array object. Iterating using the boring i = 0 syntax is a far better option for traditional arrays like this.

Categories

Resources