Here's the how I append data to struct:
user.Things = append(user.Things, item.Id)
Now, how can I remove item.id from user.Things? Seems like there's no method like delete, remove, or similar.
For example, this didn't work:
user.Things = append(user.Things[:item.id], user.Things[:item.id + 1:])
The wiki page Slice tricks gives a good overview about operations on slices.
There is also a few ways to delete an elements a slice: cutting, deleting or deleting without preserving order.
In your case, it seems you just had a typo (an extra colon):
user.Things = append(user.Things[:item.id], user.Things[item.id + 1:])
Related
Attempting to confirm that of all the schema in the head of a page exactly 3 of them should have a specific string within them. These schemas have no tags or sub classes to differentiate themselves from each other, only the text within them. I can confirm that the text exists within any of the schema:
cy.get('head > script[type="application/ld+json"]').should('contain', '"#type":"Product"')
But what I need is to confirm that that string exists 3 times, something like this:
cy.get('head > script[type="application/ld+json"]').contains('"#type":"Product"').should('have.length', 3)
And I can't seem to find a way to get this to work since .filter, .find, .contains, etc don't filter down the way I need them to. Any suggestions? At this point it seems like I either need to import a custom library or get someone to add ids to these specific schema. Thanks!
The first thing to note is that .contains() always yields a single result, even when many element match.
It's not very explicit in the docs, but this is what it says
Yields
.contains() yields the new DOM element it found.
If you run
cy.get('head > script[type="application/ld+json"]')
.contains('"#type":"Product"')
.then(console.log) // logs an object with length: 1
and open up the object logged in devtools you'll see length: 1, but if you remove the .contains('"#type":"Product"') the log will show a higher length.
You can avoid this by using the jQuery :contains() selector
cy.get('script[type="application/ld+json"]:contains("#type\": \"Product")')
.then(console.log) // logs an object with length: 3
.should('have.length', 3);
Note the inner parts of the search string have escape chars (\) for quote marks that are part of the search string.
If you want to avoid escape chars, use a bit of javascript inside a .then() to filter
cy.get('script[type="application/ld+json"]')
.then($els => $els.filter((index, el) => el.innerText.includes('"#type": "Product"')) )
.then(console.log) // logs an object with length: 3
.should('have.length', 3);
Does CKEDITOR.htmlParser.element.remove() remove the element and its descendants or does it just remove the element and stuff its immediate children into its place? I ask because I've tried a number of approaches to removing elements from consideration during a filter and have wound up with not quite the results I expect; removing the element seems not to remove everything it ought to, but perhaps it's intended behavior.
If the latter, what is the most efficient way to remove an element and everything inside it? setHtml()?
The CKEDITOR.htmlParser.node.remove removes the node which means also its children are removed.
// Removing `em` element on `setData`.
CKEDITOR.instances.editor1.on( 'toHtml', function( evt ) {
var ems = evt.data.dataValue.find( 'em', true );
console.log( 'Before: ' + evt.data.dataValue.getHtml() );
if ( ems.length ) {
ems[ 0 ].remove();
}
console.log( 'After: ' + evt.data.dataValue.getHtml() );
}, null, null, 14 );
See this codepen demo.
It is hard to speculate on your case without seeing any code, but I assume you are trying to manipulate some copy of the element or partial tree which is not used any further so it may seem this method does not work as expected.
If you provide the code, it will be easier to check if there are any issues with it.
I'm ripping my hair out trying to figure this out.
I have a table being refreshed via WebSocket. It feeds an object with frequency counts. The key is a term. The value is a count. I take this and convert it to an array of arrays, where the top level array contains arrays of the format [key, value]. This works.
I sort and slice the array to get the Top 10. This works.
I feed this 2-dimensional array into d3.
d3.select('#table').select('tbody').selectAll('tr').data(dataArray, (element) => {
return element[0];
});
Now, first of all, I can't print this to peek at the update selection. Not on Chrome. Not on Firefox. No access to the selection data, just the _groups which does not include the exit() elements in the same place so there's no way to verify. But, fine. Whatevs.
I do this:
const enterRows = selection.enter().append('tr')
enterRows.append('td').classed('key', true).text((element) => { return element[0]; });
enterRows.append('td').classed('value', true);
enterRows.merge(selection).select('.value').text((element) => { return element[1]; })
selection.exit().remove();
At this point, the rows aren't ordered. I would expect them to be ordered. But they're not. Fine. Whatever.
selection.order();
Does nothing.
What do I do?
EDIT:
Here's a sample of the array as it goes into data():
[['B': 5], ['C': 3], ['A': 2]]
I can make no sense of the resulting table row order.
I just want to preserve the incoming array order.
enterRows.merge(selection).order() does the trick. No idea why it's necessary since ordering is supposed to be stable, but it does.
Given this hash:
hash1= { node1: { node2: { node3: { node4: { node5: 1 } } } } }
We access inside nodes with square brackets like this:
hash1[:node1][:node2][:node3][:node4]
Now I have a hash that I know will always be nested as it is an XML response from a SOAP webservice, but neither the depth of the hash nor the names of the nodes stay the same. So it would be nice if I could ask the user of my application for the hash depth and store it in a variable. And then be able to do hash1[:hash_depth] and achieve the same result as above.
I have accomplished what I want by the following code:
str = 'node1,node2,node3,node4'
str_a = str.split(',')
hash_copy = hash1
str_a.each { |s| hash_copy = hash_copy.[](s.to_sym) }
hash_copy
=> {:node5=>1}
hash1[:node1][:node2][:node3][:node4]
=> {:node5=>1}
that is asking the user to enter the hash depth separated by commas, store it in a string, split it, make an array, clone the original hash, go down each level and modify the hash till I get to the desired node. Is there a way to do it with the square brackets notation and using a variable to store the depth without modifying the hash or needing to clone it?
Edit:
someone answered with the following (can't see his post anymore???)
hash_depth="[:node1][:node2][:node3][:node4]"
eval "hash1#{hash_depth}"
Although eval does everything you need, there is another approach, since you already have the working code for comma-separated list:
hash_depth="[:node1][:node2][:node3][:node4]"
csh = hash_depth.gsub(/\A\[:|\]\[:|\]\Z/, { '][:' => ',' })
#⇒ "node1,node2,node3,node4"
And now you are free to apply your existing function to csh.
If this is a webapp, I think you should prepare a list of short textareas, which starts with a single text item, and the user can keep adding a new item to the list by clicking on a button. The areas will be filled by the user, and will be sent.
Then, you will probably receive this through some serialized form. You decode this to get an array of strings:
str_a = ["node1", "node2", "node3", "node4"]
and you can reach the inner element by doing:
str_a.inject(hash1){|h, s| h[s.to_sym]} #=> {:node5 => 1}
I currently have a itemsCollection and a historyCollection. Both are separate isolated .dat files.
The saving and displaying works fine for both of these isolated storages, however the problem is when I try to delete something out of the itemCollection I also want to remove all the items out of the historyItemCollection where the historyItemCollection contains that specific itemIndex.
Update:
The itemCollection could contain something like this:
a
b
c
The historyItemCollection could look like this:
a
b
c
b
So if I remove b (itemIndex 1) then I want both to be removed in the historyItemCollection.
I can remove the items out of the itemCollection fine, but the historyItemCollection throws errors.
int i = 0;
for (i = 0; i <= App.ViewModel.historyItemCollection.Count-1; i++)
{
if (App.ViewModel.historyItemCollection[i].VehicleId == (Application.Current as App).vehicleIndex)
{
App.ViewModel.historyItemCollection[i].Remove(i);
}
}
App.ViewModel.vehicleItemsCollection[(Application.Current as App).vehicleIndex].Remove((Application.Current as App).vehicleIndex);
The error message I'm getting:
Parameter name: index
It fails on this line:
App.ViewModel.historyItemCollection[i].Remove(i);
In this line do you really mean both of these to be itemIndex?
App.ViewModel.historyItemCollection[(Application.Current as App).itemIndex].Remove((Application.Current as App).itemIndex);
Also, does calling RefreshItems() have any impact on itemIndex?
I'd recommend breaking your code up into more lines - then single stepping through.
As an aside, why are you setting the DataContext in this method? Then why are you also setting it twice - and to different types of objects each time. This seems unusual - perhaps you could instead set the DataContext globally to a new ViewModel class which has the history and items as children?
The problem is that you're removing items from a list as you iterate forwards through it.
As you delete items from the list you invalidate the count of total items in the list and your relative position.
If you must edit the list this way you should step backwards through it so as not to effect the indexing.
See also How to remove elements from a generic list while iterating over it?