Get index of object in an array - actionscript

I'm building a plugin and I'm using actionscript to save the selection of an object in an illustrator document and then reference it later.
var arrObj:Array=new Array();
arrObj.push(app.activeDocument.selection[0]);
If I select now the same object in the document and check if its in the array it returns a -1 for the index value.
var id:int=arrObj.indexOf(app.activeDocument.selection[0]);
trace (id); //-1
Why is the selection not considered the same object as in that of the array?

I figured out a work around for saving the selected objects in an array and when selecting the object again in the illustrator document it would point out the index of that object in the array. Selected objects datatypes are "PathItems" and have a variable called name. All you have to do is set this variable to a value of your choice as well as saving it in an another array.
var arrObj:Array=new Array();
var nameHold:Array=new Array();
arrObj.push(document.selection[0]); // save the selection in an array
var hold:PathItem=document.selection[0];
hold.name="index1"; // setting the name variable of the selected object to a value of choice
nameHold.push(hold.name); // adding the name value in an array
Now the selected object and its corresponding name value are stored in arrays at the same index...you can compare all "PathItems" to each other by using the name variable and if the names match then you can get the index by using the .indexOf("name") method in arrays.

Related

Qmetry- How to store and access Array of array list of strings

In Qmetry,Trying to save array of arraylist string and access same in another test case.
Array of array list :
ArrayList<ArrayList<String>> my_list
store(my_list, "array_list_1");
//Accessing saved list
Object list_details = getBundle().getObject(array_list_1);
System.out.println("++++ saved list details++++" + list_details);
I am able to print list_details content. Till this there is no issue. But when I try to get the first index within the arraylist, not able to use .get(0) method. Below is the code.
ArrayList<String> list_details1 = list_details.get(0);
When tried typecasting , got an error 'java.lang.String cannot be cast to java.util.ArrayList'
typecast:
ArrayList<ArrayList<String>>list_details1 = (ArrayList<ArrayList<String>>)list_details;
Need to know is it the right way to store and access arraylist ? Please suggest.
if you want store in file, you can use .csv or .xls to save ArrayList<ArrayList> my_list, if ArrayList<ArrayList> my_list you query from database, you can set as static variable, then you can use in different method

Best way to store and refrence lots data in Swift for use in a UITableView

This is a bit confusing so I apologise. But:
I am trying to make an app where the user has the ability to add items to a list of individual items that will be displayed back to them(Something along the lines of a todo list app) as a table view.
However, I have hit a roadblock I need to store several different bits of data for each item(Some Strings, Some ints and a date) in the list.
I think that a class(or struct) would be the best way to do this where an instance of the class holds the information need for each item and then the name of that instance is stored in a list so it can be accessed via the indexPath in the table view.
However, I don't know how I am going to make a new instance of the class for every item because the app could have hundreds of individual items.
I'm sorry, this is so confusing and any help would be appreciated! Feel free to ask for more info
Edit: what I am looking for and I'm sure there's a stupidly easy way of doing it but I'm try to work out how to create an instance of a class when the name of the class is stored in a variable. Ecencialy I want the instance of the class to store the item. To be created when the user inputs the item to be added to the table.
Eg. They enter an item. item1 and the other data that goes along with then I want to be able to store that in instance of the item class but I don't know how to make the name of that instance because the name I want which is item 1 is stored in a variable.
Sorry that's so confusing that's the reason I need help
So first: You can't store the Name of a Class in a Variable and use this variable to get a new instance of a class.
What you need is an Array containing all the different Items. This array is unlimited so you can store as many items in it as you like and you don't need to set a name for each of these Instances.
First create an empty array containing all Items as a property:
var items : [Item] = []
Second call the function populateItemArray() in your viewDidLoad():
private func populateItemArray() {
// Populate the items Array (Are you using CoreData, Realm, ...?)
}
Third, use the Item Array to populate the TableView.
REMEMBER: If your only using one section in your tableView, indexPath.row is always equal to the corresponding item in the array.
E.G. items[indexPath.row]
Hope this helps you!
UPDATE:
Look at this example struct Item. As you can see you can also store a date in it:
struct Item {
// First create all the properties for your struct.
var data1: String
var data2: Int
var data3: String
// You can create properties of any type you want, also of type date.
var dateOfCreation : Date
// Then implement all the methods of your custom Struct Item.
func setDate(with date : Date) {
self.dateOfCreation = date
}
func returnDate() -> Date {
return self.dateOfCreation
}
}

