arrays

Object to Array returns undefined

五迷三道 提交于 2021-02-11 14:46:27
问题 Im trying to convert JS Object to an Array but Array after conversion is undefined. I initially have JSON but from what I have read it is automatically parsed into JS Object (when I try to parse it, I get SyntaxError: Unexpected token o in JSON at position 1). Also when I console.log(typeof cityList) I get Object. Initial JSON goes like this: [ { "id": 707860, "name": "Hurzuf", "country": "UA", "coord": { "lon": 34.283333, "lat": 44.549999 } }, { "id": 519188, "name": "Novinki", "country":

Object to Array returns undefined

不问归期 提交于 2021-02-11 14:42:19
问题 Im trying to convert JS Object to an Array but Array after conversion is undefined. I initially have JSON but from what I have read it is automatically parsed into JS Object (when I try to parse it, I get SyntaxError: Unexpected token o in JSON at position 1). Also when I console.log(typeof cityList) I get Object. Initial JSON goes like this: [ { "id": 707860, "name": "Hurzuf", "country": "UA", "coord": { "lon": 34.283333, "lat": 44.549999 } }, { "id": 519188, "name": "Novinki", "country":

How to sort keys in json by another json values

拜拜、爱过 提交于 2021-02-11 14:40:46
问题 I have two jsons. First: here Second: here How to sort second json by first? I want to sort keys and it's values in second json by values in first json. 回答1: You could do something like this var first = ["name0", "name1", "name2"] var second = { {"name2":"xyz", "name0" : "xyz", "name1" : "xyz" } } for( var i = 1; i < Object.keys(second).length; i++ ){ var aux = {} first.forEach(function(element2) { resul[element2] = second[i][element2] }); } 来源: https://stackoverflow.com/questions/54029071

How to categorize list of objects based on a parameter

我们两清 提交于 2021-02-11 14:32:46
问题 I have a list of objects with this definition: type MyObject struct { ID int `json:"id"` Level int `json:"level"` CityID int `json:"city_id"` } I want to categorize them based on CityID in order to get a list of lists where each inner list's items have the same CityID . For example, if I have the following list: [ MyObject {ID: 1, Level: 12, CityID: 7}, MyObject {ID: 2, Level: 15, CityID: 2}, MyObject {ID: 3, Level: 55, CityID: 4}, MyObject {ID: 4, Level: 88, CityID: 7}, MyObject {ID: 5,

Reading from txt file into a struct

本秂侑毒 提交于 2021-02-11 14:29:35
问题 Trying to read from txt file until a semicolon and will store it into a array inside struct. struct Total { char *Name; int *No; }MyCities; This is my struct made the arrays pointers so i can allocate memory later. Depending on the content of the txt. Each line is a entry for the struct. London;15 Oslo;12 vienna;35 Let's say this is the content of txt file, it will read the city name into MyCities.Name and the number after the semicolon into MyCities.No FILE *fp; char line; fp = fopen("City

Is it possible to check if a JS Map contains a value and have it return its key

ε祈祈猫儿з 提交于 2021-02-11 14:24:17
问题 I'm trying to find a way to look into a Map array value to see if it exists if it does return the Map key as a variable. this has stumped me for a bit now as I don't do much javascript. const map = new Map([ ["KEY1", ["dummy1","dummy2","dummy3"]], ["KEY2", ["dummy4","dummy5","dummy6","dummy7"]], ["KEY3", ["dummy8","dummy9"]], ]); so say I have dummy4 as a var I want to look in map and see it there in the array of values with key2 and return the key into a new variable of sting "KEY2" 回答1: I'm

Can there be an efficient way for the dual loops?

拈花ヽ惹草 提交于 2021-02-11 14:22:53
问题 I got a question in exam where I was given an array a a = [9,8,10,2] what I need to do is cross iterate the array on itself and get the concatenation of all the possible elements i.e a*a Once all elements are concatenated in that order, then I need to sum them up. My code is in the snippet: Also tried in PHP function sumIt($a) { $totalSum = 0; $len1 = count($a); for($i = 0; $i < $len1; $i++){ for($ii = 0; $ii < $len1; $ii++) { $totalSum += $a[$i].''.$a[$ii]; } } return $totalSum; } The input

TypeError: Improper input: N=5 must not exceed M=2

霸气de小男生 提交于 2021-02-11 14:19:40
问题 I'm trying to use scipy.optimize.curve_fit with a custom fit function (roughly following this tutorial): # Fit function def fit_function(x, y, x0, y0, A, FWHM): return A*np.exp(1)*4*np.log(2)*((x+x0)**2 + (y+y0)**2)/FWHM**2*np.exp(-4*np.log(2)*((x+x0)**2 + (y+y0)**2)/FWHM**2) # Open image file img = Image.open('/home/user/image.tif') # xdata X, Y = img.size xRange = np.arange(1, X+1) yRange = np.arange(1, Y+1) xGrid, yGrid = np.meshgrid(xRange, yRange) xyGrid = np.vstack((xGrid.ravel(), yGrid

How to use nested for loops in python?

戏子无情 提交于 2021-02-11 14:19:36
问题 I'm trying to create an array based on values from another data frame in Python. I want it to fill the array as such. If x > or = 3 in the dataframe then it inputs a 0 in the array. If x < 3 in the dataframe then it inputs a 1 in the array. If x = 0 in the dataframe then it inputs a 0 in the array. Below is the code I have so far but the result is coming out as just [0] array = np.array([]) for x in df["disc"]: for y in array: if x >= 3: y=0 elif x < 3: y=1 else: y=0 Any help would be much

Sort array and group it with specific value [closed]

只谈情不闲聊 提交于 2021-02-11 14:16:07
问题 Closed. This question needs debugging details. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 3 months ago . Improve this question I have an array with a lot of values looking like this : I'm trying to sort the array by grouping every "part" that have the same part_image value and then render the image the value is linked to only 1 single time. I tried using the groupBy function but without success, does