arrays

Sort array by frequency

我的未来我决定 提交于 2021-02-17 02:51:05
问题 I would like to count how many times is every string in array and sort them by number of times they exist in array I have this code now hlasy.sort(); var zaciatok = null; var pocet = 0; for (var i = 0; i < hlasy.length; i++) { if (hlasy[i] != zaciatok) { if (pocet > 0) { console.log(zaciatok + ' má ' + pocet + ' hlasov'); } zaciatok = hlasy[i]; pocet = 1; } else { pocet++; } } if (pocet > 0) { console.log(zaciatok + ' má ' + pocet + ' Hlasov'); } it works, but it outputs strings from array

Array.Filter not updating array

北战南征 提交于 2021-02-17 02:39:06
问题 The task is: You will be provided with an initial array (the first argument in the destroyer function), followed by one or more arguments. Remove all elements from the initial array that are of the same value as these arguments. While working through it I found some Array.filter behaviour I'm struggling to understand: function destroyer(arr) { for (var i = 1; i<arguments.length; i++){ toDelete = arguments[i]; arr.filter(isItIn); } return arr; } function isItIn(item, undefined, array){ return

How to sort object array by time in javascript

时间秒杀一切 提交于 2021-02-17 02:38:07
问题 I have a object array looking something like this: [0] = { transportnumber: '45', time: '10:28:00', date:"2017-01-16"} [1] = { transportnumber: '45', time: '10:38:00', date:"2017-01-16"} [2] = { transportnumber: '45', time: '10:48:00', date:"2017-01-16"} [3] = { transportnumber: '14', time: '10:12:00', date:"2017-01-16"} [4] = { transportnumber: '14', time: '10:24:00', date:"2017-01-16"} [5] = { transportnumber: '14', time: '10:52:00', date:"2017-01-16"} The object array will always look like

indexeddb put not update a value, it creates a new (id)

社会主义新天地 提交于 2021-02-17 02:36:14
问题 I have a table with form, surname, lastname, email and a rectangle. I have to insert, in the rectangle, an array with arrays with points of timelines etc. I create a customer with form, surname, lastname and email, add them to indexeddb, load them later to insert the rectangle-array. After that, I want to put the newobjectstore in the indexeddb where the email is the same from my customer I choose/inserted. But with this code my array will be put in a new Objectstore with its own ID. function

OutOfBounds when working with objects in an array

时间秒杀一切 提交于 2021-02-17 02:10:59
问题 When I am creating an array of Example objects, I call something like initializeArray(); I use a simple nested for loop to traverse through the array and then assign new objects with values to each index of the array using exampleArr[i][j] = new Example(false, false, false, 0); however, calling this gives me an java.lang.ArrayIndexOutofBoundsException:0 at the line above. I am assuming that I am instantiating the new object incorrectly, as this also happens in another method which is supposed

Sorting Parallel Arrays

只愿长相守 提交于 2021-02-17 02:08:24
问题 Beginner in Java using an old textbook and Head First: Java books to figure some stuff out. I have three arrays all parallel. I need to be able to sort by Title, Author, or Page Count based on user selection. I can sort one using Arrays.sort() but I'm getting hung up on how to sort the other two arrays to correspond to the new sorted one. Say I sort the BookTitle array, but I'm going to need to display the proper author and page count of its respective arrays and I'm stumped. do { entry =

ValueError: cannot reshape array of size 7267 into shape (302,24,1)

风格不统一 提交于 2021-02-17 02:07:30
问题 I am reshaping a 1D array into 3D using the following. It works fine but it throws an error when x is 7267. I understand that it is not possible to slice an odd number as an int without losing some values. Would appreciate any solution to this. code x = 7248 y= 24 A = np.arange(x) A.reshape(int(x/y),y,1).transpose() output array([[[ 0, 24, 48, ..., 7176, 7200, 7224], [ 1, 25, 49, ..., 7177, 7201, 7225], [ 2, 26, 50, ..., 7178, 7202, 7226], ..., [ 21, 45, 69, ..., 7197, 7221, 7245], [ 22, 46,

Sorting Parallel Arrays

旧城冷巷雨未停 提交于 2021-02-17 02:06:46
问题 Beginner in Java using an old textbook and Head First: Java books to figure some stuff out. I have three arrays all parallel. I need to be able to sort by Title, Author, or Page Count based on user selection. I can sort one using Arrays.sort() but I'm getting hung up on how to sort the other two arrays to correspond to the new sorted one. Say I sort the BookTitle array, but I'm going to need to display the proper author and page count of its respective arrays and I'm stumped. do { entry =

Rank and unrank permutations with just one cycle

三世轮回 提交于 2021-02-17 02:04:55
问题 I want to rank and unrank permutations with one cycle in lexicographical order with a given len. A permutation with one cycles is where you can visit in this cycle each element. p:= (2,3,1) is a permutation with one cycle. Has rank 1. p:= (3,1,2) has 1 cycle too, but rank 2, because the permutation is lexicographical greater the frist so it becomes a greater rank. p:= (1,2,3) is a permutation with 3 cycles. (1),(2),(3) How can I efficently rank (permutation with one cycle to rank) and unrank

counting how many times an item appears in a multidimensional array in javascript

送分小仙女□ 提交于 2021-02-17 02:03:11
问题 Given a multidimensional array like this: var arr = [ "apple", ["banana", "strawberry","dsffsd", "apple"], "banana", ["sdfdsf","apple",["apple",["nonapple", "apple",["apple"]]]] ,"apple"]; the challenge was to write a function that keeps the array and an item as arguments and return how many times that item appears in the array. My first solution did the trick: function countItems(arr, item, sumarr = []) { // sumarr = array to store items matched for (let i = 0; i < arr.length; i++ ){ //