Javascript - convert array of arrays into array of objects with prefilled values
You can simply use Array.prototype.map
to project an array into another one by applying a projection function:
var arrs = [
[1, 2],
[3, 4],
[5, 6],
[7, 8]
];
var objs = arrs.map(x => ({
lat: x[0],
lng: x[1]
}));
/* or, using the older "function" syntax:
var objs = arrs.map(function(x) {
return {
lat: x[0],
lng: x[1]
};
);
*/
console.log(objs);
Converting an array of arrays into an array of objects
Use Array#map.
let array = [
[144, 'test@email.com', '7357'],
[145, 'test2@email.com', '7358'],
[146, 'test3@email.com', '7359'],
];
let result = array.map(([employeeId, WorkEmail, ClubNumber]) => ({employeeId, WorkEmail, ClubNumber}))
console.log(result);
How to convert array of array to array of objects in react
Using For Loop
const x = [
['Lettuce', 60],
['Apple', 80]
]
let arrayOfObjects = []
for(let [name, price] of x){
arrayOfObjects.push({name, price})
}
Using map
const x = [
['Lettuce', 60],
['Apple', 80]
]
let arrayOfObjects = x.map(([name, price]) => ({name,price}))
Run through an array and split values
You can use try using Array.prototype.map():
The
map()
method creates a new array populated with the results of calling a provided function on every element in the calling array.
var data = [[5.76516834507, 50.8474898368], [5.76115833641, 50.8453698247]];var res = data.map(i => ({latitude: i[0], longitute: i[1]}));console.log(res);
How to convert a JavaScript array to an object?
You can use Array.map to build desired format, here i am Destructuring element from each array as date, name, and address
and than returning an object with these key/value
pair
let data = [["2019","abc","xyz"],["2014","DEF","PQR"]]
let op = data.map(([date, name, address]) => ({date, name, address}))
console.log(op)
Assign Keys when converting array to json - Node JS
Use map, then destruct the array and return an object.
const arr = [
[
'James',
23,
'male'
],
[
'Britney',
45,
'female'
]
]
const res = arr.map(([name, age, gender]) => ({
name,
age,
gender
}))
console.log(res);
Convert a 2D array with first rows a headers to object JavaScript
Destructure the array, and take the keys (1st item), and the values (the rest). Map the values array, and then map each sub-array of values, take the respective key by the value, and return a pair of [key, value]
. Convert the array pairs to an object with Object.fromEntries()
:
const fn = ([keys, ...values]) =>
values.map(vs => Object.fromEntries(vs.map((v, i) => [keys[i], v])))
const array = [[ 'combi', 'DQ#', 'sd', 'Level 3', 'Level 6', 'Level 7' ], [ 'DQn DQDC Simple','DQn', 'DQDC', 'Simple', 'Simple_A7', 0.262],[ 'DQn DQDC Simple1','DQn', 'DQDC', 'Simple1', 'Simple_A7', 0.264]]
const result = fn(array)
console.log(result)
Related Topics
Dynamically Update Values of a Chartjs Chart
How to Round a Number in JavaScript? .Tofixed() Returns a String
Deep Copy in Es6 Using the Spread Syntax
What Is the List of Possible Values for Navigator.Platform as of Today
Event Delegation VS Direct Binding When Adding Complex Elements to a Page
How to Copy JavaScript Object to New Variable Not by Reference
How Is () => {...} Different from () =>
Differencebetween Typeof and Instanceof and When Should One Be Used VS. the Other
Semicolon Before Self-Invoking Function
How to Put an Image File in a JSON Object
Fix Node Position in D3 Force Directed Layout
How to Convert JSON Object to JavaScript Array
How to Trigger a Link's (Or Any Element'S) Click Event Through JavaScript
How to Convert String to Number According to Locale (Opposite of .Tolocalestring)
Iterate Over Object Literal Values
How to Filter JSON Data in JavaScript or Jquery
What Does ${} (Dollar Sign and Curly Braces) Mean in a String in JavaScript