javascript array to object with keys
let arr = [{a: 'a', b: 1, c: 'c'}, {a: 'a', b: 2, c: 'c'}, {a: 'a', b: 3, c: 'c'}]:
let mapped = arr.reduce (function (map, obj) {
map[obj.b] = obj;
return map;
},{}); // reduce
console.log (mapped); // {1: {a: 'a', b: 1, c: 'c'}, 2: {a: 'a', b: 2, c: 'c'}, 3: {a: 'a', b: 3, c: 'c'}const object1 = {
a: 'somestring',
b: 42,
c: false
};
console.log(Object.keys(object1));
// expected output: Array ["a", "b", "c"]const convertArrayToObject = (array, key) =>
array.reduce(
(obj, item) => ({
...obj,
[item[key]]: item
}),
{}
);
myObject = {
"key": "value"
}
Object.keys(myObject); // get array of keysconst names = ['Alex', 'Bob', 'Johny', 'Atta'];
// convert array to th object
const obj = Object.assign({}, names);
// print object
console.log(obj);
// {0: "Alex", 1: "Bob", 2: "Johny", 3: "Atta"}
const arr = ['a','b','c'];
const res = arr.reduce((a,b)=> (a[b]='',a),{});
console.log(res)
Also in JavaScript:
- '{ state: any; dispatch: React.Dispatch<{ type: string; payload: any; }>; }' is not assignable to type 'Store'
- timeout httppost angular
- populate DataTable from django json
- alphabet only in jquery
- how pass data from body in koa js
- image slideshow in jquery
- username validation js
- javascript check if elements of one array are in another
- js take last item in array
- The target origin provided ('https://www.youtube.com') does not match the recipient window's origin ('http://localhost')
- javascript add option element to select
- how to add changes every time you route navigate to page in angular
- passing event handler to useEffeect
- how to auto refresh page in javascript
- javascript vs java
- how to clean react native project
- Schema hasn't been registered for model "products"
- how to change package name in react native
- jquery copier dans le presse papier
- node js mongoose text index
- send a message to a specific channel discord.js
- jquery value of input
- js remove special characters
- javascript get the last item in an array