Convert array to object keys
try with Array#Reduce
const arr = ['a','b','c'];
const res = arr.reduce((acc,curr)=> (acc[curr]='',acc),{});
console.log(res)
How to create an object from an Array of key-value pairs?
At the time of writing (2013) JavaScript objects / dictionaries / associative arrays don't have such a constructor natively.
As you said yourself, you can of course build your own function using for instance a functional approach using the reduce
function as explained in one of the other answers. A classic for or newer forEach loop would also work, of course. But there isn't anything built-in.
Edit: It's 2019 and now we have Object.fromEntries, which will give you what you need.
Create an object from an array of keys and an array of values
var keys = ['foo', 'bar', 'baz'];var values = [11, 22, 33]
var result = {};keys.forEach((key, i) => result[key] = values[i]);console.log(result);
How to make array of objects with specific key and value from object?
const convertedArray = Object.keys(initObject).map((x)=> { return {
value : x,
label: initObject[x]
}});
Create array of objects by key from array of objects
You could take an object for and the ship
value as key for the same group. For the result take only the values of the object.
var data = [{ name: 'Malcolm Reynolds', ship: 'Serenity' }, { name: 'Carmen Ibanez', ship: 'Rodger Young' }, { name: 'Zander Barcalow', ship: 'Rodger Young' }, { name: 'Hoban Washburne', ship: 'Serenity' }, { name: 'James Kirk', ship: 'USS Enterprise' }], grouped = Object.values(data.reduce((r, o) => { if (!r[o.ship]) { r[o.ship] = o; return r; } if (!Array.isArray(r[o.ship])) r[o.ship] = [r[o.ship]]; r[o.ship].push(o); return r; }, {}));
console.log(grouped);
.as-console-wrapper { max-height: 100% !important; top: 0; }
How to merge array of keys and arrays of values into an object?
use this one.
var a = ["F", "M"];
var b = ["female", "male"];
var c = ["fa-female", "fa-male"];
var resultArray = [];
for(var i = 0; i < a.length; i++) {
resultArray [a[i]] = [b[i], c[i]];
}
Create an object from an array of objects with custom key values
To get the property name, extract the title
and replace its space-characters with the upper-case character. Then, it's as simple as reduce
-ing into an object:
const input=[{amount:23,bill:47,otherData:null,title:'cool title'},{amount:223,bill:427,otherData:null,title:'cooler title'},{amount:2313,bill:437,otherData:null,title:'super cool title'},{amount:123,bill:147,otherData:null,title:'coolest title'}]
console.log( input.reduce((a, item) => { const { title } = item; const camel = title.replace(/ ./g, chars => chars[1].toUpperCase()); a[camel] = item; return a; }, {}));
How can i create an object keys from array values in javascript?
You can simple use a for-loop
like:
const arr = ['09:00', '09:15', '09:30', '09:45'];
let obj = {};
for (var i = 0; i < arr.length; ++i)
obj[arr[i]] = '';
console.log(obj);
Create an object with key-value pairs from an array
You can use Object.keys to read the name of the key and use the 3rd parameter of array.reduce to get the index of currently processed item.
const myArray = [{value: "test"}, {value: "abc"}, {value: "xyz"}];
//the desired result is this: result = { "value1": "test", "value2": "abc", "value3": "xyz" };
const result = myArray.reduce((agg, item, index) => {
agg[Object.keys(item)[0] + (index + 1)] = item.value;
return agg;
}, {})
console.log(result);
Related Topics
React - User-Defined Jsx Components Not Rendering
Differencebetween Compile and Link Function in Angularjs
Ruby on Rails 4 JavaScript Not Executed
How to Format a Utc Date as a 'Yyyy-Mm-Dd Hh:Mm:Ss' String Using Nodejs
Adding/Removing Items from a JavaScript Object with Jquery
Linguistic Meaning of 'Let' Variable in Programming
How to Check If Function Exists in JavaScript
Google Maps Places API V3 Autocomplete - Select First Option on Enter
How to Set State Inside a Useeffect Hook
How to Prevent the Backspace Key from Navigating Back
Splitting a Js Array into N Arrays
How to Pre-Populate a Jquery Datepicker Textbox with Today's Date
Angular 4 Interceptor Retry Requests After Token Refresh
Javascript:Send JSON Object with Ajax
Reserved Keywords in JavaScript
How to Obtain the Query String from the Current Url with JavaScript