a_l_e_x
a_l_e_x

Reputation: 400

Sort the elements before inserting them into the table

I'm having a problem sorting the elements in the array based on the variable number. Once sorted, they must be inserted into the table. I try to use sort on item but I get the error.

I get the following error:

item.sort is not a function

How can I solve?

let myArr = [{
    type : "Image", 
    id_a: '123456', 
    number: 2, 
    id: 2,
    },{
    type: 'Text', 
    id_a: '123456', 
    number: 4, 
    id: 4
    },{
    type: 'Testo', 
    id_a: '564312', 
    number: 1, 
    id: 1},{
    type: 'Title', 
    id_a: '123456', 
    number: 3, 
    id: 3
    }
    ]
    
var id_url=123456;
var results = {};
for (var i = 0, len = myArr.length; i < len; i++) {
    var id_art = myArr[i].id_a;
    if (id_art == id_url) {
        results[i] = myArr[i];
        }
        
}

console.log(results);

 
Object.entries(results).forEach(item => {
    item = item[1];
    console.log(item)
    item.sort(function (a, b) {
           if (a.number != b.number) {
               return (a.number - b.number);
  }
 });
    let child = document.createElement("tr");
                 child.innerHTML = `
                    <td>${item.number}</td>
                    <td>${item.type}</td>`;
                    document.querySelector('#my-table').appendChild(child);
 })
<table id="my-table" width="90%">
                    <tr>
                        <th>Number</th>
                        <th>Type</th>
                        
                    </tr>

                </table>

Upvotes: 0

Views: 70

Answers (2)

k22pr
k22pr

Reputation: 199

Try the following.

It's easier to sort.

      const filterList = myArr.filter((now) => now.id_a == id_url);
      const sortList = filterList.sort((a, b) => a.number - b.number);
      const results = { ...sortList };

      for (const item of sortList) {
        let child = document.createElement("tr");
        child.innerHTML = `
                    <td>${item.number}</td>
                    <td>${item.type}</td>`;
        document.querySelector("#my-table").appendChild(child);
      }

Upvotes: 1

Raven
Raven

Reputation: 138

Your sorting function is correct but sort is a built-in function for arrays taking a function as the parameter. You can use it like below:

let myArr = [{
    type : "Image", 
    id_a: '123456', 
    value: 'moto_guzzi_v100_mandello.jpg', 
    number: 2, 
    id: 2,
    },{
    type: 'Text', 
    id_a: '123456', 
    value: 'The star of the Piaggio group stand is without doubt ... Discover all his secrets with our video', 
    number: 4, 
    id: 4
    },{
    type: 'Testo', 
    id_a: '564312', 
    value: 'Let find out together with our video', 
    number: 1, 
    id: 1},{
    type: 'Title', 
    id_a: '123456', 
    value: 'Moto Guzzi V100 Mandello, the queen of EICMA 2021', 
    number: 3, 
    id: 3
    }
    ]
    
var id_url=123456;
var results = {};
for (var i = 0, len = myArr.length; i < len; i++) {
    var id_art = myArr[i].id_a;
    if (id_art == id_url) {
        results[i] = myArr[i];
        }
        
}

console.log(results);

 
console.log("Before Sort", myArr);
const sortedArr =  myArr.sort(function (a, b) {
           if (a.number != b.number) {
               return (a.number - b.number);
  }
 });
console.log("After Sort", sortedArr);

Upvotes: 1

Related Questions