5

I am using map method to convert from an object to an array. What is the issue in the following code?

var data = {
    "productName": "fsdfsdf",
    "productDesc": "",
    "category": null,
    "categoryName": "",
    "style": null,
    "styleName": "",
    "substyle": null,
    "substyleName": "",
    "store": null,
    "storeName": "",
    "stand": null,
    "standName": "",
    "rack": null,
    "rackName": "",
    "roll": null,
    "rollName": "",
    "color": null,
    "width": "",
    "widthunit": "meter",
    "length": 0,
    "lengthunit": "meter",
    "pieces": "",
    "cutofquantity": "",
    "estimatedConsumption": ""
}

var key = $.map(data, function(value, index) {
    return index;
});
var value = $.map(data, function(value, index) {
    return value;
});

console.log(value)

Please refer to this JSFiddle for a live example.

2
  • 5
    That is an object literal, not JSON. Commented Jan 8, 2016 at 11:22
  • Please check jsfiddle.net/mbbh5xky/4 you can work with $.each and push() Commented Jan 8, 2016 at 11:30

5 Answers 5

6

Because you have length: 0 as one of your properties, jQuery thinks that the object is an array instead of an object.

It then loops over the numerical indexes from 0 to 0 (not inclusive) and generates a zero length array.

Sign up to request clarification or add additional context in comments.

1 Comment

Well spotted... that would probably be why.
1

Here is the alternate if you want to use with length:0

var data = {
    "productName": "fsdfsdf",
    "productDesc": "",
    "category": null,
    "categoryName": "",
    "style": null,
    "styleName": "",
    "substyle": null,
    "substyleName": "",
    "store": null,
    "storeName": "",
    "stand": null,
    "standName": "",
    "rack": null,
    "rackName": "",
    "roll": null,
    "rollName": "",
    "color": null,
    "width": "",
    "widthunit": "meter",
    "length": 0,
    "lengthunit": "meter",
    "pieces": "",
    "cutofquantity": "",
    "estimatedConsumption": ""
};



for(var key in data) {
    if(data.hasOwnProperty(key)) {
      console.log(key)  ;
      console.log(data[key]);  
    }
}

     
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Comments

0

You could do something like this below:

var data = {
    "productName": "fsdfsdf",
    "productDesc": "",
    "category": null,
    "categoryName": "",
    "style": null,
    "styleName": "",
    "substyle": null,
    "substyleName": "",
    "store": null,
    "storeName": "",
    "stand": null,
    "standName": "",
    "rack": null,
    "rackName": "",
    "roll": null,
    "rollName": "",
    "color": null,
    "width": "",
    "widthunit": "meter",
    "length": 0,
    "lengthunit": "meter",
    "pieces": "",
    "cutofquantity": "",
    "estimatedConsumption": ""
};

var arr = Object.keys(data).map(function(k) { return data[k] });  
console.log(arr)

Fiddle to play around: https://jsfiddle.net/u5t4L55g/

2 Comments

I need both key and value. It is giving value only
@Deen: can you just post how your array should look... i will form it accordingly
0

Loop through each property of the object and push the key and value in array like below. Hope this will help you.

var data = {
  "productName": "fsdfsdf",
  "productDesc": "",
  "category": null,
  "categoryName": "",
  "style": null,
  "styleName": "",
  "substyle": null,
  "substyleName": "",
  "store": null,
  "storeName": "",
  "stand": null,
  "standName": "",
  "rack": null,
  "rackName": "",
  "roll": null,
  "rollName": "",
  "color": null,
  "width": "",
  "widthunit": "meter",
  "length": 0,
  "lengthunit": "meter",
  "pieces": "",
  "cutofquantity": "",
  "estimatedConsumption": ""
}

var value = [];
var key = [];
for (var property in data) {
    key.push(property);
    value.push(data[property]);
}

console.log(value)

Comments

-1
var key = Object.keys(data).map(function(index, value) { return index });
var value = Object.keys(data).map(function(index, value) { return data[index] });

So it is giving key and value pairs

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.