Does python have any equivalent to JavaScript’s Array.prototype.some / every? Trivial JavaScript example: Will output: The below python seems to be functionally equivalent, but I do not know if there is a more “pythonic” approach. Answer Python has all(iterable) and any(iterable). So if you make a generator or an iterator that does what you want, you can test it with
Tag: arrays
Javascript: add value to array while looping that will then also be included in the loop
Sorry if this is a dupplicate, can’t seem to find it. I wonder if there is a way (any sort of loop or datatype) so that this will ouput 1 2 3 4 5 during the loop (or in any order, as long as all the 5 numbers are in there) Answer Using Array.prototype.forEach() will not apply the callback to
Sum similar keys in an array of objects
I have an array of objects like the following: I need to add up all the values for objects with the same name. (Probably also other mathematical operations like calculate average.) For the example above the result would be: Answer First iterate through the array and push the ‘name’ into another object’s property. If the property exists add the ‘value’
JavaScript move an item of an array to the front
I want to check if an array contains “role”. If it does, I want to move the “role” to the front of the array. Here, I got the result: [“role”, “email”, “role”, “type”, “name”] How can I fix this? Answer You can sort the array and specify that the value “role” comes before all other values, and that all other
Razor MVC Populating Javascript array with Model Array
I’m trying to load a JavaScript array with an array from my model. Its seems to me that this should be possible. Neither of the below ways work. Cannot create a JavaScript loop and increment through Model Array with JavaScript variable Cannot create a Razor loop, JavaScript is out of scope I can get it to work with But I
Check if object value exists within a Javascript array of objects and if not add a new object to array
If I have the following array of objects: Is there a way to loop through the array to check whether a particular username value already exists and if it does do nothing, but if it doesn’t to add a new object to the array with said username (and new ID)? Thanks! Answer I’ve assumed that ids are meant to be
Create an array with all numbers from min to max without a loop
I have two numbers, min and max, and I want to create an array that contains all number between them (including min and max). The most obvious approach is to use a for loop for this, and push the single values onto an array. Nevertheless, this seems to be a quite naive approach, i.e. it’s imperative programming. Now I was
Javascript: Convert Array to Object
Which is the easiest way to convert this: to this: in order to pass it to AJAX data? I’m using VisualSearch and it returns an array of Facet model instances which i need to convert into an Object. Answer think about the array.reduce function everytime you need to perform these kind of transformations. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
Merging of two arrays, store unique elements, and sorting in jQuery
I am trying to do merge these two arrays and output coming is [1,3,4,5,6,4,5,6] I have used $.merge(Arr1, Arr2); this piece to merge them. Using alert I can see the merged array like above. Now my question is how can I get the following output: [1,3,4,5,6,8,9,10] i.e. the elements should be unique as well as sorted in the same manner
Find and remove objects in an array based on a key value in JavaScript
I have been trying several approaches on how to find an object in an array, where ID = var, and if found, remove the object from the array and return the new array of objects. Data: I’m able to search the array using jQuery $grep; But how can I delete the entire object when id == 88, and return data