Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get key values from JsonObj

I have a json object like this actually it is coming dynamically. For example we can use this Json object.

var JsonObj= { "one":1, "two":2, "three":3, "four":4, "five":5 };

But i want to change it some thing like this using javascript or jquery.

var sample = [

 {
  "label":"one",
  "value":1
 },

 {
  "label":"two",
  "value":2
 },
 {
  "label":"three",
  "value":3
 },
 { 
  "label":"four",
  "value":4
 },
 { 
  "label":"five",
  "value":5
  }

 ];
like image 259
Shrinivas Pai Avatar asked Apr 14 '26 14:04

Shrinivas Pai


1 Answers

var JsonObj= { "one":1, "two":2, "three":3, "four":4, "five":5 };
var arr = [];
for(property in JsonObj) {
    arr.push(
             {"label":property, 
             "value":JsonObj[property]})
};
like image 98
Paweł Chorążyk Avatar answered Apr 16 '26 03:04

Paweł Chorążyk