Convert an array to an even / odd index based object

I am trying to convert an array to an object based on whether the index of the array is odd or even.

For instance,

Input: ["name", "Tom", "age", 20]

output: { "name": "tom","age": 20 }

It can be implemented using basic JavaScript functions such as forEach , map and filter . But I want simpler code.

So, I checked the underscore.js docs, but I could not find a good way. Is there any way to solve this simply?

+5
source share
6 answers

An interesting question, my two cents:

Simple and efficient loop:

 const simpleArray = ["name", "Tom", "age", 20]; // Modifying the Array object Array.prototype.toObject = function() { let r = {}; for(let i = 0; i < this.length; i += 2) { let key = this[i], value = this[i + 1]; r[key] = value; } return r; } // Or as a function const toObject = arr => { let r = {}; for(let i = 0; i < arr.length; i += 2) { let key = arr[i], value = arr[i + 1]; r[key] = value; } return r; } const simpleObjectOne = simpleArray.toObject(); // First method const simpleObjectTwo = toObject(simpleArray); // Second method 
+4
source

You can use Array#forEach and check the index if uneven, and then assign the item to the key from the last item.

 var array = ["name", "Tom", "age", 20], object = {}; array.forEach(function (a, i, aa) { if (i & 1) { object[aa[i - 1]] = a; } }); console.log(object); 

Same thing with Array#reduce

 var array = ["name", "Tom", "age", 20], object = array.reduce(function (r, a, i, aa) { if (i & 1) { r[aa[i - 1]] = a; } return r; }, {}); console.log(object); 
+2
source
 var input = ["name", "Tom", "age", 20] ; var output = {} input.forEach((x, i, arr) => { if (i % 2 === 0) output[x] = arr[i+1]; }); console.log(output); //{ name: 'Tom', age: 20 } 
+1
source

You can also use the abbreviation

 var arr = ["name", "Tom", "age", 20], obj = arr.reduce((p,c,i,a) => (i%2 ? p[a[i-1]] = c : p[c],p),{}); console.log(obj); 

for this operation as follows:

+1
source

Underline Solution:

 output = _.object(..._.partition(input, (_, i) => !(i % 2))) 

_.partition will split the array into [['name', 'age'], ["tom", "20"]] . In other words, it returns an array containing two subarrays - in this case, one array of keys and one array of values. _.object takes an array of keys and an array of values ​​as parameters, so we use ... to pass subarrays to the value returned by _.partition , into it as two parameters.

If you use a very functional semantic code:

 const even = i => !(i % 2); const index = fn => (_, i) => fn(i); output = _.object(..._.partition(input, index(even))) 

Recursive solution:

 function arrayToObject([prop, value, ...rest], result = {}) { return prop ? arrayToObject(rest, Object.assign(result, {[prop]: value})) : result; } 

Iterative solution:

 function arrayToObject(arr) { const result = {}; while (arr.length) result[arr.shift()] = arr.shift(); return result; } 
+1
source

Simple loop

 var arr = ["name", "Tom", "age", 20, "address"]; var obj = {}; for (var i = 0; i < arr.length; i+=2){ //First iteration: assign obj["name"] to "Tom" //Second iteration: assign obj["age"] to 20 //Third iteration: assign obj["address"] to "" because arr[5] does not exist obj[arr[i]] = arr[i+1] || ""; } 
0
source

Source: https://habr.com/ru/post/1259021/


All Articles