Welcome, Guest: Join Nairaland / LOGIN! / Trending / Recent / New
Stats: 2,622,265 members, 6,112,245 topics. Date: Friday, 22 January 2021 at 04:05 AM

How Do I Convert Object To Arrey In Javascript - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / How Do I Convert Object To Arrey In Javascript (1072 Views)

How Do I Convert My Jpg Files To Doc And Pdf. / Please How Can I Convert This Mysql Code To Mysqli / How Do I Convert Csv File To Graph (2) (3) (4)

(1) (Reply) (Go Down)

How Do I Convert Object To Arrey In Javascript by Venzee(m): 12:12pm On Mar 04, 2020
please i want to a object to an Arrey, so dat i can easly loop it.
i hv try these codes but it dosn't wrk. e.g
var obj = {a: 'venzee',
b:23,
c:false
}
var conv = [].slice.call(obj) or also
var conv = [].concat.call(obj);
pls help me
Re: How Do I Convert Object To Arrey In Javascript by siwhoh(m): 12:19pm On Mar 04, 2020
I asked same question on a Facebook group, still waiting for answer up till now. Let me park here and see.
Re: How Do I Convert Object To Arrey In Javascript by Venzee(m): 12:35pm On Mar 04, 2020
siwhoh:
I asked same question on a Facebook group, still waiting for answer up till now. Let me park here and see.
Re: How Do I Convert Object To Arrey In Javascript by Venzee(m): 12:39pm On Mar 04, 2020
siwhoh:
I asked same question on a Facebook group, still waiting for answer up till now. Let me park here and see.
my brother ah don try am oo, d object de enter inside de Array en no de convert to Array.
Re: How Do I Convert Object To Arrey In Javascript by biggjoe(m): 2:49pm On Mar 04, 2020
why not loop through the object instead.
Re: How Do I Convert Object To Arrey In Javascript by afuye(m): 3:00pm On Mar 04, 2020
use object.keys https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

you use Object.keys(obj) // if you need the keys of the object

you use Object.values(obj) // if you need the values of the object





Venzee:
please i want to a object to an Arrey, so dat i can easly loop it.
i hv try these codes but it dosn't wrk. e.g
var obj = {a: 'venzee',
b:23,
c:false
}
var conv = [].slice.call(obj) or also
var conv = [].concat.call(obj);
pls help me
Re: How Do I Convert Object To Arrey In Javascript by Venzee(m): 6:33pm On Mar 04, 2020
biggjoe:
why not loop through the object instead.
bros u no fee loop an object using de .length function.
even u use am, how u go log de object to de console, cuz if u do am lik dis ( obj{i} ) en no go work
Re: How Do I Convert Object To Arrey In Javascript by afuye(m): 9:15pm On Mar 04, 2020
Venzee:

bros u no fee loop an object using de .length function.
even u use am, how u go log de object to de console, cuz if u do am lik dis ( obj{i} ) en no go work
u can using for-in-loop but can be dangerous
Re: How Do I Convert Object To Arrey In Javascript by Karleb(m): 11:55pm On Mar 04, 2020
Use the Array.from() method.

Pass your object within the from function.

1 Like

Re: How Do I Convert Object To Arrey In Javascript by afuye(m): 12:13am On Mar 05, 2020
Karleb:
Use the Array.from() method.

Pass your object within the from function.

Very wrong!!!
Re: How Do I Convert Object To Arrey In Javascript by silento(m): 3:36am On Mar 05, 2020
The last time I check object can be looped easily or did I not understand the question

Var obj = {1:"ara",2:"test"}
For(var i in obj)
{
Var value = obj[i]

}

2 Likes

Re: How Do I Convert Object To Arrey In Javascript by Karleb(m): 6:35am On Mar 05, 2020

1 Like

Re: How Do I Convert Object To Arrey In Javascript by afuye(m): 8:55am On Mar 05, 2020
Karleb:



https://www.w3schools.com/jsref/jsref_from.asp

undecided



Try it for yourself and lemme know what it gives u
Re: How Do I Convert Object To Arrey In Javascript by jelel6: 9:40am On Mar 05, 2020
Venzee:
please i want to a object to an Arrey, so dat i can easly loop it.
i hv try these codes but it dosn't wrk. e.g
var obj = {a: 'venzee',
b:23,
c:false
}
var conv = [].slice.call(obj) or also
var conv = [].concat.call(obj);
pls help me

