How to converting an array to a dictionary in Javascript (also re-order columns into rows)?

javascript array
javascript sort two dimensional array numerically
store html table values in a javascript array
javascript sort multidimensional array alphabetically
python dictionary
store values in dictionary python
how to save data from html table to database using javascript
javascript custom sort array of objects

I have the following array of arrays:

[
  [o1, c1, h1, l1 ,t1],
  [o2, c2, h2, l2, t2],
  ...
  [oN, cN, hN, lN, tN]
]

I assume by elegant you mean easier-to-read code and not necessarily faster performance-wise. This is where map() comes in handy because it reduces the clunky for loop into one single line.

function convertData(rawDataArray) {
    return {
        open: rawDataArray.map(i=>i[0]),
        close: rawDataArray.map(i=>i[1]),
        high: rawDataArray.map(i=>i[2]),
        low: rawDataArray.map(i=>i[3]),
        timeSignature: rawDataArray.map(i=>i[4])
    }
}

Sophisticated Sorting in JavaScript, Using sort it's possible to organise arrays, not just into alphabetical or numerical order, but We can also create a comparison function that produces a dictionary sort, Note how we pre-convert each of the strings to lower-case, which For example, use value [0] for primary sort, but if the two values are  A common pattern in in Javascript is to cast arrays of objects into a separate object, with a certain property defined as the new object’s key. For example, instead of [{id: 'abfq-f39f', name…

let row =[
  ["o1", "c1", "h1", "l1","t1"],
  ["o2", "c2", "h2", "l2", "t2"]
]
const extractIndex = (ind,data)=> data.map(e=>e[ind]) 
const convertDataNew = (rawDataArray)=> {
  return {
    open: extractIndex(0,rawDataArray),
    close: extractIndex(1,rawDataArray),
    high: extractIndex(2,rawDataArray),
    low: extractIndex(3,rawDataArray),
    timeSignature: extractIndex(4,rawDataArray)
  }

}


console.log(convertDataNew(row))

Store HTML Table Values in a Javascript Array and Send to a PHP , For example, the columns in the sample table will have index values of 0 – 3. Converting a Javascript array into JSON format is easy to do with jQuery-json "​task" : $(tr).find('td:eq(3)').text() } }); TableData.shift(); // first row will be empty - so remove return TableData; } How to send it to database(Mysql)?. Example 1: Changing the DataFrame into numpy array by using a method DataFrame.to_numpy(). Always remember that when dealing with lot of data you should clean the data first to get the high accuracy. Although in this code we use the first five values of Weight column by using .head() method.

You could take the keys and use them as part of the property accesssor for pushing the value.

function convertData(rawDataArray) {
    var keys = ['open', 'close', 'high', 'low', 'time'],
        result = Object.assign(...keys.map(k => ({ [k]: [] })));

    rawDataArray.forEach(a => keys.forEach((k, i) => result[k].push(a[i])));
    return result;
}

Using dictionaries to store data as key-value pairs, The dictionary stores objects as key-value pairs and can be used to represent Both the dictionary and list are ubiquitous for representing real-world data. as keys when manually creating dictionary objects and converting real-world Note​: Unlike lists, iterating through a dictionary will not happen in a predictable order. An array-like or iterable object to convert to an array. mapFn Optional Map function to call on every element of the array. thisArg Optional Value to use as this when executing mapFn. Return value. A new Array instance. Description. Array.from() lets you create Arrays from: array-like objects (objects with a length property and indexed elements

Dictionary Data Structures in Python 3, Dictionaries map keys to values, making key-value pairs that can then store data. Typically used to hold data that are related, such as the information contained in In addition to the curly braces, there are also colons ( : ) throughout the dictionary. Looking at the output, the order of the key-value pairs may have shifted. To transpose NumPy array ndarray (swap rows and columns), use the T attribute (.T), the ndarray method transpose() and the numpy.transpose() function.. With ndarray.transpose() and numpy.transpose(), you can not only transpose a 2D array (matrix) but also rearrange the axes of a multidimensional array in any order.

array_column - Manual, In order for protected or private properties to be pulled, the class must The column to use as the index/keys for the returned array. The value is cast as usual for array keys (however, objects supporting conversion to string are also allowed). function dictionaryFilterList(array $source, array $data, string $​column) : array Input :array('k', [45, 23, 56, 12]) Output :[45, 23, 56, 12] Explanation: the array with elements [45, 23, 56, 12] are converted into list with the same elements. Approach to the problem: We want to convert an array into an ordinary list with the same items. For doing so we need to use a function

array_combine - Manual, Creates an array by using the values from the keys array as keys and the Illegal values for key will be converted to string. See Also ¶ Then I ran into a problem when you have empty columns at the end of a row you can switch the array_combine around, to where the vars are the keys, and this will output correctly. Given a list, write a Python program to convert the given list to dictionary such that all the odd elements have the key, and even number elements have the value. Since python dictionary is unordered, the output can be in any order.

Comments
  • if you are looking for perfomances, your code is maybe the best solution to that problem, because accessing array by positions has complexity O(1). If you are looking for compactness and/or readability, you could use some map or reduce instead
  • I have the impression that methods such as .map are designed to be performance optimizations as well. Is that not the case? By the way, I accepted the .map based answer.
  • Thanks. I have two answers. Very difficult decision. I think I will go with @fjch1997's answer.
  • Thanks, that is an interesting solution as well.
  • I don't get it. k isn't defined in the lambda expression a => k.forEach(...)
  • sorry. it should be keys.