Do not get array from object

I use the map method to convert from an object to an array. What is the problem 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.

+5
source share
5 answers

Since you have length: 0 as one of your properties, jQuery considers the object to be an array instead of the object.

Then it iterates over the numerical indices from 0 to 0 (not inclusively) and generates an array of zero length.

+6
source

Here is an alternative 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> 
+1
source

You can 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) 

Try playing: https://jsfiddle.net/u5t4L55g/

0
source

Scroll through each property object and press key and value in the array, as shown below. Hope this helps 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) 
0
source
 var key = Object.keys(data).map(function(index, value) { return index }); var value = Object.keys(data).map(function(index, value) { return data[index] }); 

Thus, it gives key pairs and values

-1
source

Source: https://habr.com/ru/post/1240165/


All Articles