When I push the index of itemList in list 'a'.
Right in for loop, it works fine.
But when I push the index in if statement, it doesn't work.
I don't know what's secret exists behind the coffee-script engine.
a=[]
for item, index of itemList
a.push(index)
if item.isExchanged is true
a.push(index)
Some guesses:
for item, index of itemList
So, item takes in its turn each property name/array index. So how could item.isExchanged be true:
if item.isExchanged is true
# ^^^^
# string object
Don't you have simply swapped item <-> index:
for index, item of itemList
# ^^^^^^^^^^^
This will iterate through your object/dictionary key value pairs.
In the other hand, if itemList is an array you should write:
for item, index in itemList
# ^^^^^^^^^^^ ^^
Notice that item and index are in the same order as in our original question. But I used the keyword in there.
Related
I understand Set Collection is key-value and Keys are not duplicated.
In the example below, I thought fruits were the key.
however .firstIndex(of: ) is exist why?
So can a second index exist?
Am I misunderstanding the set?
var favoriteFruits: Set = ["Banana", "Apple", "Orange", "Orange"]
favoriteFruits.insert("WaterMelon")
print(favoriteFruits)
favoriteFruits.remove("Banana")
print(favoriteFruits)
if favoriteFruits.contains("Tomato") {
print("Tomato is my favorite Fruits")
} else {
print("Tomato is not my favorite Fruits")
}
print(favoriteFruits.firstIndex(of: "Orange"))
It would be of great help if you leave a comment.
If you check firstIndexOf method's explanition you will see a defition :
Available when Element conforms to Equatable.
Sets conforms to Equatable.You can also test this while checking the equality of the two sets.Below code doesn't give any error.
if set1 == set2{
//....
}
You ask .firstIndex(of: ) is exist why?
So there is no obstacle for the collection type of Set to use the firstIndexOf method
If you ask why set is equatable.Set uses the hash table to check the elements inside. So the Set must be hashable.An object that conforms to Hashable must also be Equatable too
This is a consequence of Set<String> conforming to Collection<String>. The Collection protocol requires this method:
func firstIndex(of element: Self.Element) -> Self.Index?
The associated types Element and Index are also required by the Collection protocol. Even if Set is not index-based, Set does declare a nested struct called Index. See its implementation here.
It is true that there can be no "second" index for a particular element, since elements in a set are unique. firstIndex(of:) and lastIndex(of:) for any element for a set would return the same thing.
However, the Collection protocol does not care. After all, functions that can take any Collection does not know what kind of collection they are working with, e.g.
func someGenericFunction<C: Collection>(collection: C) {
// ...
}
so they need to specify whether they want the first, last, or the nth index of a particular element, so that their code works for all kinds of collections. If Set did not have these methods, then Set is not a Collection, and cannot be passed to these functions.
How would i go about picking the array within the nested object as shown in the below image:
I can pick up section_information as below.
cur = collection.find_one({"filename":"172977114"}, {"section_information": 1})
But I am not sure or unable to fetch row_cells from each object (0_0,1_0...) from section_information.
find_one() returns a dictionary, in which section_information is a list, so you can iterate that list, then traverse the dictionary items to get the values you need.
for section_information in cur.get('section_information', {}):
for k, v in section_information.items():
print(v.get('row_cells'))
I have a list with with an array of Objects added with this code:
_selectedItems.add([color, size, type]);
Each time I update the quantity of an item I have to call a method to verify if the list already have that item (if yes, update the quantity. if not, add the item and the quantity). So I tried this code:
print(_selectedItems.contains([color, size, type])); // searching if this product is in the list
But it always return false. I tried too verify the id's of each Object, but it returns false too. What I have to do to be able to reach the goal?
You can use .indexWhere() to find an object in a list and return the index:
Item _objectToInsert = Item();
int index = _selectedItems.indexWhere((item) => item.color.contains(_objectToInsert.color));
If index is -1, the object isn't in the list, else index will be the position of the obect in the list. So you can do:
if (index == -1) // add
_selectedItems.add(_objectToInsert);
else // update
_objectToInsert[index].color = _objectToInsert.color;
This page describes how to retrieve an item, (immediate) child items and searching in Sitecore9 using the RESTful API (via PostMan).
What it doesn't appear to say is how to combine those queries.
I would like to search the children of an item which is specified by path. So, currently, I have this returning an item:
GET https://{{sitecorehost}}/sitecore/api/ssc/aggregate/content/Items('{{sitecorehome}}/banners-tests')?sc_apikey={{sitecore-master-apikey}}
I also have this returning the child items of that item:
GET https://{{sitecorehost}}/sitecore/api/ssc/aggregate/content/Items('{{sitecorehome}}/banners-tests/Subcontent/Image and Texts')/Children?sc_apikey={{sitecore-master-apikey}}
However, because the children are not immediate children - they are two levels down at /Subcontent/Image and Texts - I cannot request them. Yes, I could search for them, but then any items would come back with the matching criteria and I only want to search items under that particular path.
I would like something which, I imagine, would look something like this:
GET https://{{sitecorehost}}/sitecore/api/ssc/aggregate/content/Items?sc_apikey={{sitecore-master-apikey}}&$filter=Name eq 'banner' and Path eq 'banners-tests'
Or perhaps this:
GET https://{{sitecorehost}}/sitecore/api/ssc/aggregate/content/Items('{{sitecorehome}}/banners-tests')/Children?sc_apikey={{sitecore-master-apikey}}&$filter=Name eq 'banner'
But these do not work.
#Matt We can do filtering based on the item path. For example, considering item path as :
'sitecore/content/home/tenant1/Subcontent/Image and Texts/neededitem' - the one needed
'sitecore/content/home/tenant1/Subcontent/Image and
Texts/item1/neededitem/notneededitem' - the one we need to exclude
since '/' is not a valid character in the Sitecore item name and indicates the children of the needed item.
Hence, it can be used as a filter in javascript.
So we can split by 'Image and Texts' and then find the items.
For example, consider an array of results and let us say object with a collection of items is items and item path of each item is denoted by Path(let's say, this can be some other property as well) property
let items = [{
Path: 'sitecore/content/home/tenant1/Subcontent/Image and Texts/neededitem',
anotherProperty: 'text-val1'
}, {
Path: 'sitecore/content/home/tenant1/Subcontent/Image and Texts/item1/neededitem/notneededitem',
anotherProperty: 'text-val2'
}];
const results = items.filter(item => {
const splittedPath = item.Path.split('Image and Texts');
if (splittedPath[1].split("/").length <= 2) {
return item;
}
});
console.log(results);
In case your SSC controller (C#) is custom one and having access to Sitecore Context object or Sitecore APIs then the GetChildren() method of Item class will bring children of first level only.
I hope this helps.
I'm trying to prep my Data Model so it can be used in a UITableView with sections.
var folderHolder: [String: [String]]?
folderHolder = ["Projects": ["All", "Recent"], "Smart Folders": ["Folder 1", "Folder 2", "Folder 3"]]
How can I access the keys and objects in this dictionary via an index (as needed by the UITableView)
I tried this in the playground and got stuck. Thank you for your help with this.
// Need number of Keys
// Expected result: 2
folderHolder!.count
// Need number of elements in Key
// Expected: All and Recent are in Projects, so 2 would be expected
folderHolder!["Projects"]
folderHolder!["Projects"]!.count
// How can I get this result by stating the index, e.g. writing 1 as a parameter instead of "Smart Folders"
folderHolder![1]!.count
// Need specific element
// Input parameter: Key index, Value index
// Expected: "Folder 2"
folderHolder![1]![1]
// I don't know why it only works when I state the key explicitly.
folderHolder!["Smart Folders"]![1]
Screenshot with Playground results
The way that dictionaries are set up, you cannot index them in the same way that you would index an array. Due to the Key: Value nature of dictionaries, the order is not important, and thus subscripting like so: folderHolder[1] will not work. Indexing like that would only work in an array, where the order is important and thus maintained.
The Swift Documentation here states that:
A dictionary stores associations between keys of the same type and values of the same type in a collection with no defined ordering. Each value is associated with a unique key, which acts as an identifier for that value within the dictionary. Unlike items in an array, items in a dictionary do not have a specified order.
Found out the solution after a bit more research:
The Dictionary keys need to be converted into an array. The array items can be accessed via an index (the section of the UITableView) and return the name of the Key. And the name of the key can be used to access the Value of the Dictionary (the row of the UITableView).
Here the correct playground data as a reference:
var folderHolder: [String: [String]]?
folderHolder = ["Projects": ["All", "Recent"], "Smart Folders": ["Folder 1", "Folder 2", "Folder 3"]]
let folderHolderArray = Array(folderHolder!.keys)
// Need number of Keys
// Expected: 2
folderHolder!.count
folderHolderArray.count
// Need number of elements in Key
// Expected: All and Recent are in Projects, so 2 would be expected
folderHolder!["Projects"]
folderHolder!["Projects"]!.count
// How can I get this result by stating the index, e.g. writing 1 as a parameter instead of "Smart Folders"
folderHolderArray[1]
// Need specific element
// Input parameter: Key index, Value index
// Expected: "Folder 2"
//folderHolder![1]![1]
let folderHolderSection = folderHolderArray[1]
let folders = folderHolder![folderHolderSection]
let folder = folderHolder![folderHolderSection]![1]