Be concise and elaborate when when you want to ask technical questions to enable people help you better. Also, endeavor to reply to suggestions to allow people follow up with you according. If you have found a solution, say so to let others know what eventually worked out.

In the example object you provided above. The following object methods would return an array as this:

Object.keys(obj) = [ 'a', 'b', 'c' ];

Object.values(obj) = [ "venzee", 23, false];

Object.entries(obj) = [ [ 'a' : "venzee" ], [ 'b' : 23 ], [ 'c' : false ] ];

All the above methods should give a "Real" array, which you can loop over afterwards using something like map or forEach


If you just want to loop over directly, you can do this...

// Looping over all the values:
for (let value of Object.values(obj)) {
alert(value); // "venzee", -> 23 -> false in that order
};

That should solve your problem

1 Like

Re: How Do I Convert Object To Arrey In Javascript by Maskyy(m): 9:21pm On Mar 07, 2020
jelel6:


Be concise and elaborate when when you want to ask technical questions to enable people help you better. Also, endeavor to reply to suggestions to allow people follow up with you according. If you have found a solution, say so to let others know what eventually worked out.

In the example object you provided above. The following object methods would return an array as this:

Object.keys(obj) = [ 'a', 'b', 'c' ];

Object.values(obj) = [ "venzee", 23, false];

Object.entries(obj) = [ [ 'a' : "venzee" ], [ 'b' : 23 ], [ 'c' : false ] ];

All the above methods should give a "Real" array, which you can loop over afterwards using something like map or forEach


If you just want to loop over directly, you can do this...

// Looping over all the values:
for (let value of Object.values(obj)) {
alert(value); // "venzee", -> 23 -> false in that order
};

That should solve your problem

Nawahooo for js devs here. Just dey write in nonsense.

This is the only person wey dey reason normal with the question the op is asking.

Also, for...in loop is used to iterate object but it's not for best practice. So the options this author gave is cool. Just read more on them op.

Besides, I sell paid udemy courses for low price.
Re: How Do I Convert Object To Arrey In Javascript by benedictuyi(m): 2:46am On Mar 08, 2020
Oga Google is your friend na. There's a high order function in JavaScript called 'map'. You can use map to loop through an object
Re: How Do I Convert Object To Arrey In Javascript by Johnmattee(m): 8:27am On Mar 08, 2020
benedictuyi:
Oga Google is your friend na. There's a high order function in JavaScript called 'map'. You can use map to loop through an object
You can't use map function on a plain object unless you find a way to convert it to an array and can filter through using filter function.

1 Like

Re: How Do I Convert Object To Arrey In Javascript by makavele: 9:10am On Mar 08, 2020
Thank me later:

const object = { ..... blah blah blah }

const array = Object.keys(object).map(key => object[key])

3 Likes

Re: How Do I Convert Object To Arrey In Javascript by Venzee(m): 11:36pm On Mar 09, 2020
silento:
The last time I check object can be looped easily or did I not understand the question

Var obj = {1:"ara",2:"test"}
For(var i in obj)
{
Var value = obj[i]

}
Chaii Bros thank you...
Dis one work..
Re: How Do I Convert Object To Arrey In Javascript by Venzee(m): 11:40pm On Mar 09, 2020
makavele:
Thank me later:

const object = { ..... blah blah blah }

const array = Object.keys(object).map(key => object[key])

Confirm.....!!
Exactly what I needed
Bros you too much..
Re: How Do I Convert Object To Arrey In Javascript by silento(m): 12:13am On Mar 10, 2020
Venzee:

Chaii Bros thank you...
Dis one work..

U are welcome
Re: How Do I Convert Object To Arrey In Javascript by Maskyy(m): 10:50pm On Apr 09, 2020
You want to be a programmer, developer, data scientists, designer and mobile dev eith full guide and motoring with tasks.

Codefans is helping a budding techie person Like you. Join their community and get train at no cost.

Fill this form to get started

https://docs.google.com/forms/d/18cWuMdfV2fZ7ERJHRkwzpAmqaAwA-_W5R4cpE0zGeFw/edit?usp=drivesdk

(1) (Reply)

I Want To Learn Java. / Please Assist Me In This C Code Challenge! / Which Is The Best IT School In The World?

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2021 Oluwaseun Osewa. All rights reserved. See How To Advertise. 85
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.