Convert ARRAY to OBJECT in javascript - JavaScript Interview Questions
ฝัง
- เผยแพร่เมื่อ 18 ม.ค. 2023
- How to convert Array to Object in JavaScript? Find out here in 1 minute
🤯 Crash Courses (Single Video)
Git/Github Crash Course : bit.ly/3JSA5VT
TypeScript Crash Course : bit.ly/372dZSh
Angular Crash Course : bit.ly/3DoGJR1
Vue JS Crash Course : bit.ly/3uDujRl
Python Crash Course : bit.ly/3Dod7U2
React Router Crash Course : bit.ly/36YfO2i
🧑🏫 Full Course Playlists
HTML : bit.ly/36IMq0h
CSS : bit.ly/3LpRQw6
JavaScript : bit.ly/3u049tf
BootStrap : bit.ly/3NA9nDJ
ES 6 : bit.ly/3DvYCh6
DOM Playlist : bit.ly/35nMKB7
ReactJS (Redux & Hooks) : bit.ly/3iMethN
React with TypeScript : bit.ly/3fQjXtF
React Hooks: bit.ly/3Vmh7wV
Redux: bit.ly/3yAIIkl
NodeJS/ExpressJS : bit.ly/35nN6Yt
MongoDB / Mongoose : bit.ly/3qPj0EO
💻 Projects Playlists
MERN Stack Complete Ecommerce : bit.ly/3ymSs0E
Web Design HTML+CSS - Home Page : bit.ly/35nZiIB
Web Design BootStrap - E-Commerce Site : bit.ly/3iPhaz7
React/Redux/Firebase - Todo-App : bit.ly/3DnekL8
🕹 Mini Projects (Single Video)
React - Tic Tac Toe (Redux / Hooks) : bit.ly/3uzLEuy
React - Game of Flag Quiz (Hooks) : bit.ly/3LpTC0e
React - Google Translate Clone (Hooks) : bit.ly/3Lo9xvZ
React - Chat App using Firebase (Hooks) : bit.ly/3wLgymj
Visit our site: coderdost.com
🔴 Full Courses List : coderdost.com/courses
🔴 Full Projects List : coderdost.com/projects
💾 Source Codes at : github.com/coderdost - วิทยาศาสตร์และเทคโนโลยี
Course of JavaScript : bit.ly/3u049tf
Course of Modern JS. (ES6) : bit.ly/3DvYCh6
React JS MasterClass (10 Hour) : th-cam.com/video/6l8RWV8D-Yo/w-d-xo.html
NODE+ EXPRESS+ MONGO MasterClass (12 Hour) : th-cam.com/video/ChVE-JbtYbM/w-d-xo.html
React Interviews : bit.ly/3QAjAln
JavaScript Shorts:
bit.ly/3XhHRQ1
React Shorts :
bit.ly/3VfIrMi
❤😊
simple way
let obj =[1.2,3]
console.log({...obj})
Easy way
// Convert an array into an object.
const names = ['Rahul','Krityveer','Anish','Gopal','Parveen'];
const obj = {...names }
console.log(obj);
still not 100% correct, in this case keys are not identical to the values
@@n_fan329 no one want keys identical to values
let arr = ['Sun', 'Moon', 'Star']
// Convert array to object
let obj = {};
let contertToObj = arr.forEach(i=>obj[i] = i);
console.log(obj)
// Convert object to array
let newArr = [];
Object.keys(obj).map(key=>newArr.push(key));
console.log(newArr)
most of the one channel in youtube which uploaded the best question for interviews in 1 minute
Please add some more video in this playlist, please
Ye sir enteries se bi to ho sakta hain ❤❤
Nice share 🙌
Awesome
Object.entries(obj) or if you wanna iterate the value only so you can go with object. Values(obj).
Using for each loop we can handle it easily
Object.keys(), Object.values(), Object.entries() to convert into array??
Ha
Bhaii ap kese kr rhe ho sb
Muje JS smj he nhi atti thi and u make it very simple for me thanku.
you can use a record for these conversions
big fan
Love from Bangladesh
Which extension do you use to get that return info while changing something in the function?
Quokka js
Object.values()
Object.assign({}, object name)
logic was simple but code should be readabe i was scared after this short later i understood
let arr = ['a','b','c']
let obj = arr.reduce((a,it,i)=>{
a[it] = it
return a
},{})
console.log(obj)
//{ a: 'a', b: 'b', c: 'c' }
let tab=[]
for (let val in obj){
tab.push(obj[val])
}
console.log(tab2)
Sir theem kon si hi
Bro ye answers side me kese aa rahe ge?
Object. Keys(array_name)
Which extention for inline result ?
Quokka js
Array.from()
Let newArr = [...Object.values(obj)];
//['a', 'b', 'c']
Solution -------->
Object.keys(Obj).map(i =>obj[i])
Object.entriesFrom
awesome,
Software ka name batao na!
quokka js extension
let arr=["a","b","c"];
let obj={};
for( let i=0 ;i
Yes that will also work
Arr.reverse
My question is why it is taking only the last one in case of string means
({...a,it:it}) o/p {it:"a"}
But why ({...a,[it]:it}) gives right answer
I am still not clarified.
When you put a square bracket on key of an object. The variable is evaluated and then the result is assigned as key string. This feature is used a lot in forms.
{[e.target.name]:e.target.value} this kind of syntax you will see in lot of frontend forms..
@coderdost after over a time I realised that when the data is entered in the object like {it:"a"} and in further iteration data is entered with different value but with the same key that's why our last key value is modified or replaced with new one bcz key is the same.
👍🏻 yes
Aap kaun sa code editor use karte ho. yeah console ki command bhi code mein hi Dikhai ja raha hai ya aap kaise kar rahe ho.
Aap ko subscribe Kar Diya Hai Ab lajmi batana.
VScode with Quokka JS extension
Using the Object.assign() method to convert an array to an object ,e.g. const obj = Object.assign({}, arr).
Using the spread syntax (...) to convert an array to an object, e.g. const obj = {...arr}.
Using the Array.forEach() method to iterate over all items and add them as a key-value pair to an object.
Using the Object.fromEntries() method to convert an array of key-value pairs to an object, e.g. const obj = Object.fromEntries(arr).
great info
Which code editor is this? Kindly share
VScode but with custom theme (setting json modified)
@@coderdost sir please ye kese karty hen aap bata sakty hen please mai buhat try karha hun mere vs code mai JS nhi run hora mai dhundh dhundh ke thak gya hun
Which extension gives results beside code like yours?
Quokka JS
Ye konsa theme hai??
Dark theme + Customized settings.json of VSCode
@@coderdost ispe ek shorts bnao.
Kitne time Lage aapko javascript Sikhne mein ? Aapki tarah kaise sikhu ?
I am using from 2010
const arr = [1, 23, 4, 56, 7];
// convert it into object
const out = arr.reduce((num, crt, i, ar) => {
return { ...num, [crt]: crt };
}, {});
console.log(out);
const rev = Object.values(out).reduce((prev, current, index, arr) => {
return [...prev, current];
}, []);
console.log(rev);
Boss kuch samajh mein nahin aya . And funny part is i know programming. Mujhe kuch knowledge hoke agar samajh nahin aya dusre ko kya aega
these are just tricky question ppl ask sometime in interviews. They are not normal in everyday work of JS developer. But good for practice.
Bro .. coding se upar he javascript... Js padho sahi se... He explained perfectly...
My question is that can program understand who is subject and object in array sentence ?
program must use some library to understand that language, other wise by simple logic its not possible.
const arr=['a','b','c']
let obj = Object.assign({},arr)
console.log(obj);
let array= Object.entries(obj)
console.log(array.flat());
array to object and object to array
ye ek good approch hai ya nahi
video ko reverse karo ho jayega 😂😂
😂
var emptyObj= { }
are.forEach((val)=> {
emptyObj[val] = val;
}
console.log(emptyObj);
🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉
Awesome