user image

Published in : 2022-03-06

Required to convert Obj {} to Arr [] from key-value pairs using javascript


Hi, I want to convert objects JSON (Key-value pairs) to Array as given below,

My object JSON is given as below,

{"bookid": 1, "bookname": "rich dad poor dad", "issue_number": 1234}

I want output as,

[['bookid',1], ['bookname', 'rich dad poor dad'], ['issue_number',1234]]

 Is It possible to achieve it?


Mohamed Atef Date : 2022-03-06

Best answers


Best answers


You can do it using For loop of object like 

let obj = {"bookid": 1, "bookname": "rich dad poor dad", "issue_number": 1234};
let arr = [];
for (const [key, value] of Object.entries(obj)) {
    arr.push([key, value]);

Let me know if this works with you 

Rakshit Date : 2022-03-06

Best answers


Best answers


Using `Object.keys() ` and `map() `, you can convert your input as per your expected output.

See this working jsfiddle demo



var input = {"bookid": 1, "bookname": "rich dad poor dad", "issue_number": 1234};

var output = Object.keys(input).map((key) => [String(key), input[key]]);

//[["bookid",1],["bookname","rich dad poor dad"],["issue_number",1234]]

Reference: Mozilla Object keys, map

Leave a comment

Join us

Join our community and get the chance to solve your code issues & share your opinion with us

Sign up Now

Related posts

Can we add a javascript or jQuery to the URL?
Publish date: 2022-03-05 | Comments: 2

Tag: Javascript

Vanilla JavaScript Next & prev Navigation
Publish date: 2022-02-22 | Comments: 1

Tag: Javascript

How to move a div with its content to TinyMEC editor?
Publish date: 2022-02-11 | Comments: 0

Tag: Javascript

How can I switch between two background-colors using js setinterval( )
Publish date: 2022-02-22 | Comments: 1

Tag: Javascript

How select specific tag in jQuery using if else statement?
Publish date: 2022-02-27 | Comments: 1

Tag: Javascript

Adding an id number at the first index to an array of objects
Publish date: 2022-02-13 | Comments: 1

Tag: Javascript

Javascript Add (+) issue
Publish date: 2022-02-26 | Comments: 1

Tag: Javascript