Adding a Value to Collection Items

I want to load data from many files. Each file is named with a date and I need to inject this date to each of the fetched Entries of my file.
I know I could do this with an foreach - loop before inserting the data into the collection, but I think there should be a better solution.
Content of one file
[{"price":"95,34","isin":"FR0000120073"},{"price":"113,475","isin":"CA13645T1003"}]
The Code I use to move the data into a collection.
$collection= collect(json_decode(File::get($file)));
I tried for example the "map" method, however I don't know how to pass an additional variable to the anonymous function.
The content of my collection should look like this:
[{"price":"95,34","isin":"FR0000120073","date":"2016-06-23"},{"price":"113,475","isin":"CA13645T1003","date":"2016-06-23"}]
Is there any simple solution using the collections or do I have to use a foreach-loop?
May be this will help
$collection = collect(json_decode(File::get($file)));
$collection = $collection->each(function ($item, $key) {
//First iteration of $item will be {"price":"95,34","isin":"FR0000120073"}
$item->date = "2016-06-23"; //Insert key, value pair to the collection
});

Keep a session variable value after it has been cleared?

Question background:
I have a session object that is used to store a list of object called 'CartItems'. I convert this object to an actual instance, set it to another List variable then finally clear the list. This is then sent to to a ViewBag variable and sent to a View.
The issue:
What I'm trying to do may not be possible but currently as soon as I clear the list instance of CartItems all references to this are lost aswell. Please see the following code:
public ActionResult Complete(string OrderId)
{
//Retrieve the CartItem List from the Session object.
List<CartItem> cartItems = (List<CartItem>)Session["Cart"];
//Set the list value to another instance.
List<CartItems>copyOfCartItems= cartItems;
//Set the ViewBag properties.
ViewBag.OrderId = OrderId;
ViewBag.CartItems = copyOfCartItems;
//Clear the List of CartItems. This is where the **issue** is occurring.
//Once this is cleared all objects that have properties set from
//this list are removed. This means the ViewBag.CartItems property
//is null.
cartItems.Clear();
return View(ViewBag);
}
Can I store this value without losing it after clearing the List?
When you do
ListcopyOfCartItems= cartItems;
You are creating a another variable by the name of copyOfCartItems that points to the same object cartItems. In other words cartItems and copyOfCartItems are now two names for the same object.
So when you do cartItems.clear(); you are clearing all the list items on the base object.
To get around this, make a copy of cartItems, rather than creating a reference
List<CartItems> copyOfCartItems = new List<CartItems>();
cartItems.ForEach(copyOfCartItems.Add); //copy from cartItems
If you want to clear Session["Cart"], use Session.Remove("Cart")

JQgrid: Get Json data

Is there any method I can get the full JSON data from jqGrid when datatype is local along with the column name?
In this format: [{"firstname": "Chris"},{"firstname": "Dave"}]
You can use the getRowData method to return an array containing all of the grid data. For example:
var fullData = jQuery("#myGrid").jqGrid('getRowData');
Here is the documentation for getRowData from the jqGrid wiki, which helps explain what is going on:
getRowData
Parameters: rowid or none
Returns: array
Description:
Returns an array with data of the requested id = rowid. The returned array is of type name:value, where the name is a name from colModel and the value from the associated column in that row. It returns an empty array if the rowid can not be found.
If the rowid is not set the method return all the data from the grid in array
Update
You might also be interested in the data parameter, which can be used to retrieve the data passed to the grid. From the jqGrid docs:
An array that stores the local data passed to the grid. You can directly point to this variable in case you want to load an array data. It can replace the addRowData method which is slow on relative big data
For example:
var data = $('#' + gridid).jqGrid('getGridParam', 'data');
Does that help?

Resources