i have this in a collection:
resul = {
'name': 'bob',
'position': 'ceo',
}
when i run a python code again with a different position value a new data , it's overwriting the position and i dont want it to
result i want:
resul = {
'name':'bob',
'position':'ceo',
'age':25,
}
result i am getting:
resul = {
'name':'bob',
'position':'co-founder',
'age':25,
}
code using:
info_dic={}
info_dict.update(resul)
infoREferenc = db.collection(u'test').document(u'bob')
infoREferenc.set({"general":info_dic}, merge=True)
If the field(ex. position,age,name) is existing in your map(general) it will just update the value of the field. If not, it will write the field and insert with its value. I suggest add a new field in your map for additional position, for example:
info_dic={
u'name': u'bob',
u'position':u'ceo',
u'secondary-position': u'co-founder',
'age': 26
}
infoREferenc = db.collection(u'test').document(u'bob')
infoREferenc.set({'general':info_dic}, merge=True)
Related
I have a list of user ids like so: ["1111","5555","1111","8983",...]. I then compute the distribution of the frequency of the ids. But somehow adding the size of the distribution bins is smaller than the user set.
function histogram(List){
var d = {};
for(const x of List){
if (x in d){
d[x]+=1;
}
else{
d[x]=1;
}
}
return d
}
var featureuserids = f1_users.concat(f2_users,f3_users,f4_users)
var featureusers = [...new Set(featureuserids)];
const featurehist = histogram(Object.values(histogram(featureuserids)))
const n_featureusers = featureusers.length
Here is an example output.
Feature Users: 17379
Feature Hist: { '1': 16359, '2': 541, '3': 93, '4': 6 }
What is my mistake?
I have found the answer. One of my Lists (f1_users) had saved the ids as int, while the others were in string format. Therefore they were counted double in the set. After converting them all to string the issue was fixed.
I'm trying to write an automation script that needs to get the values from the output below. The problem is the CValue is not a constant number. It can range anywhere from 1 - x sample values. Is there a way I can store each value properly?
{
'Output': {
'Name': 'Sample',
'Version': {
'Errors': [],
'VersionNumber': 2,
'AValue': 'Hello',
'BValue': ['val:val:BVal'],
'CValue': [{
'DValue': 'aaaaa-bbbbb-cccc',
'Name': 'Sample_Name_1'
}, {
'DValue': 'aaaaa-bbbbb-ddddd',
'Name': 'Sample_Name_2'
}]
}
},
'RequestId': 'eeeee-fffff-gggg'
}
Right now, I'm doing it in the most inefficient way by storing each value separately. My code looks like something below:
def get_sample_values():
test_get = command.sdk(xxxx)
dset_1 = test_get['Output']['Version']['CValue'][0]['DValue']
dset_2 = test_get['Output']['Version']['CValue'][1]['DValue']
return dset_1, dset_2
It works but it's limited to only 2 sets of the dset. Can you please provide input on how I can do it more efficiently?
Use case is this, I need the DValues for another function that requires it. The format for that request is going to be something like:
Source = {
'SourceReference': {
'DataReference': [
{
'EValue': 'string, string, string'
'FValue': DValue1
},
'EValue': 'string, string, string'
'FValue': DValue2
}
]
}
Use a list comprehension to create a list constructed from the desired element of the CValue dicts, then return the list.
return [x['DValue'] for x in test_get['Output']['Version']['CValue']]
Does this work for you?
# either
def get_sample_values():
test_get = command.sdk(xxxx)
return test_get['Output']['Version']['CValue']
# or a generator - maybe not that useful here, but possible
def get_sample_values():
test_get = command.sdk(xxxx)
yield from test_get['Output']['Version']['CValue']
# Then you can use it
for value in get_sample_values():
print(value)
# or
print(values[3])
# for the generator
values = list(get_sample_values())
print(values[3])
For more information https://realpython.com/introduction-to-python-generators/
So my goal is to have an object variable that will be empty at the start but as the code starts running it would get filled up with data from other varibales. When it gets filled up it should look like this:
var fruits = [banana, apple, ...];
var colors = [yellow, green, ...];
var calories = [300, 250, ...]
//the JSON object
{
banana :
{
"color" : "yellow",
"calories" : 300
},
apple :
{
"color" : "green",
"calories" : 250
},
...
}
As you can see all of the data is supposed to be pulled from other variables and this is where I bump into problems. I've tried the following:
var object.fruits[0] = {colors : calories};
//also tried this
var object.fruits[0] = "{""'" + (colors[0]) + "'":+calories[0]+"}";
//and tried many other things...
I've been failing to counter this for at least an hour now and what makes it worse is that some data is supposed to come from other JSON objects. Any idea how to make it work? Also note that having them in an object array is not a option as the list will be HUGE and therefore the time efficiency will be very poor.
Maybe try something like this
res = {}
fruits.map((key, index) => {
res[key] = {
'color': colors[index],
'calories': calories[index]
}
})
You can do like this but yeah put validations to make sure all three arrays are of equal length.
Whenever you want to add a property to an Object where the property value is a value of another variable it is better to use the bracket notation to add the properties to the object [] as used below.
One more thing better use let and const in place of var.
Finally you can use JSON.stringify() to convert into JSON String from the Javascript Object.
'use strict';
const fruits = ['banana', 'apple'];
const colors = ['yellow', 'green'];
const calories = [300, 250];
const fruits_object = {};
for (let i = 0; i < fruits.length; i++) {
fruits_object[fruits[i]] = {};
fruits_object[fruits[i]]['color'] = colors[i];
fruits_object[fruits[i]]['calories'] = calories[i];
}
console.log(JSON.stringify(fruits_object));
Just do it normally like so:
color: colors[0]
and then call JSON.stringify on the entire object like so
JSON.stringify({color: colors[0]})
I'm using vuejs and laravel.
In component, i have:
data(): {
return {
data: []
}
}
After fetching, i have this.
I want to load more data if user scroll, so i have to add new object into data.
I tried Object.assign, push... but the properties had be overwritten.
I also loop the data and add new object but not work either...
I want something like:
obj1 = {0: value1, 1: value2};
obj2 = {0: value3, 1: value4};
=> obj = {0: value1, 1: value2, 3: value3, 4: value4};
Any idea? Tks!
You can extract the values from the object using Object.values(), then join both of the values using array#concat then using Object.assign() create your object.
const obj1 = {0: 'value1', 1: 'value2'},
obj2 = {0: 'value3', 1: 'value4'},
result = Object.assign({}, Object.values(obj1).concat( Object.values(obj2)));
console.log(result);
You can also use array#reduce instead of Object.assign.
const obj1 = {0: 'value1', 1: 'value2'},
obj2 = {0: 'value3', 1: 'value4'},
result = Object.values(obj1).concat( Object.values(obj2)).reduce((r,v,i) => (r[i] = v, r), {});
console.log(result);
data:function(){
return {
data: []
}
}
Now you can add element either by
this.data.push(object);
or You can concat another array like this -
this.data = this.data.concat(anotherArray);
After Updating the question -
/* For the case in question, you would do: */
Object.assign(obj1, obj2);
/** There's no limit to the number of objects you can merge.
* All objects get merged into the first object.
* Only the object in the first argument is mutated and returned.
* Later properties overwrite earlier properties with the same name. */
let obj = Object.assign({}, obj1, obj2, obj3, etc);
I have a custom list that is used as a matrix option of Inventory item. Its 'Color'. This custom list has an abbreviation column. I am creating a saved search on item and using Color field(join) and trying to access 'Abbreviation' field of color.
Abbreviation on custom list is available on when 'Matrix Option List' is checked.
Can someone please help me achieve this? I tried to do this through script and it seems like we cannot access 'Abbreviation' column through script. I also tried to use script to write a search directly on 'Color' - custom list and get the 'abbreviation' through search columns. It did not work. Is there a way to access 'Abbreviation' from custom lists?
Thanks in Advance
You can access it via suitescript by using the record type "customlist" and the internal id of the list like so:
var rec = nlapiLoadRecord('customlist', 5);
var abbreviation = rec.getLineItemValue('customvalue', 'abbreviation', 1);
nlapiLogExecution('DEBUG', 'abbreviation', abbreviation);
Keep in mind that the third argument of getLineItemValue is the line number, not the internal ID of the item in the list. If you want to find a specifc line item, you may want to use rec.findLineItemValue(group, fldnam, value).
Unfortunately, it doesn't look like this translates to saved searches. The suiteanswer at https://netsuite.custhelp.com/app/answers/detail/a_id/10653 has the following code:
var col = new Array();
col[0] = new nlobjSearchColumn('name');
col[1] = new nlobjSearchColumn('internalid');
var results = nlapiSearchRecord('customlist25', null, null, col);
for ( var i = 0; results != null && i < results.length; i++ )
{
var res = results[i];
var listValue = (res.getValue('name'));
var listID = (res.getValue('internalid'));
nlapiLogExecution('DEBUG', (listValue + ", " + listID));
}
However, whatever part of the application layer translates this into a query doesn't handle the abbreviation field. One thing to keep in mind is that the 'custom list' record is basically a header record, and each individual entry is it's own record that ties to it. You can see some of the underlying structure here, but the takeaway is that you'd need some way to drill-down into the list entries, and the saved search interface doesn't really support it.
I could be wrong, but I don't think there's any way to get it to execute in a saved search as-is. I thought the first part of my answer might help you find a workaround though.
Here is a NetSuite SuiteScript 2.0 search I use to find the internalId for a given abbreviation in a custom list.
/**
* look up the internal id value for an abbreviation in a custom list
* #param string custom_list_name
* #param string abbreviation
* #return int
* */
function lookupNetsuiteCustomListInternalId( custom_list_name, abbreviation ){
var internal_id = -1;
var custom_list_search = search.create({
type: custom_list_name,
columns: [ { name:'internalId' }, { name:'abbreviation' } ]
});
var filters = [];
filters.push(
search.createFilter({
name: 'formulatext',
formula: "{abbreviation}",
operator: search.Operator.IS,
values: abbreviation
})
);
custom_list_search.filters = filters;
var result_set = custom_list_search.run();
var results = result_set.getRange( { start:0, end:1 } );
for( var i in results ){
log.debug( 'found custom list record', results[i] );
internal_id = results[i].getValue( { name:'internalId' } );
}
return internal_id;
}
Currently NetSuite does not allows using join on matrix option field. But as you mentioned, you can use an extra search to get the result, you could first fetch color id from item and then use search.lookupFields as follows
search.lookupFields({ type: MATRIX_COLOR_LIST_ID, id: COLOR_ID, columns: ['abbreviation'] });
Note: Once you have internalid of the color its better to use search.lookupFields rather than creating a new search with search.create.