2

I have any array and I want to first sort it by filenames alphabetically then output the result into the table.

I have created the array successfully but don't know how to sort and output the result into the table. My current code doesn't output anything into table but console.log shows data of array! hope some one help me fix this problems.Thanks

error on f12:JSON.parse: unexpected character

This is how I create the array:

files.push({ url: this.gsx$url.$t, filename: this.gsx$name.$t });

array structure in console.log output:

Object { url="http://someurl.com", filename="new file"},.......

javascript function:

function displayArray(files){ 
alert(files.length);
console.log(files);


var txt = files;

var files = $.parseJSON( txt ).files;

var $table = $( "<table></table>" );

for ( var i = 0; i < files.length; i++ ) {
    var emp = files[i];
    var $line = $( "<tr></tr>" );
    $line.append( $( "<td></td>" ).html( emp.url ) );
    $line.append( $( "<td></td>" ).html( emp.filename ) );
    $table.append( $line );
}

$table.appendTo(document.body);


};
7
  • 2
    txt contains invalid JSON. Are you sure you need to call $.parseJSON on it and it's not already a JavaScript array? Commented Oct 1, 2015 at 19:30
  • 1
    I think you just need to remove $.parseJSON Commented Oct 1, 2015 at 19:31
  • You don't have JSON, you have an array of object. JSON is a string. Commented Oct 1, 2015 at 19:33
  • 1
    thanks for replies. That fixed the problem . But why for some array data i get two array data in one table cell? How i can sort before i print ? Commented Oct 1, 2015 at 19:35
  • @ExplosionPills - This is not dealing with a string though, as your duplicate closure suggests. I am reopning this. It is more than likely a duplicate of another post, just not that one. Commented Oct 1, 2015 at 19:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.