lodash findindex push to an array - lodash

I am using _.findIndex which returns me an array, which needs to be pushed to array. How can I do this?
$scope.filtersRequested[_.findIndex( $scope.filtersRequested, {
'codeColumnName': $scope.refData[idx].codeColumnName
} )].filterCondition = strWhere;

If I understand correctly, you'd like to set filterCondition on a specific value. Since you use lodash, you'd better use _.set which is safe (i.e. does not fail if the first arg is undefined) and _.find (to get access to the relevant request). Hence, I'd suggest you do:
_.set(
_.find( $scope.filtersRequested, {'codeColumnName': $scope.refData[idx].codeColumnName} ) ,
'filterCondition', strWhere
);
If an element was found, the _.set will operate on it, otherwise, it will gracefully ignore it.

Related

filter and range with map are not working

my need is to go over a list but extract only the first two elements out of that list.
i wrote the following code
payload.list.*listItem filter ($.type == "supervisor") map {
// other code
}
this worked perfectly fine. Then I modified it to extract the first two elements only like this
payload.list.*listItem filter ($.type == "supervisor") [0 to 1] map {
// other code
}
and it doesnt work anymore. i am getting no error but the other code doesnt work as if the map operator has nothing to go over.
also i need to make it work for a scenario if there is only one element after the filter is applied.
how should i change my code to make this work.
The range selector must be used directly over an Array so you just need to properly do the selection:
(payload.list.*listItem filter ($.type == "supervisor"))[0 to 1] map {
// other code
}
You need to add parenthesis to ensure precedence. Also, I'm surprised it worked for you at all. I needed to put "type" in single quotes to get it to work since that is a dataweave keyword. I did this ?($$<2) instead of [0 to 1] since I wasn't sure if you were guaranteed to have at least two objects in the output of the filter. If you know, you'll have at least two, leave it the way you had it.
(payload.list.*listItem filter ($.'type' == "supervisor"))[?($$<2)] map (value, index) ->
{
"key":value
}
Here's a link to Dataweave documentation on precedence. It snags a lot of people. Safer just to use parens in most cases.

Clarifying behavior of unset

I wanted to clarify my understanding of unset() or rather the behavior I am observing. I understand if I call unset() it replaces the value with a null (per the deleting data in Gun). So this is what I would like to confirm, assuming you've called unset():
1) When you call once() or on() it returns null for nodes which have been unset()
2) When you call Gun.obj.empty(table, '_') it returns false
I also tried setting the value of my set to null e.g.
get('mylist').put(null)
Which worked! I wanted to empty my set. However, the next time I added a new node my original set along with all of the original nodes were restored. I ended up writing the following to empty my set
this.context.once().map().once(data => {
let key = data["_"]["#"];
let node = this.context.get(key);
if (node) {
this.context.unset(node);
}
});
I believe that .unset tombstones (null) an item in the table, but not the table itself. Just so others are aware: .unset is community maintained inside of the GUN repo, and not maintained by me - so I may be wrong about its behavior OR the extension may be out of date.
Gun.obj.empty({}) is just a utility to check if an object is empty or not. The 2nd parameter lets you pass it a property to ignore (like '_' which every GUN node has on it in the JS implementation).
You are correct though, that gun.get('list').put(null) should "clear out" the list, such that when you go to save data to it again (or call .set(item) to add an item) this should force/cause GUN to generate a new list/table/set/collection.
It is wrong behavior for it to "resurrect" the old list (unless another peer was trying to at the same time re-write to the old list at the same parent context), so this should be considered a bug that needs to be fixed. (Probably due to the old list ID being cached and not cleared from memory properly)

Splice function not working, Actionscript 2

I'm trying to remove an element from an array. For some reason this isn't working.
MyArray = ["Fus", "Ro", "Dah", "Blah"];
MyArray.splice[2,1];
trace(MyArray); // returns ["Fus", "Ro", "Dah", "Blah"], splice did nothing to the array.
I'm sure I've missed something completely obvious but I've searched multiple forums and they all say that code is correct, but it's somehow not functioning at all.
You should try MyArray.splice(2,1);
Note the brackets! splice() is a function so () has to be used.

character manipulation?

the idea is to take a word and sub out all specified letters for another letter.
Any help on how to make this kind of function work?
The last array_push is not being called because you're returning before. Change it to:
array_push($stepsinchain, $subed);
return $subed;
Since $subed is never stored in the $stepsinchain array, due to the return being before, you're not able to access previous alternations.
array_push is also slower and not recommended when entering one element in an array. Instead, use
$stepsinchain[] = $subed;
It is also much faster as documented at http://www.php.net/manual/en/function.array-push.php#Hcom83388

Passing a block to .try()

This question is about using the .try() method in rails 3 (& a bit about best practice in testing for nil also)
If I have a db object that may or may not be nil, and it has a field which is eg a delimited string, separated by commas and an optional semi-colon, and I want to see if this string contains a particular sub-string, then I might do this:
user_page = UserPage.find(id)
if user_page != nil
result = user_page.pagelist.gsub('\;', ',').split(",").include?(sub)
end
So, eg user_page.pagelist == "item_a,item_b;item_c"
I want to re-write this using .try, but cannot find a way of applying the stuff after pagelist
So I have tried passing a block (from this link http://apidock.com/rails/Object/try)
user_page.try(:pagelist) {|t| t.gsub('\;', ',').split(",").include?(sub)}
However, the block is simply ignored and the returned result is simply pagelist
I have tried other combinations of trying to apply the gsub etc part, but to no avail
How can I apply other methods to the method being 'tried'?
(I dont even know how to phrase the question in an intelligible way!)
I think you're probably coming at this wrong - the quick solution should be to add a method to your model:
def formatted_pagelist
(pagelist || "").split(/[;,]/)
end
Then you can use it where ever:
user_page.formatted_pagelist.include? sub
Strictly speaking, I'd say this goes into a decorator. If you're not using a decorator pattern in your app (look into draper!), then a model method should work fine.