Deen
Deen

Reputation: 621

Not getting array from object

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.

Upvotes: 5

Views: 70

Answers (5)

Parth Trivedi
Parth Trivedi

Reputation: 3832

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>

Upvotes: 1

Deen
Deen

Reputation: 621

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

Upvotes: -1

Ibrahim Khan
Ibrahim Khan

Reputation: 20750

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)

Upvotes: 0

Quentin
Quentin

Reputation: 944442

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.

Upvotes: 6

Thalaivar
Thalaivar

Reputation: 23642

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/

Upvotes: 0

Related Questions