Skip to content
Advertisement

Tag: arrays

Javascript ascii string to hex byte array

I am trying to convert an ASCII string into a byte array. Problem is my code is converting from ASCII to a string array and not a Byte array: This results in: But what I am looking for is: How can I convert to a byte rather than a byte string ? This array is being streamed to a USB

Sort array on key value

I have a function which sorts by name currently and an array of value / key pairs. I wonder how can I pass the key on which sort is being performed so I can call the same function every time like so: Answer [edit 2020/08/14] This was rather an old answer and not very good as well, so simplified and

Javascript reduce() on Object

There is nice Array method reduce() to get one value from the Array. Example: What is the best way to achieve the same with objects? I’d like to do this: However, Object does not seem to have any reduce() method implemented. Answer What you actually want in this case are the Object.values. Here is a concise ES6 implementation with that

Javascript call() & apply() vs bind()?

I already know that apply and call are similar functions which set this (context of a function). The difference is with the way we send the arguments (manual vs array) Question: But when should I use the bind() method ? jsbin Answer I created this comparison between function objects, function calls, call/apply and bind a while ago: .bind allows you

How to select all other values in an array except the ith element?

I have a function using an array value represented as How can I select all other values in an array except this one? The purpose of this is to reset all other Google Maps images to their original state but highlight a new one by changing the image. Answer Use Array​.prototype​.splice to get an array of elements excluding this one.

2-dimensional array to object (JavaScript)

I have an array, that holds a large number of two-dimensional arrays: I need to convert this array into an object of the following form to send it as JSON: (“s_id” should be myArray[0][0], “t_id myArray[0][1], and “type” myArray[0][2] and so on.) How can I get the array in the desired form? Thanks in advance. Answer Be aware that map

Javascript – sort array based on another array

Is it possible to sort and rearrange an array that looks like this: to match the arrangement of this array: Unfortunately, I don’t have any IDs to keep track on. I would need to priority the items-array to match the sortingArr as close as possible. Update: Here is the output I’m looking for: Any idea how this can be done?

Advertisement