I am completing a fairly complicated process in node and for it to work properly I need to compare the value of a key in an object to an array. I have checked to make sure I have no async issues and am simply using a indexOf to get the index of the object's key in the array. I have checked the typeof each item and both return "object" "object". Here is an example check that I am making.
var cID = [55cebe83d0b3d];
var item = { _id: 55cebe83d0b377d,
_client: 55cebe83d0b3d,
institution_type: 'test' }
var cIndex = cID.indexOf(item._client)
and then cIndex always equals -1 regardless of it really exists within cID. If I console log item._client it is 55cebe83d0b3d which is exactly what is stored within cID but still returns -1. Any ideas? I also have the data coming from MongoDB and the data is going through body-parser before hitting this function.
I suppose that hex numbers are not js-native. Turn them into hex strings. E.g. '5cebe83d0b377d' instead of 5cebe83d0b377d.
Update
Mea culpa, see Hiyper's comment.
Related
I didn't even know that this was possible, what is this? I tried running this on chrome and firefox, and got the same result:
I'm sending a 100 item array from the server, but when it gets to the client, this happens.
I tried accessing the item at index 100, and got undefined as response.
I also tried slicing it to the first 100 items, but the rest of the key pairs remain there.
I can't try to access the values by key because they are random every time. If that's of any value.
Since an array is also an object, you can add arbitrary properties to it. Run this code then look in your browser console (not the console inside this post).
const array = ['11034.61000000', '0.30200000'];
array['10987'] = 0.009101;
array['11024.05'] = 0.001998;
array['11026.96'] = 0.001;
array['11026.59'] = 1.5;
console.log(array);
console.log(Object.keys(array));
Object.keys(array) will show you every property of the array, even the random ones.
db = new Array("myserver", "myfolder\\mydb.nsf")
dir = getComponent("Dir").value;
div = getComponent("Div").value;
lu = #DbLookup(db, "ManagerAccess", dir + "PP" + div, "DTManagers");
var a = [];
a.push(lu);
var item:NotesItem = docBackEnd.replaceItemValue('FormReaders', #Unique(a));
item.setReaders(true);
That code is on the querySaveDocument ssjs. The result I get from the #DbLookup (when I put in a computed field) look like this:
Pedro Martinez,Manny Ramirez,David Ortiz,Terry Francona
I tried doing an #Explode(#Implode) thing on it, but it doesn't seem to work.
The error I get in the browser just tells me that the replaceItemValue line is broken.
To test it, I pushed several strings one at a time, and it worked correctly populating my FormReaders field with the multiple entries.
What am I doing wrong?
I see several problems here:
A. In cases as described by you #Dblookup in fact would return an array. If you push an array into a plain computedField control it will exactly look as that you wrote:
value1, value2, ..., valueN
A computedField doesn't know anything about multiple values etc, it just can display strings, or data that can be converted to strings.
If you want to test the return value you could try to return something like lu[0]; you then should receive the array's 1st element, or a runtime error, if lu is NOT an array. Or you could ask for the array's size using lu.length. That returns the number of array elements, or the number of characters if it's just a plain string.
B. your code contains these two lines:
var a = [];
a.push(lu);
By that you create an empty array, then push lu[] to the first element of a[]. The result is something like this:
a[0] = [value1, value2, ..., valueN],
i.e. a is an array where the first element contains another array. Since you don't want that, just use #Unique(lu) in your replaceItemValue-method.
C. I don't see why replaceItemValue would throw an error here, apart from what I wrote in topic B. Give it a try by writing lu directly to the item (first without #Unique). That should work.
D. for completeness: in the first line you used "new Array". A much better way to define your db parameters is
var db = ["myserver", "myfolder/mydb.nsf"];
(see Tim Tripcony's comment in your recent question, or see his blog entry at http://www.timtripcony.com/blog.nsf/d6plinks/TTRY-9AN5ZK)
I face a problem when tried to assign a value with a specific index. suppose I have javascript variable like
var track = new Array();
Now I assign a value for a specific index like-
track[10]= "test text";
now array has one value and it's length would be 1. But the main problem is it show it's length is 11.
alert(track.length); // 11 but I expect 1
and if I print its value then it shows like--
alert(track); // ,,,,,,,,,test text
and if I console this array then it show like below--
console.log(track); // undefined,undefined,undefined,undefined,.....,test text
I am very much confused because I assign only one value but it show 11. How it assign it's value and what characteristics array variable shows. Can anyone explain me and how to get its length 1 using below code--
var track = new Array();
track[10]= "test text";
alert(track); // test text
alert(track.length); // 1
console.log(track); // test text
The Array object automatically fills in the missing indexes. The reason it gives length 11 is because the index starts at 0.
If you are wanting to use key-value just use an object.
var track = {};
It will not have a .length value however.
javascript automatically fills in the array to push the element you want. You can get the "true" count by doing this:
track.filter(Boolean).length
However note that this will only "work" if you do not have any other elements that resolve to "false" value (eg. empty strings or setting them to false) so if you want to this, make sure you never actually set any other array elements to a falsy value so that you can use this convention. For example if you want to set other array values to a falsy value, use something like -1 instead as the thing to check.
Since you're setting the value for 10th position it's showing array size of 11
You must start from 0th position..
var track = new Array();
track[0]= "test text";
alert(track); // test text
alert(track.length); // 1
console.log(track); // test text
Try this
For such kind of operations i generally prefer the library called Underscore.js.
It abstracts array manipulations. You might want to checkout the compact method
Compact works like this:
_.compact([undefined, undefined, undefined, "test test"]) as ["test test"]
Then you can check the length of the returned array.
Though a simple approach is
filter(Boolean).length
But if you want to use the array then you might like underscore.
I have an object
data = {
'choiceA' : 'Long-Wear',
'choiceB' : 'Anti-Age Lifting/Firming',
'choiceC' : 'Replenishing/ Moisturizing',
'choiceD' : 'Natural/ True-to-Skin',
'choiceE' : 'Smoothing/ Illuminating'
}
and I need to retrieve the fourth value given an integer
position = 3;
normally I would write
key = $.inArray( position, ['choiceA', 'choiceB', 'choiceC', 'choiceD', 'choiceE']);
answer = data[key];
but is it valid javascript to access the object directly with the numeric key like this?
answer = data[position]; // where position is an integer
EDIT:
some bad code I wrote as I'm using $.inArray backwards!
I meant to write
arr = ['choiceA', 'choiceB', 'choiceC', 'choiceD', 'choiceE'];
key = arr[position];
answer = data[key];
No, it is not valid until you have numeric object "keys", i.e.
data = {
'1' : 'Long-Wear',
'2' : 'Anti-Age Lifting/Firming',
'3' : 'Replenishing/ Moisturizing',
'4' : 'Natural/ True-to-Skin',
'5' : 'Smoothing/ Illuminating'
};
Also, it is important to note, that properties in JavaScript objects are not sorted. So your solution is the best way to go as I see.
if you would have tried it, you would have seen that it is not valid. even more it is not working!
data[position]
would return undefined in your example...
Two things:
Javascript keys are always strings. If you pass something else (say, a number) it is converted to a string. (For example, try indexing a array with array["3"] and it should work
Javascript objects are not ordered! You cannot get back the i-th key value pair portably. Instead you should use an array to store things (or something like that):
data = [
{ name:'choiceA', value: 'Long-Wear'},
{ name:'choiceB', value: 'Anti-Age Lifting/Firming'}
];
One thing you could do if you can't change the data representation is iterate though your object using a for-in loop, plus a counting variable. However, this approach is not portable, since not all browsers iterate in the same order as the keys were defined.
OK, I'm missing something here and I just can't seem to find it because the logic seems correct to me, but I'm certain I'm not seeing the error.
var VisibleMarkers = function() {
var filtered = _.reject(Gmaps.map.markers, function(marker) {
return marker.grade != $('.mapDataGrade').val() && !_.contains(marker.subjects,$('.mapDataSubjects').val())
});
return filtered
}
I'm using underscore.js and jQuery to simplify my javascript work.
So right now, I'm checking by means of selects which data gets to be rejected and then I display the filtered markers on the (google) map (if it helps at all, this is using gmaps4rails which is working perfectly fine, its this bit of javascript that's making me lose the last of the hairs on my head).
Currently, the code functions 100% correctly for the ".mapDataGrade" select, but the ".mapDataSubjects" isn't. Now the markers object has a json array of the subjects (this is for students) and each item in the array has its ID. Its this ID that I am supposed to be checking.
Can someone see what I'm doing wrong?
If there's more info that needs to be included, please let me know.
This is on plain javascript on a RoR application using gmaps4rails
Now the markers object has a json array of the subjects (this is for students) and each item in the array has its ID. Its this ID that I am supposed to be checking.
_.contains compares a values, but it sounds like you want your iterator to compare a value to an object's "id" property. For that, _.some would work; it's like contains, except that, instead of comparing values, you can write the comparison as a function:
Returns true if any of the values in the list pass the iterator truth test.
Here's how you'd use it:
!_.some(marker.subjects, function(subject) {
return subject.id == $('.mapDataSubjects').val();
})
If I'm right, the whole line should be like this:
return marker.grade != $('.mapDataGrade').val() &&
// check that none of the subjects is a match
!_.some(marker.subjects, function(subject) {
// the current subject is a match if its ID matches the selected value
return subject.id == $('.mapDataSubjects').val();
});