[Solved] Rails: Remove Curly Braces in Array [closed]

@temp = SalesOrder.where(“status > ?”, 0).ids items = SalesOrderItem.where(sales_order_id: @temp).where.not(product_id: nil) total = items.to_a.group_by(&:product_id).each_with_object({}) do |(product_id, quantity), total| total[Product.find(product_id.to_i).name] = quantity.map(&:quantity).map(&:to_f).sum end @top_five = total.sort_by { |k, v| v }.reverse! Check this. It should work. If any errors, ping me, I will update it PS: your code is not optimized at all. All of this … Read more

[Solved] I need to compare strings in an array and store/return the longest word [duplicate]

Your post isn’t javascript, but here’s basically what you want to do. It should be easy to change it over from javascript. function compareWords(words){ var longestWord = ”; for(var i = 0; i < words.length; i++){ if(words[i].length > longestWord.length){ longestWord = words[i]; } } return longestWord; } var words = [‘gunslinger’, ‘gundam’, ‘dragon’, ‘shirt’, ‘unicorn’, … Read more

[Solved] When I try to add Strings to an Array List I get an “illegal start of type error”

To add items to a static list you can use the block static{} public class Employee { …. private static ArrayList <String> employeeID = new ArrayList <String> (); static { employeeID.add(“632584”); employeeID.add(“259415”); employeeID.add(“257412”); employeeID.add(“953647”); employeeID.add(“126497”); employeeID.add(“453256”); employeeID.add(“125689”); } … } } Or use constructor public class Employee { …. private static ArrayList <String> employeeID = … Read more

[Solved] need to create new array called result with key/value from posts and inside that reviews array with user and comment key/value objects

Use map with filter: let posts = [{ “id”: 101, “title”: “Some post title” }, { “id”: 102, “title”: “Some Another post title” }, { “id”: 103, “title”: “Some Best post title ever” } ] let reviews = [{ “postId”: 101, “user”: “Chris”, “comment”: “Great post!” }, { “postId”: 101, “user”: “Jason”, “comment”: “Worth reading.” … Read more

[Solved] C# appointing distinct array elements randomly to another array

A simple solution: int[] card1 = nums.OrderBy(it => Guid.NewGuid()).Take(6).ToArray(); Ordering the original array by a new guid should give you a decent approximation of randomness while avoiding the requirement to explicitly guard against duplication. Then you just take the first 6 results. Warning: The random-like behaviour of ordering by guid is an implementation detail of … Read more

[Solved] How to dynamically allocate memory in 2D array and store values? [closed]

As i said problem is in scanning N and M variables. Change scanf(“%d %d”,&N,&N); to scanf(“%d”,&N); scanf(“%d”,&M); and you are fine. Problem was you were reading N twice while didnt read an M which was used uninitialized. Non-static variables (local variables) are indeterminate. Reading them prior to assigning a value results in undefined behavior. You … Read more

[Solved] Sort a class array by a string attribute in Java

For Java 8 use a lambda expression and the Arrays sort function: Users[] someUsers = {user1, user2, user3}; Arrays.sort(someUsers, (a,b) -> a.firstName.compareTo(b.firstName)); For previous versions use a Collection Comparator: Users[] someUsers = {user1, user2, user3}; List<Users> userList = new ArrayList<Users>(Arrays.asList(someUsers)); Comparator<Users> comparator = new Comparator<Users>() { @Override public int compare(Users left, Users right) { return … Read more

[Solved] In JavaScript, what is the simplest way to insert text into a string?

Use: var queryStringParts = queryString.split(‘&’); var pairs = queryStringParts.map(function(str) { return str.split(‘=’) }) var rewrittenParts = pairs.map(function(pair){ return ‘slides[0].’ + pair[0] + ‘=’ + pair[1] }); var newQuerystring = rewrittenParts.join(‘&’); As was pointed out in the comments, in this specific case we could skip the split into pairs step and just do var queryStringParts = … Read more

[Solved] PHP Multidimensional Array Access

Code should look something like this, if you were intending on using strings as your keys. $this_array= array( ‘string_name’ => ‘string’, ‘string_array’ => array( ‘string_key’ => ‘string_val’ ) ); Yes, you will access it by: $this_array[‘string_array’][‘string_key’]; 0 solved PHP Multidimensional Array Access

[Solved] isnt everything where it should be, why the segmentation fault?

Here char *firstName[50]; firstName is array of 50 character pointer, and if you want to store anything into each of these char pointer, you need to allocate memory for them. For e.g for (int counter = 0; counter < 10; counter ++) { firstName[counter] = malloc(SIZE_FIRST); /* memory allocated for firstName[counter], now you can store … Read more