Changing returned ajax data structure
Changing returned ajax data structure
classic12
Posts: 228Questions: 60Answers: 4
I have produced some data from the user_ files, files and my own quote table using this MYSQL statement.
"SELECT
a.quoteID, c.filename
FROM quotes a
INNER JOIN users_files b on b.user_id = a.quoteID
INNER JOIN files c on c.id = b.file_id"
I get the data back as:
[{"quoteID":"1","quoteTitle":"Title for 1","notes":"Deal no 1","web_path":"\/surplusAdmin3\/upload\/23.pdf"},{"quoteID":"1","quoteTitle":"Title for 1","notes":"Deal no 1","web_path":"\/surplusAdmin3\/upload\/22.jpeg"},{"quoteID":"1","quoteTitle":"Title for 1","notes":"Deal no 1","web_path":"\/surplusAdmin3\/upload\/21.jpeg"},{"quoteID":"1","quoteTitle":"Title for 1","notes":"Deal no 1","web_path":"\/surplusAdmin3\/upload\/20.jpeg"},{"quoteID":"2","quoteTitle":"Kaitlin","notes":"Smith","web_path":"\/surplusAdmin3\/upload\/24.jpg"},{"quoteID":"8","quoteTitle":"Bryar2","notes":"Long","web_path":"\/surplusAdmin3\/upload\/17.png"},{"quoteID":"11","quoteTitle":"Avram","notes":"Allison","web_path":"\/surplusAdmin3\/upload\/4.jpg"}]
I need the data as follows
[{"quoteID":"1","quoteTitle":"Title for 1","notes":"Deal no 1","web_path":"\/surplusAdmin3\/upload\/23.pdf","web_path1":"\/surplusAdmin3\/upload\/22.jpeg,"web_path2":"\/surplusAdmin3\/upload\/21.jpeg","web_path3":"\/surplusAdmin3\/upload\/20.jpeg"},{"quoteID":"2","quoteTitle":"Kaitlin","notes":"Smith","web_path":"\/surplusAdmin3\/upload\/24.jpg"}]
So I have used the following to try this
// loop through and add field 'Type' can all be done in one loop.
dataReturned.forEach(function(e){
if (typeof e === "object" ){
e["Type"] = "other";
// console.log('stuff '+e['Type'][i] )
}
});
//loop through and workout the file type
dataReturned.forEach(function(e){
if (typeof e === "object" ){
var ext = e['web_path'].substr(e['web_path'].lastIndexOf('.') + 1);
if ( (ext === 'jpeg' ) || ( ext === 'jpg' )|| ( ext === 'png' )|| ( ext === 'gif' ) || ( ext === 'pdf' ))
{ e["Type"] = "image"; }
//console.log(e['web_path']);
// console.log('stuff '+ JSON.stringify(e))
}
});
// create a new array formated as needed
var currentQuoteID =0;
var dataChanged = [];
var arrayRow = -1 ;
var fileCount = 0;
dataReturned.forEach(function(e){
// now loop through and get the quoteID and add a new row to a new array ( copying the row data)
// if the next row = same quoteID push the new web_path to the existing & repeat.
// if not the same quoteID add a new row. We will end up with one row for each quoteID and x umber of files in each row.
console.log(' current quote '+ currentQuoteID + ' = '+ e["quoteID"] + 'file count = '+ fileCount);
if ( currentQuoteID !== e["quoteID"]) {dataChanged.push(e); arrayRow = arrayRow + 1; fileCount = 1}
else
{
console.log('just add the filename array Row = ' +arrayRow );
// need to insert to the array arrayRow
// dataChanged.push('web_path'+fileCount,e["web_path"]);
toPush = 'web_path'+fileCount+'"'+':'+'"'+ e["web_path"];
dataChanged.push(toPush);
fileCount = fileCount +1;
}
currentQuoteID = e["quoteID"];
console.log('stuff '+ JSON.stringify(dataChanged));
});
I am close as my result is
[{"quoteID":"1","quoteTitle":"Title for 1","notes":"Deal no 1","web_path":"/surplusAdmin3/upload/23.pdf","Type":"image"},"web_path1\":\"/surplusAdmin3/upload/22.jpeg","web_path2\":\"/surplusAdmin3/upload/21.jpeg"]
So I need to insert at the end of the current array instead of the push.
What is the correct syntax please
dataChanged.push(toPush);
to
dataChanged[arrayRow].toPush
var currentQuoteID =0;
var dataChanged = [];
var arrayRow = -1 ;
var fileCount = 0;
dataReturned.forEach(function(e){
// now loop through and get the quoteID and add a new row to a new array ( copying the row data)
// if the next row = same quoteID push the new web_path to the existing & repeat.
// if not the same quoteID add a new row. We will end up with one row for each quoteID and x umber of files in each row.
console.log(' current quote '+ currentQuoteID + ' = '+ e["quoteID"] + 'file count = '+ fileCount);
if ( currentQuoteID !== e["quoteID"]) {dataChanged.push(e); arrayRow = arrayRow + 1; fileCount = 1}
else
{
console.log('just add the filename array Row = ' +arrayRow );
// need to insert to the array arrayRow
// dataChanged.push('web_path'+fileCount,e["web_path"]);
toPush = 'web_path'+fileCount+'"'+':'+'"'+ e["web_path"];
dataChanged.push(toPush);
fileCount = fileCount +1;
}
currentQuoteID = e["quoteID"];
console.log('stuff '+ JSON.stringify(dataChanged));
});
Cheers
Steve Warby
This discussion has been closed.
Answers
You want to add extra data to each object in the array? The MDN documentation for
forEach
says that the first parameter passed in to the callback is the item in the array - so you could use:to modify those objects.
Allan
How do I edit the post. It was a late night last night and its not formatted correctly.
e is the dataReturned array I need to update the dataChanged array.
So I now have
I now need to get my result as:
I need to insert at the end not add.
I've tried various approaches.
I thought I could do
```
dataChanged[arrayRow].'web_path'+fileCount = 'web_path'+fileCount
Heres hoping....
Cheers
Steve Warby
I've edited it now. You only get 5 minutes to edit a post I'm afraid.
would be the Javascript syntax to update a variable
web_path{i}
inside the arrayRow item.Allan
Great guys,
thanks for all the help.
I actually use "
webPath allows me to change the server.
I now populate the rows with web_path, web_path1 etc but some have no value. Is there a option to hide if undefined ?
Also on iphone if I rotate the device there is a large wasted area on both sides.
I have tried
http://toolfolks.com/surplusAnywhere4/
I am using responsive plugin.
Cheers
Steve Warby
Try adding
width="100%"
to your table in HTML. For example:<table id="example" class="display" width="100%"></table>
Kevin