I am currently looping through a json object with the purpose of writing certain key value pairs to a new json file. I can write all the elements in the newVideos array to a new file. However, I need to get the first 50 and write it to a file, then the next 50 and so on... Can anyone point me in the right direction?
async function GetVideos(videoData) {
var videos = videoData.videos;
var newVideos = [];
for (var i = 0; i < videos.length; i++) {
var o = videos[i];
newVideos[i] = {
ID: o.ID,
Videos: o.Videos
};
}
}
Related
how can i do a split for each item inside an array of strings ?
i am trying with for, but the typescript acuses Type 'string[]' is not assignable to type 'string'.. I don't know another way to made that.
to contextualize , I am converting the request.files into a JSON , and JSON to Array, and i need the first numbers of the file name , which will be used as an ID to the database
my try with for:
let ids: Array<string> = []; //Array declaration
var myfiles = JSON.parse(JSON.stringify(req.files)) // Convert Request Files to JSON
myfiles.map((item: any) => {
ids.push(item.filename) //Push each file name into the array
})
for(var i = 0; i< ids.length; i++){
ids[i] = ids[i].split('_',1) // Here would change the name of each item inside the array, but it accuses the aforementioned error
}
and my try with foreach, who accuses the same error
ids.forEach((item, index) => {
ids[index] = item.split('_', 1)
})
SOLUTION
as our friend long_hair_programmer suggested, changing ids[i] = ids[i].split('_',1) to ids[i] = ids[i].split('_',1)[0] resolves the problem
I'm working in the Google Earth Engine code editor. I have a feature collection containing fires in multiple states and need to generate a unique list of states that will be used in a selection widget. I'm trying to write a function that takes the list of state values for all fires, creates a new list, and then adds new state values to the new unique list. I have run the code below and am not getting any error messages, but the output is still statesUnique = []. Can anyone point me in the right direction to get the new list to populate with unique values for states?
My Code:
// List of state property value for each fire
var states = fire_perim.toList(fire_perim.size()).map(function(f) {
return ee.Feature(f).get('STATE');
}).sort();
print('States: ', states);
// Create unique list function
var uniqueList = function(list) {
var newList = []
var len = list.length;
for (var i = 0; i < len; i++) {
var j = newList.contains(list[i]);
if (j === false) {
newList.add(list[i])
}
}
return newList
};
// List of unique states
var statesUnique = uniqueList(states);
print('States short list: ', statesUnique)
Okay, I did not come up with this answer, some folks at work helped me, but I wanted to post the answer so here is one solution:
var state_field = 'STATE'
var all_text = 'All states'
// Function to build states list
var build_select = function(feature_collection, field_name, all_text) {
var field_list = ee.Dictionary(feature_collection.aggregate_histogram(field_name))
.keys().insert(0, all_text);
return field_list.map(function(name) {
return ee.Dictionary({'label': name, 'value': name})
}).getInfo();
};
var states_list = build_select(fire_perim, state_field, all_text)
print(states_list)
I'm currently having some trouble with Client-Side Scripting of an Inventory Detail Subrecord on an Assembly Build. As you know, Assembly Builds have two Inventory Details. The one in the top right corner works as expected, and I can access every field with Suitescript.
I'm having trouble with the bottom Inventory Detail though. I'm able to access it and use nlapiGetFieldValue() just fine. However, when I access the sublist, I am only able to look up the value of 'id'.
These are the fields that are supposed to exist, along with a less documented one called "receiptinventorynumber".
Here is my code:
//get the line items in the bottom inventory details
var bottom_line_items = [];
for(var line_index = 0; line_index < nlapiGetLineItemCount("component"); line_index++)
{
var bottom_inv_detail = nlapiViewLineItemSubrecord("component", 'componentinventorydetail', line_index+1);
if(bottom_inv_detail != null)
{
var bottom_line_count = bottom_inv_detail.getLineItemCount('inventoryassignment');
for(var index =0; index < bottom_line_count; index++)
{
bottom_inv_detail.selectLineItem('inventoryassignment', index+1);
var sn = bottom_inv_detail.getCurrentLineItemValue('inventoryassignment', 'receiptinventorynumber');
bottom_line_items.push(sn);
}
}
}
console.log(bottom_line_items);
Here is the result of executing it in the browser console:
As you can see, 'id', and 'internalid' work. 'receiptinventorynumber' does not. Neither do any of the other fields.
Because of my use case, I cannot wait for the record to be saved on the server. I have to catch this client side. Any suggestions are appreciated.
It has been a long time since I have worked with Inventory Detail subrecord, but I think there is another field called 'assigninventorynumber'. Have you tried using that?
Just was able to answer my own question, but it did end up involving a server-side search. I'm pretty sure it works as I wanted it to, but I am still involved in testing it. In essence, I grabbed the field 'issueinventorynumber', which had an id. That id was the internalid of a 'Inventory Serial Number', which I was able to perform a search for to get the actual number. Here's the resulting code:
//get the line items in the bottom inventory details
var bottom_line_ids = [];
for(var line_index = 0; line_index < nlapiGetLineItemCount("component"); line_index++)
{
var bottom_inv_detail = nlapiViewLineItemSubrecord("component", 'componentinventorydetail', line_index+1);
if(bottom_inv_detail != null)
{
var bottom_line_count = bottom_inv_detail.getLineItemCount('inventoryassignment');
for(var index =0; index < bottom_line_count; index++)
{
bottom_inv_detail.selectLineItem('inventoryassignment', index+1);
var sn = bottom_inv_detail.getCurrentLineItemValue('inventoryassignment', 'issueinventorynumber');
bottom_line_ids.push(sn);
}
}
}
//do search to identify numbers of bottom serial numbers
var columns = [new nlobjSearchColumn('inventorynumber')];
var filters = []
for(var index = 0; index < bottom_line_ids.length; index++)
{
filters.push(['internalid', 'is', bottom_line_ids[index]]);
filters.push('or');
}
//remove the last 'or'
if(filters.length > 0)
{
filters.pop();
}
var search = nlapiCreateSearch('inventorynumber', filters, columns);
var results = search.runSearch().getResults(0,1000);
bottom_line_items = []
if(results.length != bottom_line_ids.length)
{
//if you get to this point, pop an error as the 'issueinventorynumber' we pulled is associated with multiple serial numbers
//you can see which ones by doing a 'Inventory Serial Number' Saved Search
//this is a serious problem, so we'd have to figure out what to do from there
}
for(var index = 0; index < results.length; index++)
{
bottom_line_items.push(results[index].getValue('inventorynumber'));
}
console.log(bottom_line_items);
i want to get all the records in particular record type , but i got 1000 only.
This is the code I used.
function getRecords() {
return nlapiSearchRecord('contact', null, null, null);
}
I need two codes.
1) Get whole records at a single time
2) Get the records page wise by passing pageindex as an argument to the getRecords [1st =>0-1000 , 2nd =>1000 - 2000 , ...........]
function getRecords(pageIndex) {
.........
}
Thanks in advance
you can't get whole records at a time. However, you can sort results by internalid, and remember the last internalId of 1st search result and use an additional filter in your next search result.
var totalResults = [];
var res = nlapiSearchRecord('contact', null, null, new nlobjSearchColumn('internalid').setSort()) || [];
lastId = res[res.length - 1].getId();
copyAndPushToArray(totalResult, res);
while(res.length < 1000)
{
res = nlapiSearchRecord('contact', null, ['internalidnumber', 'greaterthan', lastId], new nlobjSearchColumn('internalid').setSort());
copyAndPushToArray(totalResult, res);
lastId = res[res.length - 1].getId();
}
Beware, if the number of records are high you may overuse governance limit in terms of time and usage points.
If you remember the lastId you can write a logic in RESTlet to take id as param and then use that as additional filter to return nextPage.
You can write a logic to get nth pageresult but, you might have to run search uselessly n-1 times.
Also, I would suggest to use nlapiCreateSearch().runSearch() as it can return up to 4000 records
Here is another way to get more than 1000 results on a search:
function getItems() {
var columns = ['internalid', 'itemid', 'salesdescription', 'baseprice', 'lastpurchaseprice', 'upccode', 'quantityonhand', 'vendorcode'];
var searchcolumns = [];
for(var col in columns) {
searchcolumns.push(new nlobjSearchColumn(columns[col]));
}
var search = nlapiCreateSearch('item', null, searchcolumns);
var results = search.runSearch();
var items = [], slice = [], i = 0;
do {
slice = results.getResults(i, i + 1000);
for (var itm in slice) {
var item = {};
for(var col in columns) { item[columns[col]] = slice[itm].getValue(columns[col]); } // convert nlobjSearchResult into simple js object
items.push(item);
i++;
}
} while (slice.length >= 1000);
return items;
}
My code gathers all categories from a CSV file, sorts and grabs top 10 categories and the top 10 result are displayed on a chart. The code works fine if the gategory found is 10, but if it is less than 10 no chart is displayed..basically code dies.
I am a newbie when it comes to coding and the code was passed on to me by someone else who is not available. What I would like to add is an if cases that checks:
Gather all category, sort
Set category to 1 to 10
if category is null, stop. Chart has no value
if category=1 ===> Display found value
if category=2 ===> Display found value
if category=3 ===> Display found value
if category=3 ===> Display found value
So on, so on..untill it reaches to 10
//collect top 10 cat from array
catArray.sort(sort_by("count", false, function (a) {
return parseInt(a)
}));
var categorytop10 = new Array(catArray[0]["tier3"], catArray[1]["tier3"], catArray[2]["tier3"], catArray[3]["tier3"], catArray[4]["tier3"], catArray[5]["tier3"], catArray[6]["tier3"], catArray[7]["tier3"], catArray[8]["tier3"], catArray[9]["tier3"]);
var categorytop10Count = new Array(catArray[0]["count"], catArray[1]["count"], catArray[2]["count"], catArray[3]["count"], catArray[4]["count"], catArray[5]["count"], catArray[6]["count"], catArray[7]["count"], catArray[8]["count"], catArray[9]["count"]);
Any help is appreciated. Thanks
Based on the limited info provided, here is what I can suggest.
//collect top 10 cat from array
catArray.sort(sort_by("count", false, function (a) {
return parseInt(a)
}));
var categorytop10 = new Array();
var categorytop10Count = new Array();
for (var i = 0; i < 10 && i < catArray.length; ++i)
{
categorytop10.push( catArray[i]["tier3"] );
categorytop10Count.push( catArray[i]["count"] );
}
EDIT: You can also try this
//collect top 10 cat from array
catArray.sort(sort_by("count", false, function (a) {
return parseInt(a)
}));
var categorytop10 = new Array();
var categorytop10Count = new Array();
for (var i = 0; i < 10; ++i)
{
if (i < catArray.length)
{
categorytop10.push( catArray[i]["tier3"] );
categorytop10Count.push( catArray[i]["count"] );
}
else {
categorytop10.push( "?" );
categorytop10Count.push( "0" );
}
}