Related
I searched this a lot on stackoverflow before posting this ticket so please don't consider this as duplicate question.
I have scenario of an array with two items, I am pushing an object at index 1 but it's pushing same object on both indexes.
Extra Object
const extraObj = {
"item_id": "4",
"item_name": "Extra Patty",
"item_image": "placeholder_img.jpg",
"item_price": "3.00",
"currency_code": "USD",
"currency_symbol": "$",
"gst_fee": "0.00",
"item_quantity": "1",
"branch_id": "1",
"is_type": "extra"
};
Items Array
const itemsArr = [
{
"item_id": "3",
"category_id": "12",
"item_name": "Waffles",
"item_image": "item_081220170335.jpg",
"is_variant": "0",
"item_price": "7",
"currency_code": "USD",
"currency_symbol": "$",
"gst_fee": "0.00",
"item_quantity": "1",
"branch_id": "1",
"is_type": "item"
},
{
"item_id": "3",
"category_id": "12",
"item_name": "Waffles",
"item_image": "item_081220170335.jpg",
"is_variant": "0",
"item_price": "7",
"currency_code": "USD",
"currency_symbol": "$",
"gst_fee": "0.00",
"item_quantity": "1",
"branch_id": "1",
"is_type": "item",
}
];
itemsArr[1].extras = [extraObj];
// result
[
{
"item_id": "3",
"category_id": "12",
"item_name": "Waffles",
"item_image": "item_081220170335.jpg",
"is_variant": "0",
"item_price": "7",
"currency_code": "USD",
"currency_symbol": "$",
"gst_fee": "0.00",
"item_quantity": "1",
"branch_id": "1",
"is_type": "item",
"extras": [
{
"item_id": "4",
"item_name": "Extra Patty",
"item_image": "placeholder_img.jpg",
"item_price": "3.00",
"currency_code": "USD",
"currency_symbol": "$",
"gst_fee": "0.00",
"item_quantity": "1",
"branch_id": "1",
"is_type": "extra"
}
]
},
{
"item_id": "3",
"category_id": "12",
"item_name": "Waffles",
"item_image": "item_081220170335.jpg",
"is_variant": "0",
"item_price": "7",
"currency_code": "USD",
"currency_symbol": "$",
"gst_fee": "0.00",
"item_quantity": "1",
"branch_id": "1",
"is_type": "item",
"extras": [
{
"item_id": "4",
"item_name": "Extra Patty",
"item_image": "placeholder_img.jpg",
"item_price": "3.00",
"currency_code": "USD",
"currency_symbol": "$",
"gst_fee": "0.00",
"item_quantity": "1",
"branch_id": "1",
"is_type": "extra"
}
]
}
]
case SET_FOOD_EXTRAS:
const { itemId, extra, extraIndex, itemIndex } = action.payload;
const cloneItems = cloneDeep([...state.items]);
// cloneItems.splice(itemIndex, 0, { extras: [extra] });
cloneItems[itemIndex].extras = [extra];
return {
...state,
items: cloneItems,
summary: setSummary(state, cloneItems)
};
It depends on how you populate your itemsArr.
If you do something like:
const ob1 = {
"item_id": "3",
"category_id": "12",
"item_name": "Waffles",
"item_image": "item_081220170335.jpg",
"is_variant": "0",
"item_price": "7",
"currency_code": "USD",
"currency_symbol": "$",
"gst_fee": "0.00",
"item_quantity": "1",
"branch_id": "1",
"is_type": "item"
}
itemsArr.push(ob1);
itemsArr.push(ob1);
Then, both items in the array point to the same object so any modifications to one will be reflected on the other.
One solution is to make copies of the object:
itemsArr.push({...ob1});
itemsArr.push({..ob1});
I am working in react Js. There is a trouble for me to remove an item corresponding to "product_option_value_id" .I need to remove item from product_option_value ( child array object) if my given itemId==product_option_value_id, and return whole array object after removing the specified item. I think the idea is clear . I tried a method , but I got undefined.
"default": 0,
"master_option": 0,
" master_option_value": 0,
"maximum": 0,
"minimum": 0,
"name": "Choose Size",
"option_id": "4",
"product_option_id": "111",
"product_option_value": [{
"ec_product_id": "",
"exclude_others": "0",
"image": "",
"master_option_value": "0",
"name": "25ml",
"option_value_id": "8",
"points": "0",
"product_option_value_id": "267"
},
{
" ec_product_id": "22w",
"exclude_others": "0",
"image": "",
"master_option_value": "0",
"name": "25ml",
"option_value_id": "8",
"points": "0",
"product_option_value_id": "266"
}
]
},
{
"default": 0,
"master_option": 0,
" master_option_value": 0,
"maximum": 0,
"minimum": 0,
"name": "Water",
"option_id": "4",
"product_option_id": "111",
"product_option_value": [{
"ec_product_id": "",
"exclude_others": "0",
"image": "",
"master_option_value": "0",
"name": "25ml",
"option_value_id": "8",
"points": "0",
"product_option_value_id": "700"
},
{
" ec_product_id": "22w",
"exclude_others": "0",
"image": "",
"master_option_value": "0",
"name": "50ml",
"option_value_id": "8",
"points": "0",
"product_option_value_id": "701"
},
{
" ec_product_id": "22w",
"exclude_others": "0",
"image": "",
"master_option_value": "0",
"name": "500ml",
"option_value_id": "8",
"points": "0",
"product_option_value_id": "702"
}, {
" ec_product_id": "22w",
"exclude_others": "0",
"image": "",
"master_option_value": "0",
"name": "500ml",
"option_value_id": "8",
"points": "0",
"product_option_value_id": "703"
}
]
}
]
The method I tried
const removeOption=(itemId:any)=>{
const filteredItems = productOptions.forEach(function(o:any) {
o.product_option_value = o.product_option_value.filter((s:any) => s.product_option_value_id != itemId);
});
}
Well, the forEach method does not return the array, that is why you are getting undefined as result. This method just perform a function for each element:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
So you need to use the .map() method instead:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
so basically:
const removeOption = (itemId: any) => {
return productOptions.map((o: any) => {
return { ...o, product_option_value: o.product_option_value.filter((s: any) => s.product_option_value_id !== itemId)};
});
}
I am trying to insert JSON data that are supposed to be search results into my Tabulator, which is then supposed to display the search results in their respective columns. Here is my code:
<body>
<div id="example-table"></div>
<script>
var table = new Tabulator("#example-table", {
ajaxURL:"http://hadrians-search.tk/search",
ajaxParams:{search_param:"ball", items_per_page:"2", page_number:"2"},
ajaxResponse:function(url, params, response){
//url - the URL of the request
//params - the parameters passed with the request
//response - the JSON object returned in the body of the response.
return response.Object; //return the d property of a response json object
},
columns:
[{title:"Title", field:"shippingCost.title"},
{title:"Price", field:"price"},
{title:"Shipping Cost", field:"shippingCost.shippingServiceCost.value"},
{title:"Shipping Type", field:"shippingCost.shippingServiceCost.shippingType"},
],
});
</script>
</body>
I am receiving this in the Console tab of my browser:
Data Loading Error - Unable to process data due to invalid data type
Expecting: array
Received: undefined
Data: undefined tabulator.min.js:2:29478
n.prototype._setDataActual
https://unpkg.com/tabulator-tables#4.1.2/dist/js/tabulator.min.js:2:29478
n.prototype.setData/<
https://unpkg.com/tabulator-tables#4.1.2/dist/js/tabulator.min.js:2:28800
n.prototype.setData
https://unpkg.com/tabulator-tables#4.1.2/dist/js/tabulator.min.js:2:28624
f.prototype._loadDataStandard/</<
https://unpkg.com/tabulator-tables#4.1.2/dist/js/tabulator.min.js:5:7539
This is the JSON Response I am sending to the Tabulator:
{
"0": {
"country": "US",
"itemId": "323440622675",
"price": "11.02",
"shippingCost": {
"expeditedShipping": "false",
"handlingTime": "3",
"oneDayShippingAvailable": "false",
"shipToLocations": "Worldwide",
"shippingServiceCost": {
"_currencyId": "USD",
"value": "0.0"
},
"shippingType": "Free"
},
"title": "Magnetic Pearl Ball Curtain Tiebacks Tie Backs Holdbacks Buckle Clips Accessory",
"user_args": {
"advanced": null,
"pages": {
"entries_per_page": 10,
"page_number": 10
},
"search_terms": "ball"
}
},
"1": {
"country": "CN",
"itemId": "332746804737",
"price": "2.49",
"shippingCost": {
"expeditedShipping": "false",
"handlingTime": "1",
"oneDayShippingAvailable": "false",
"shipToLocations": "Worldwide",
"shippingServiceCost": {
"_currencyId": "USD",
"value": "0.0"
},
"shippingType": "Free"
},
"title": "Natural Amethyst Quartz Stone Sphere Crystal Fluorite Ball Healing Gemstone",
"user_args": {
"advanced": null,
"pages": {
"entries_per_page": 10,
"page_number": 10
},
"search_terms": "ball"
}
},
"2": {
"country": "US",
"itemId": "322315462251",
"price": "5.49",
"shippingCost": {
"expeditedShipping": "true",
"handlingTime": "1",
"oneDayShippingAvailable": "false",
"shipToLocations": "Worldwide",
"shippingServiceCost": {
"_currencyId": "USD",
"value": "4.89"
},
"shippingType": "Flat"
},
"title": "Richardson Trucker Ball Cap Meshback Hat Snapback Cap Trucker Hat Cap - 112",
"user_args": {
"advanced": null,
"pages": {
"entries_per_page": 10,
"page_number": 10
},
"search_terms": "ball"
}
},
"3": {
"country": "US",
"itemId": "183411812494",
"price": "22.99",
"shippingCost": {
"expeditedShipping": "true",
"handlingTime": "0",
"oneDayShippingAvailable": "false",
"shipToLocations": "Worldwide",
"shippingServiceCost": {
"_currencyId": "USD",
"value": "0.0"
},
"shippingType": "Free"
},
"title": "Seismic Sports Slam Ball 10 - 30 lb Slam Ball for Crossfit, HIIT, Plyometrics",
"user_args": {
"advanced": null,
"pages": {
"entries_per_page": 10,
"page_number": 10
},
"search_terms": "ball"
}
},
"4": {
"country": "US",
"itemId": "113179929571",
"price": "20.89",
"shippingCost": {
"expeditedShipping": "true",
"handlingTime": "1",
"oneDayShippingAvailable": "true",
"shipToLocations": "Worldwide",
"shippingServiceCost": {
"_currencyId": "USD",
"value": "0.0"
},
"shippingType": "Free"
},
"title": "3 In 1 Kids Baby Play Tent Ball Pit Pool House Crawl Tunnel Indoor Outdoor Game",
"user_args": {
"advanced": null,
"pages": {
"entries_per_page": 10,
"page_number": 10
},
"search_terms": "ball"
}
},
"5": {
"country": "US",
"itemId": "153249589978",
"price": "10.34",
"shippingCost": {
"expeditedShipping": "true",
"handlingTime": "1",
"oneDayShippingAvailable": "false",
"shipToLocations": "Worldwide",
"shippingServiceCost": {
"_currencyId": "USD",
"value": "0.0"
},
"shippingType": "Free"
},
"title": "Premium Official Size 5 USA Soccer Ball W/ Pump Assorted Graphics!",
"user_args": {
"advanced": null,
"pages": {
"entries_per_page": 10,
"page_number": 10
},
"search_terms": "ball"
}
},
"6": {
"country": "US",
"itemId": "153168623537",
"price": "58.99",
"shippingCost": {
"expeditedShipping": "true",
"handlingTime": "1",
"oneDayShippingAvailable": "false",
"shipToLocations": "Worldwide",
"shippingServiceCost": {
"_currencyId": "USD",
"value": "0.0"
},
"shippingType": "Free"
},
"title": "Dragon Ball The Complete Series Seasons 1-5 - 1,2,3,4,5 New",
"user_args": {
"advanced": null,
"pages": {
"entries_per_page": 10,
"page_number": 10
},
"search_terms": "ball"
}
},
"7": {
"country": "US",
"itemId": "110874290750",
"price": "9.41",
"shippingCost": {
"expeditedShipping": "true",
"handlingTime": "1",
"oneDayShippingAvailable": "true",
"shipToLocations": "Worldwide",
"shippingServiceCost": {
"_currencyId": "USD",
"value": "0.0"
},
"shippingType": "FlatDomesticCalculatedInternational"
},
"title": "5006 Flexfit Sweep Low Profile Fitted Baseball Blank Plain Hat Ball Cap Flex Fit",
"user_args": {
"advanced": null,
"pages": {
"entries_per_page": 10,
"page_number": 10
},
"search_terms": "ball"
}
},
"8": {
"country": "US",
"itemId": "332908229449",
"price": "39.99",
"shippingCost": {
"expeditedShipping": "true",
"handlingTime": "2",
"oneDayShippingAvailable": "false",
"shipToLocations": "Worldwide",
"shippingServiceCost": {
"_currencyId": "USD",
"value": "0.0"
},
"shippingType": "Free"
},
"title": "ADIDAS BRAZUCA OFFICIAL MATCH BALL AUTHENTIC WORLD CUP 2014",
"user_args": {
"advanced": null,
"pages": {
"entries_per_page": 10,
"page_number": 10
},
"search_terms": "ball"
}
},
"9": {
"country": "US",
"itemId": "282781534125",
"price": "13.99",
"shippingCost": {
"expeditedShipping": "true",
"handlingTime": "1",
"oneDayShippingAvailable": "false",
"shipToLocations": "Worldwide",
"shippingServiceCost": {
"_currencyId": "USD",
"value": "0.0"
},
"shippingType": "Free"
},
"title": "3/4/5/6/8inch 110V Magic Crystal Globe Desktop Lightning Lamp Plasma Ball Sphere",
"user_args": {
"advanced": null,
"pages": {
"entries_per_page": 10,
"page_number": 10
},
"search_terms": "ball"
}
}
}
I do not know why I am receiving this undefined error, as I made sure to return response.Object for the ajaxResponse. Any help would be greatly appreciated!
There are a couple of issue here
Firstly you should generally be returning an array of objects not an object with enumerated properties.
instead of this:
{
{
"0": {
"country": "US",
},
"1": {
"country": "CN",
}
}
It should be this:
[
{"country": "US"},
{"country": "CN"},
]
At that point you wouldn't need to use the ajaxResponse function, tabulator would be able to parse the data directly.
If you can only return the data in that original format there is no problem, but it brings us onto the second issue, the reason you are getting the "undefined error" is because there is no "Object" property on the response object, only the properties "0", "1", "2" etc...
To convert your response to something that Tabulator can understand you will need to use the following ajaxResponse callback:
ajaxResponse:function(url, params, response){
return Object.values(response);
}
That will convert your returned object of objects into an array of objects.
I'm working with some custom jSON objects.
I'm trying to explore this whole object, and to change field type.
For exemple, real_id or clean_id and even temperature need to be converted as Integer.
To deal with that, I started to make a function which is very simple :
var reg = /^\d+$/;
_.each(myObject, function(val, key) {
myObject[key] = reg.test(val) ? parseInt(val) :val;
});
I got several needs :
it will check every field one by one on the first level, but if myObject has elements which themselves include object or array of object, my _.each won't explore them.
For now it can detect integer thanks to the regex, and convert them to Integer but what if I need to convert a float?
If I find null I need to set it to 0 instead
On my exemple below,
Exemple of Sample of jSON object :
myObject = {
"real_name": "Test",
"maker": "Jean-Paul",
"company": "",
"real_id": "646402",
"clean_id": "152691",
"year": 2013,
"type": "Red real",
"available_for_order": 0,
"level": null,
"bio": null,
"temperature": "15",
"potential": "3",
"country_code": "USA",
"coord_lng": "2.7014349999999",
"coord_lat": "42.717317",
"real_description": [
{
"comment": "Real good stuff",
"lang": "it",
"video_end": null,
"video_id": null,
"video_start": null,
"video_url": null
},
{
"comment": "Awesome and cheap stuff",
"lang": "en",
"video_end": null,
"video_id": null,
"video_start": null,
"video_url": null
}
],
"full_description": [
{
"description": "Long description",
"lang": "fr"
},
{
"description": "",
"lang": "en"
}
],
"list": [
{
"available_for_order": "0",
"capacity": "4500",
"conciergerie": "0",
"currency_code": "EUR",
"price": 0,
"quantity": 0,
"retail_price": 0,
"real_id": "656732",
"year": "1"
},
{
"available_for_order": "0",
"capacity": "4500",
"conciergerie": "0",
"currency_code": "EUR",
"price": 0,
"quantity": 0,
"retail_price": 0,
"real_id": "330381",
"year": "2008"
},
{
"available_for_order": "0",
"capacity": "4500",
"conciergerie": "0",
"currency_code": "EUR",
"price": 20,
"quantity": 0,
"retail_price": 0,
"real_id": "11453216",
"year": "2010"
},
{
"available_for_order": "0",
"capacity": "4500",
"conciergerie": "0",
"currency_code": "EUR",
"price": 0,
"quantity": 0,
"retail_price": 0,
"real_id": "11497420",
"year": "2011"
},
{
"available_for_order": "0",
"capacity": "4500",
"conciergerie": "1",
"currency_code": "EUR",
"price": 10,
"quantity": 0,
"retail_price": 0,
"real_id": "11506715",
"year": "2012"
},
{
"available_for_order": "1",
"capacity": "4500",
"conciergerie": "0",
"currency_code": "EUR",
"price": 10,
"quantity": 0,
"retail_price": 0,
"real_id": "11458207",
"year": "2013"
}
],
"check_list": [
"3",
"8",
"9",
"10",
"14"
],
"image_src": "791330_31odFZZoM2_JqkCPJGHXa_4500x1000.jpeg"
}
Thanks a lot for your help
What you need, in my opinion, is a simple example of recursion, Lodash isn't required...
NOTE: You're using a regex that matches only string that starts and ends with a one or more occurrence of digit...
So, instead of using parseInt or parseFloat, you should use Number to cast values!
In Javascript Number is the constructor of the type Number, but, if called without the new operator, performs a basic Casting.
It returns NaN when the casting is unsatisfable, so, using the or operator we can keep the original value!
Hope Helps...
var myObject = {
"real_name": "Test",
"maker": "Jean-Paul",
"company": "",
"real_id": "646402",
"clean_id": "152691",
"year": 2013,
"type": "Red real",
"available_for_order": 0,
"level": null,
"bio": null,
"temperature": "15",
"potential": "3",
"country_code": "USA",
"coord_lng": "2.7014349999999",
"coord_lat": "42.717317",
"real_description": [
{
"comment": "Real good stuff",
"lang": "it",
"video_end": null,
"video_id": null,
"video_start": null,
"video_url": null
},
{
"comment": "Awesome and cheap stuff",
"lang": "en",
"video_end": null,
"video_id": null,
"video_start": null,
"video_url": null
}
],
"full_description": [
{
"description": "Long description",
"lang": "fr"
},
{
"description": "",
"lang": "en"
}
],
"list": [
{
"available_for_order": "0",
"capacity": "4500",
"conciergerie": "0",
"currency_code": "EUR",
"price": 0,
"quantity": 0,
"retail_price": 0,
"real_id": "656732",
"year": "1"
},
{
"available_for_order": "0",
"capacity": "4500",
"conciergerie": "0",
"currency_code": "EUR",
"price": 0,
"quantity": 0,
"retail_price": 0,
"real_id": "330381",
"year": "2008"
},
{
"available_for_order": "0",
"capacity": "4500",
"conciergerie": "0",
"currency_code": "EUR",
"price": 20,
"quantity": 0,
"retail_price": 0,
"real_id": "11453216",
"year": "2010"
},
{
"available_for_order": "0",
"capacity": "4500",
"conciergerie": "0",
"currency_code": "EUR",
"price": 0,
"quantity": 0,
"retail_price": 0,
"real_id": "11497420",
"year": "2011"
},
{
"available_for_order": "0",
"capacity": "4500",
"conciergerie": "1",
"currency_code": "EUR",
"price": 10,
"quantity": 0,
"retail_price": 0,
"real_id": "11506715",
"year": "2012"
},
{
"available_for_order": "1",
"capacity": "4500",
"conciergerie": "0",
"currency_code": "EUR",
"price": 10,
"quantity": 0,
"retail_price": 0,
"real_id": "11458207",
"year": "2013"
}
],
"check_list": [
"3",
"8",
"9",
"10",
"14"
],
"image_src": "791330_31odFZZoM2_JqkCPJGHXa_4500x1000.jpeg"
};
function recurse(obj) {
for(var key in obj) {
if(!obj.hasOwnProperty(key)) { continue; }
if('object' === typeof obj[key]) {
recurse(obj[key]);
} else {
console.log(key, obj[key], typeof obj[key], ' ==> ', typeof (Number(obj[key]) || obj[key]));
obj[key] = Number(obj[key]) || obj[key];
}
}
}
recurse(myObject);
var parseObjectProperties = function (obj) {
_.each(obj, function(val, key) {
if (typeof(key) === 'object') {
parseObjectProperties(key);
} else {
if (getType(val) === 'float') {
myObject[key] = parseFloat(val);
} else if (getType(val) === 'int') {
myObject[key] = parseInt(val);
} else if (val === null) {
myObject[key] = 0;
}
}
});
};
var getType = function(input) {
var match = (/[\d]+(\.[\d]+)?/).exec(input);
if (match ) {
if (match [1]) {
return 'float';
} else {
return 'int';
}
}
return 'string';
}
exploreObject(myObject);
I have the following json array:
{
"id": "1004482725",
"parent": 0,
"industry": "Akrotiri",
"industry_id": "root",
"name": "Comp",
"percentage": "0",
"report": "null",
"description": "",
"subsidiary": "3",
"children": [{
"id": "1004482733",
"parent": "1004482725",
"industry": "Akrotiri",
"industry_id": "industry9",
"name": "Comp11",
"percentage": "30",
"report": "null",
"description": "",
"subsidiary": "1",
"children": [{
"id": "1004482734",
"parent": "1004482733",
"industry": "Akrotiri",
"industry_id": "industry10",
"name": "Comp110",
"percentage": "30.00",
"report": "null",
"description": "",
"subsidiary": "1",
"children": [{
"id": "aa02634",
"parent": "1004482734",
"industry": "",
"industry_id": "0",
"name": "Sef 4",
"percentage": "4",
"report": "null",
"description": "",
"subsidiary": "0",
"children": []
}]
}, {
"id": "1004482735",
"parent": "1004482733",
"industry": "Akrotiri",
"industry_id": "industry11",
"name": "Comp111",
"percentage": "30.00",
"report": "null",
"description": "",
"subsidiary": "1",
"children": [{
"id": "aa02635",
"parent": "1004482735",
"industry": "",
"industry_id": "0",
"name": "Sef 5",
"percentage": "5",
"report": "null",
"description": "",
"subsidiary": "0",
"children": []
}]
}, {
"id": "1004482736",
"parent": "1004482733",
"industry": "Akrotiri",
"industry_id": "industry12",
"name": "Comp112",
"percentage": "30.00",
"report": "null",
"description": "",
"subsidiary": "1",
"children": [{
"id": "1004482737",
"parent": "1004482736",
"industry": "Akrotiri",
"industry_id": "industry13",
"name": "Comp1120",
"percentage": "100.00",
"report": "null",
"description": "",
"subsidiary": "2",
"children": [{
"id": "1004482738",
"parent": "1004482737",
"industry": "Akrotiri",
"industry_id": "industry14",
"name": "Comp11200",
"percentage": "100.00",
"report": "null",
"description": "",
"subsidiary": "1",
"children": [{
"id": "1004482739",
"parent": "1004482738",
"industry": "Akrotiri",
"industry_id": "industry15",
"name": "Comp112000",
"percentage": "100.00",
"report": "null",
"description": "",
"subsidiary": "1",
"children": [{
"id": "aa02636",
"parent": "1004482739",
"industry": "",
"industry_id": "0",
"name": "Sef 6",
"percentage": "6",
"report": "null",
"description": "",
"subsidiary": "0",
"children": []
}]
}]
}]
}, {
"id": "1004482740",
"parent": "1004482736",
"industry": "Akrotiri",
"industry_id": "industry16",
"name": "Comp1121",
"percentage": "100.00",
"report": "null",
"description": "",
"subsidiary": "1",
"children": [{
"id": "1004482741",
"parent": "1004482740",
"industry": "Akrotiri",
"industry_id": "industry17",
"name": "Comp11210",
"percentage": "100.00",
"report": "null",
"description": "",
"subsidiary": "1",
"children": [{
"id": "aa02637",
"parent": "1004482741",
"industry": "",
"industry_id": "0",
"name": "Sef 7",
"percentage": "7",
"report": "null",
"description": "",
"subsidiary": "0",
"children": []
}]
}]
}, {
"id": "1004482742",
"parent": "1004482736",
"industry": "Akrotiri",
"industry_id": "industry18",
"name": "Comp1122",
"percentage": "30.00",
"report": "null",
"description": "",
"subsidiary": "2",
"children": [{
"id": "1004482743",
"parent": "1004482742",
"industry": "Akrotiri",
"industry_id": "industry19",
"name": "Comp11220",
"percentage": "100.00",
"report": "null",
"description": "",
"subsidiary": "1",
"children": [{
"id": "aa02638",
"parent": "1004482743",
"industry": "",
"industry_id": "0",
"name": "Sef 8",
"percentage": "8",
"report": "null",
"description": "",
"subsidiary": "0",
"children": []
}]
}]
}]
}]
}, {
"id": "1004482726",
"parent": "1004482725",
"industry": "Akrotiri",
"industry_id": "industry2",
"name": "Comp10",
"percentage": "30",
"report": "null",
"description": "",
"subsidiary": "1",
"children": [{
"id": "1004482732",
"parent": "1004482726",
"industry": "Akrotiri",
"industry_id": "industry8",
"name": "Comp102",
"percentage": "30",
"report": "null",
"description": "",
"subsidiary": "1",
"children": [{
"id": "aa02633",
"parent": "1004482732",
"industry": "",
"industry_id": "0",
"name": "Sef 3",
"percentage": "3",
"report": "null",
"description": "",
"subsidiary": "0",
"children": []
}]
}, {
"id": "1004482728",
"parent": "1004482726",
"industry": "Akrotiri",
"industry_id": "industry4",
"name": "Comp101",
"percentage": "30",
"report": "null",
"description": "",
"subsidiary": "1",
"children": [{
"id": "1004482729",
"parent": "1004482728",
"industry": "Akrotiri",
"industry_id": "industry5",
"name": "Comp1010",
"percentage": "100.00",
"report": "null",
"description": "",
"subsidiary": "1",
"children": [{
"id": "1004482730",
"parent": "1004482729",
"industry": "Akrotiri",
"industry_id": "industry6",
"name": "Comp10100",
"percentage": "100.00",
"report": "null",
"description": "",
"subsidiary": "1",
"children": [{
"id": "1004482731",
"parent": "1004482730",
"industry": "Akrotiri",
"industry_id": "industry7",
"name": "Comp101000",
"percentage": "100.00",
"report": "null",
"description": "",
"subsidiary": "1",
"children": [{
"id": "aa02632",
"parent": "1004482731",
"industry": "",
"industry_id": "0",
"name": "Sef 2",
"percentage": "2",
"report": "null",
"description": "",
"subsidiary": "0",
"children": []
}]
}]
}]
}]
}, {
"id": "1004482727",
"parent": "1004482726",
"industry": "Akrotiri",
"industry_id": "industry3",
"name": "Comp100",
"percentage": "30",
"report": "null",
"description": "",
"subsidiary": "1",
"children": [{
"id": "aa02631",
"parent": "1004482727",
"industry": "",
"industry_id": "0",
"name": "Sef1",
"percentage": "1",
"report": "null",
"description": "",
"subsidiary": "0",
"children": []
}]
}]
}, {
"id": "1004482744",
"parent": "1004482725",
"industry": "Akrotiri",
"industry_id": "industry20",
"name": "Comp12",
"percentage": "30",
"report": "null",
"description": "",
"subsidiary": "1",
"children": [{
"id": "1004482745",
"parent": "1004482744",
"industry": "Akrotiri",
"industry_id": "industry21",
"name": "Comp120",
"percentage": "30.00",
"report": "null",
"description": "",
"subsidiary": "1",
"children": [{
"id": "",
"parent": "",
"industry": "",
"industry_id": "",
"name": "",
"percentage": "",
"report": "",
"description": "",
"subsidiary": "",
"children": ""
}, {
"id": "1004482748",
"parent": "1004482745",
"industry": "Akrotiri",
"industry_id": "industry24",
"name": "Comp1210",
"percentage": "50.00",
"report": "null",
"description": "",
"subsidiary": "2",
"children": [{
"id": "1004482749",
"parent": "1004482748",
"industry": "Akrotiri",
"industry_id": "industry25",
"name": "Comp12100",
"percentage": "100.00",
"report": "null",
"description": "",
"subsidiary": "1",
"children": [{
"id": "1004482750",
"parent": "1004482749",
"industry": "Akrotiri",
"industry_id": "industry26",
"name": "Comp121000",
"percentage": "100.00",
"report": "null",
"description": "",
"subsidiary": "1",
"children": [{
"id": "aa02639",
"parent": "1004482750",
"industry": "",
"industry_id": "0",
"name": "Sef 9",
"percentage": "9",
"report": "null",
"description": "",
"subsidiary": "0",
"children": []
}]
}]
}]
}, {
"id": "",
"parent": "",
"industry": "",
"industry_id": "",
"name": "",
"percentage": "",
"report": "",
"description": "",
"subsidiary": "",
"children": ""
}]
}, {
"id": "1004482746",
"parent": "1004482744",
"industry": "Akrotiri",
"industry_id": "industry22",
"name": "Comp121",
"percentage": "30.00",
"report": "null",
"description": "",
"subsidiary": "1",
"children": [{
"id": "",
"parent": "",
"industry": "",
"industry_id": "",
"name": "",
"percentage": "",
"report": "",
"description": "",
"subsidiary": "",
"children": ""
}]
}, {
"id": "1004482747",
"parent": "1004482744",
"industry": "Akrotiri",
"industry_id": "industry23",
"name": "Comp122",
"percentage": "30.00",
"report": "null",
"description": "",
"subsidiary": "1",
"children": [{
"id": "aa02640",
"parent": "1004482747",
"industry": "",
"industry_id": "0",
"name": "Sef 10",
"percentage": "10",
"report": "null",
"description": "",
"subsidiary": "0",
"children": []
}]
}]
}]
}
In this example i have some elements that looks like this:
"children": [{
"id": "",
"parent": "",
"industry": "",
"industry_id": "",
"name": "",
"percentage": "",
"report": "",
"description": "",
"subsidiary": "",
"children": ""
}
What i would like to achive is to eliminate this "childrens" from the original array.
So far i've tried this function but it doesn't work the way i want:
function deleteEmpty(data){
if (data.children.length)
{
nr = data.children.length;
for (i=0;i<nr;i++)
{
// console.log(data.children[i]);
if (data.children[i]["id"] == ""){
console.log(data.children[i]);
delete data.children[i];
}
this.deleteEmpty(data.children[i]);
}
}
return data;
}
Also i have creatd a jsfiddle here : http://jsfiddle.net/fP7NS/2/
Please help me out or give me some hints with what i am doing wrong.
First (if necessary) turn your JSON string into an object. Your children property is sometimes an array, sometimes an empty array, and sometimes an empty string. This makes the logic a bit awkward. i suggest you write a function that tests whether a particular child is empty. Then you can write your main loop like this:
function isEmpty(node) {
// logic for testing that a node is empty
}
function deleteEmptyChildren(node) {
// assumes node is not empty
if (Array.isArray(node.children)) {
node.children = node.children.filter(
function(child) {
if (isEmpty(child)) {
return false;
} else {
deleteEmptyChildren(child);
return true;
}
}
);
}
}