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.
Related
So I have two arrays with objects. And also an array indicating where the replacement should take place(It spots if object contains error)
const oldData = [
{
"index": "01",
"skuId": "Sarbb-033",
"name": "Sasko Black",
"barcode": "843331510012",
"description": "Nice black bread",
"brand": "ERROR: No brand matching: Sasko",
"productLine": "ERROR: No product line matching: line",
"inputType": "Weight",
"uom": "ERROR: Invalid UoM type, valid values are: kg,g,mg,kl,l,ml,m,cm,mm",
"value": "700",
"capacity": "1",
"image": ""
},
{
"index": "02",
"skuId": "ERROR: Empty sku_id is not allowed",
"name": "Sasko Black1",
"barcode": "ERROR: Empty barcode is not allowed",
"description": "Nice black bread",
"brand": "ERROR: No brand matching: Future Life",
"productLine": "ERROR: No product line matching: line",
"inputType": "Weight",
"uom": "kg",
"value": "701",
"capacity": "2",
"image": ""
},
{
"index": "03",
"skuId": "Sarbb-099",
"name": "Sasko Black100",
"barcode": "843332555614",
"description": "Nice black bread",
"brand": "fwfwf",
"productLine": "naam",
"inputType": "weight",
"uom": "g",
"value": "702",
"capacity": "3",
"image": ""
},
{
"index": "04",
"skuId": "Sarbb-100",
"name": "Sasko Black101",
"barcode": "ERROR: Empty barcode is not allowed",
"description": "ERROR: Invalid description: [] it should not be blank.",
"brand": "fwfwf",
"productLine": "fwfwf",
"inputType": "Weight",
"uom": "g",
"value": "703",
"capacity": "4",
"image": ""
},
{
"index": "05",
"skuId": "Sarbb-101",
"name": "Sasko Black102",
"barcode": "843332555616",
"description": "Nice black bread",
"brand": "fwfwf",
"productLine": "naam",
"inputType": "weight",
"uom": "g",
"value": "704",
"capacity": "5",
"image": ""
}
]
const newData = [
{
"index": "01",
"skuId": "Sarbb-033",
"name": "Sasko Black",
"barcode": "843331510012",
"description": "Nice black bread",
"brand": "fwfwf",
"productLine": "fwfwf",
"inputType": "Weight",
"uom": "g",
"value": "700",
"capacity": "1",
"image": ""
},
{
"index": "02",
"skuId": "sarb",
"name": "Sasko Black1",
"barcode": "124125125",
"description": "Nice black bread",
"brand": "fwfwf",
"productLine": "fwfwf",
"inputType": "Weight",
"uom": "kg",
"value": "701",
"capacity": "2",
"image": ""
},
{
"index": "03",
"skuId": "Sarbb-100",
"name": "Sasko Black101",
"barcode": "214214214",
"description": "Desc",
"brand": "fwfwf",
"productLine": "fwfwf",
"inputType": "Weight",
"uom": "g",
"value": "703",
"capacity": "4",
"image": ""
}
]
const errorRows = [0,1,3]
const myTerribleAttempt = oldTableData.map((oldData, rowIndex) => {
return errorRows.map(index => {
if(rowIndex === index){
newTableData.map(newData => {
oldData = newData
})
}
})
})
I have tried multiple maps and just can't seem to get the right result. The new data objects should replace old data objects at the object containing the error. Please give me some assistance.
I think you've overcomplicated this - just check if the index is contained in the error array and choose old or new data.
const result = oldData.map( (item, idx) => errorRows.includes(idx)
? newData[errorRows.findIndex(x => x == idx)]
: item);
live example:
const oldData = [{
"index": "01",
"skuId": "Sarbb-033",
"name": "Sasko Black",
"barcode": "843331510012",
"description": "Nice black bread",
"brand": "ERROR: No brand matching: Sasko",
"productLine": "ERROR: No product line matching: line",
"inputType": "Weight",
"uom": "ERROR: Invalid UoM type, valid values are: kg,g,mg,kl,l,ml,m,cm,mm",
"value": "700",
"capacity": "1",
"image": ""
},
{
"index": "02",
"skuId": "ERROR: Empty sku_id is not allowed",
"name": "Sasko Black1",
"barcode": "ERROR: Empty barcode is not allowed",
"description": "Nice black bread",
"brand": "ERROR: No brand matching: Future Life",
"productLine": "ERROR: No product line matching: line",
"inputType": "Weight",
"uom": "kg",
"value": "701",
"capacity": "2",
"image": ""
},
{
"index": "03",
"skuId": "Sarbb-099",
"name": "Sasko Black100",
"barcode": "843332555614",
"description": "Nice black bread",
"brand": "fwfwf",
"productLine": "naam",
"inputType": "weight",
"uom": "g",
"value": "702",
"capacity": "3",
"image": ""
},
{
"index": "04",
"skuId": "Sarbb-100",
"name": "Sasko Black101",
"barcode": "ERROR: Empty barcode is not allowed",
"description": "ERROR: Invalid description: [] it should not be blank.",
"brand": "fwfwf",
"productLine": "fwfwf",
"inputType": "Weight",
"uom": "g",
"value": "703",
"capacity": "4",
"image": ""
},
{
"index": "05",
"skuId": "Sarbb-101",
"name": "Sasko Black102",
"barcode": "843332555616",
"description": "Nice black bread",
"brand": "fwfwf",
"productLine": "naam",
"inputType": "weight",
"uom": "g",
"value": "704",
"capacity": "5",
"image": ""
}
]
const newData = [{
"index": "01",
"skuId": "Sarbb-033",
"name": "Sasko Black",
"barcode": "843331510012",
"description": "Nice black bread",
"brand": "fwfwf",
"productLine": "fwfwf",
"inputType": "Weight",
"uom": "g",
"value": "700",
"capacity": "1",
"image": ""
},
{
"index": "02",
"skuId": "sarb",
"name": "Sasko Black1",
"barcode": "124125125",
"description": "Nice black bread",
"brand": "fwfwf",
"productLine": "fwfwf",
"inputType": "Weight",
"uom": "kg",
"value": "701",
"capacity": "2",
"image": ""
},
{
"index": "03",
"skuId": "Sarbb-100",
"name": "Sasko Black101",
"barcode": "214214214",
"description": "Desc",
"brand": "fwfwf",
"productLine": "fwfwf",
"inputType": "Weight",
"uom": "g",
"value": "703",
"capacity": "4",
"image": ""
}
]
const errorRows = [0, 1, 3];
const result = oldData.map((item, idx) => errorRows.includes(idx) ? newData[idx] : item);
console.log(result);
I want to merge two multi-level object to form a single object with no loss of data.
till the level data is same no extra fields should be added, at level where fields are different new object should get inserted in the array.
we have to group json object of similar fields in one complete json.
multilevel object needs to be merged together to get one aggragated result.
my sample objects are as -
let one={
"name": "Hindi",
"country": "USA",
"region": "EAST COAST",
"enttity": "ENTITY-2",
"operation": [
{
"p_name": "RECONSTRUCTION",
"code": "jhhkj-132",
"class": "medical",
"products": [
{
"main": "electronic",
"area": [
{
"subarea": "electrical",
"use": 0,
"max_use": 0,
"mode_use": "10",
"median": "12",
"things": [
{
"id": "1000514",
"number": "1588TB-1",
"description": "DESCRIPTION1",
"manufacturer": "LG",
"tag": "NOT AVAILABLE",
}
]
}
]
}
]
}
]
};
let two={
"name": "Hindi",
"country": "USA",
"region": "EAST COAST",
"enttity": "ENTITY-2",
"operation": [
{
"p_name": "RECONSTRUCTION",
"code": "jhhkj-132",
"class": "medical",
"products": [
{
"main": "electronic",
"area": [
{
"subarea": "mechanical",
"use": 0,
"max_use": 0,
"mode_use": "10",
"median": "12",
"things": [
{
"id": "1000523314",
"number": "1588T3B-1",
"description": "DES32CRIPTION1",
"manufacturer": "LG",
"tag": "NOT AVAILABLE",
}
]
},
{
"subarea": "electrical",
"use": 0,
"max_use": 0,
"mode_use": "10",
"median": "12",
"things": [
{
"id": "1000514",
"number": "1588TB-1",
"description": "DESCRIPTION1",
"manufacturer": "LG",
"tag": "NOT AVAILABLE",
}
]
}
]
}
]
}
]
};
output json should be something like this->
let outpus={
"name": "Hindi",
"country": "USA",
"region": "EAST COAST",
"enttity": "ENTITY-2",
"operation": [
{
"p_name": "RECONSTRUCTION",
"code": "jhhkj-132",
"class": "medical",
"products": [
{
"main": "electronic",
"area": [
{
"subarea": "mechanical",
"use": 0,
"max_use": 0,
"mode_use": "10",
"median": "12",
"things": [
{
"id": "1000523314",
"number": "1588T3B-1",
"description": "DES32CRIPTION1",
"manufacturer": "LG",
"tag": "NOT AVAILABLE",
}
]
},
{
"subarea": "electrical",
"use": 0,
"max_use": 0,
"mode_use": "10",
"median": "12",
"things": [
{
"id": "1000514",
"number": "1588TB-1",
"description": "DESCRIPTION1",
"manufacturer": "LG",
"tag": "NOT AVAILABLE",
}
]
}
]
}
]
}
]
};
I have to group products from the products array based on the season which is given in the orders array. The orders array has the season key-value pair (eg. "season": "2015" ). The products can be mapped to the individual order object with the "id" of the order array and the "orderlineId" of the products array.
When I have the products grouped by season, they also need to be grouped based on "uniqueId" and "colorCode" which is inside product.uniqueId and product.colorCode.
Orders array
{
"id": 99945333,
"key": "1",
"orderNumber": "01",
"season": "2007"
},
{
"id": 99945335,
"key": "1",
"orderNumber": "02",
"season": "2016"
},
{
"id": 99945333,
"key": "2",
"orderNumber": "03",
"season": "2019"
},
{
"id": 99945333,
"key": "3",
"orderNumber": "04",
"season": "2017"
}
]
products array
"orderlineId": 99945333,
"product": {
"season": null,
"size": "XXL",
"category: "pants"
"sizeSortCode": "60",
"subSize": null,
"subSizeSortCode": "0",
"uniqueId": "80457",
"year": null
},
"quantity": 1,
"quantityDelivered": 0,
"remark": null
},
{
"orderlineId": 99945333,
"product": {
"season": null,
"size": "XXL",
"category: "pants"
"sizeSortCode": "60",
"subSize": null,
"subSizeSortCode": "0",
"uniqueId": "80457",
"year": null
},
"quantity": 1,
"quantityDelivered": 0,
"remark": null
},
{
"orderlineId": 99945335,
"product": {
"season": null,
"size": "XXL",
"category: "shirt"
"sizeSortCode": "60",
"subSize": null,
"subSizeSortCode": "0",
"uniqueId": "80457",
"year": null
},
"quantity": 1,
"quantityDelivered": 0,
"remark": null
},
{
"orderlineId": 99945335,
"product": {
"season": null,
"size": "XXL",
"category: "trouser"
"sizeSortCode": "60",
"subSize": null,
"subSizeSortCode": "0",
"uniqueId": "80453",
"year": null
},
"quantity": 1,
"quantityDelivered": 0,
"remark": null
},
{
"orderlineId": 99945473,
"product": {
"season": null,
"category: "blouse"
"size": "XXL",
"sizeSortCode": "60",
"subSize": null,
"subSizeSortCode": "0",
"uniqueId": "80453",
"year": null
},
"quantity": 1,
"quantityDelivered": 0,
"remark": null
},
I think a new array would be best here so I iterate over them more easily.
desired outcome: new array
{
"season": 2007,
"products": [
{
"season": null,
"size": "XXL",
"category: "pants"
"sizeSortCode": "60",
"subSize": null,
"subSizeSortCode": "0",
"uniqueId": "80453",
"year": null
}
{
"season": null,
"size": "XXL",
"sizeSortCode": "60",
"subSize": null,
"subSizeSortCode": "0",
"uniqueId": "80453",
"year": null
}
},
{
"season": 2016,
"products": [
{
"season": null,
"size": "XXL",
"sizeSortCode": "60",
"subSize": null,
"subSizeSortCode": "0",
"uniqueId": "80457",
"year": null
},
},
{
"season": null,
"size": "XXL",
"sizeSortCode": "60",
"subSize": null,
"subSizeSortCode": "0",
"uniqueId": "80453",
"year": null
}
}
]
I found it quite tricky to do. I assume I should map over the seasons first, but not sure how to group the products and build a new array. Help is much appreciated!
Ah,
I think I found a sufficient answer to my own question. It's not exactly like the desired output, but I can work with it!. I used:
const sortedSeasons = orders.concat(products).reduce((acc, currentVal) => {
acc[currentVal.season] = Object.assign(acc[currentVal.id] || {}, currentVal);
return acc;
}, {});
Object.keys(sortedSeasons).map(i => sortedSeasons[i]);
I downloaded some JSON, as shown below. In the developer console it looks like an object containing nested objects which contain nested objects ...
I would like to add a new, empty "campaign" into the data (at the front). How do I do that?
Insert
var blankCampaignData = {'title': '', 'description': '', 'path_to_logo': '', 'start_time': date, 'end_time' : date, 'paused': false};
into
{
"campaigns": {
"1": {
"campaign_id": "1",
"title": "Nike Air 2015 campaign",
"description": null,
"path_to_logo": null,
"start_time": "09/11/2015 22:42:08",
"end_time": "09/03/2016 22:42:08",
"paused": "0",
"destinations": {
"1": {
"destination_id": "1",
"campaign_id": "1",
"url": "www.nike.com/nike_air",
"description": "Nike air destination",
"connections": {
"3": {
"connection_id": "3",
"destination_id": "1",
"tag_id": "0",
"country": "Scotland",
"county": "Yorkshire",
"town": "East Ham",
"post_code": "SE1 1AA",
"custom": "bus stop",
"description": "Connection number 3"
}
}
},
"2": {
"destination_id": "2",
"campaign_id": "1",
"url": "www.nike.com/nike_air/sub_campaign",
"description": "Nike air - free laces promotion destination",
"connections": {
"2": {
"connection_id": "2",
"destination_id": "2",
"tag_id": "0",
"country": "Engerland",
"county": "Devon",
"town": "East Ham",
"post_code": "SE1 1AA",
"custom": "bus stop",
"description": "Connection number 2"
},
"4": {
"connection_id": "4",
"destination_id": "2",
"tag_id": "0",
"country": "Engerland",
"county": "Yorkshire",
"town": "Felixswtowe",
"post_code": "RB3 9YR",
"custom": "police staticon",
"description": "Connection number 4"
},
"6": {
"connection_id": "6",
"destination_id": "2",
"tag_id": "0",
"country": "Scotland",
"county": "Essex",
"town": "York",
"post_code": "JD8 4LF",
"custom": "somewhere else",
"description": "Connection number 6"
},
"9": {
"connection_id": "9",
"destination_id": "2",
"tag_id": "0",
"country": "Scotland",
"county": "Cork",
"town": "York",
"post_code": "JD8 4LF",
"custom": "in the ladies' loo",
"description": "Connection number 9"
}
}
}
}
},
"2": {
"campaign_id": "2",
"title": "Nike football boots campaign",
"description": null,
"path_to_logo": null,
"start_time": "09/12/2015 22:42:08",
"end_time": "09/01/2016 22:42:08",
"paused": "0",
"destinations": {
"3": {
"destination_id": "3",
"campaign_id": "2",
"url": "www.nike.com/nike_football_boots/",
"description": "Nike footie boots destination",
"connections": {}
},
"4": {
"destination_id": "4",
"campaign_id": "2",
"url": "www.nike.com/nike_football_boots/sub_campaign",
"description": "Buy left boot, get right boot free destination",
"connections": {}
}
}
},
"3": {
"campaign_id": "3",
"title": "Nike general promotion campaign",
"description": null,
"path_to_logo": null,
"start_time": "09/12/2013 22:42:08",
"end_time": "09/08/2016 22:42:08",
"paused": "0",
"destinations": {
"5": {
"destination_id": "5",
"campaign_id": "3",
"url": "www.nike.com/general_promotion",
"description": "Nike general promotion destination",
"connections": {}
},
"6": {
"destination_id": "6",
"campaign_id": "3",
"url": "www.nike.com/general_promotion/discount_coupon",
"description": "20% off coupon destination",
"connections": {}
}
}
}
}
}
Work out what the next campaign id should be:
var nextId = +Object.keys(obj.campaigns).sort().pop() + 1;
Add the empty campaign to the campaigns object. Obviously you'll need to define date beforehand.
obj.campaigns[nextId] = {
'title': '',
'description': '',
'path_to_logo': '',
'start_time': date,
'end_time' : date,
'paused': false
}
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);