Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

js create object from array

[
  { id: 10, color: "red" },
  { id: 20, color: "blue" },
  { id: 30, color: "green" }
].reduce((acc, cur) => ({ ...acc, [cur.color]: cur.id }), {})

//output: 
{red: 10, blue: 20, green: 30}
Comment

js create object from array

 const obj = {};

 for (const key of yourArray) {
      obj[key] = whatever;
 }
Comment

create object from array

// create object from array
const createObjectFromArray = (arr) => {
  let obj = {};
  for (let value of arr) {
    obj[value] = ++obj[value] || 1;
  }
  return obj;
};
Comment

PREVIOUS NEXT
Code Example
Javascript :: navlink activestyle not working 
Javascript :: how to check if a date has passed javascript 
Javascript :: es6 functions 
Javascript :: how to detect click outside div 
Javascript :: how to write a javascript function 
Javascript :: how to create a new angular project in visual studio code 
Javascript :: lastindexof() javascript 
Javascript :: add next to react 
Javascript :: jquery recharger la page 
Javascript :: form submit not reaload 
Javascript :: findone mongoose 
Javascript :: react html parser 
Javascript :: jquery once 
Javascript :: logical operators in js 
Javascript :: html canvas not clearing 
Javascript :: axios data fetch 
Javascript :: how to add element to an object 
Javascript :: innerhtml 
Javascript :: node js function infinite parameters 
Javascript :: expres body parser 
Javascript :: node check if internet 
Javascript :: reactjs cdn file 
Javascript :: anchor click event angular refresh page 
Javascript :: object.keys mdn 
Javascript :: code for javascript message box 
Javascript :: .foreach in javascript 
Javascript :: mongoose count 
Javascript :: how to make a check if letters are capital in discord js 
Javascript :: truthy and falsy values in javascript 
Javascript :: add button dynamically in javascript 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =