1

I have model

function row() {
    this.name = '';
    this.rulingValue = '';
    this.pathToImage = '';
    this.rulingType = '';
    this.dateStart = '';
    this.dateEnd = '';
}

and array var jsonData = [];

I have table

<table class="table" id="data-table">
    <tbody>
    </tbody>
</table>

and button to add row ti this table. When I add row to table I add it to array too.

methos for add row to array

function add() {
    var item = new row();
    item.name = document.getElementById("ruling-name").value;
    item.pathToImage = document.getElementById("path-to-image").value;
    item.rulingValue = document.getElementById("ruling-value").value;
    item.rulingType = document.getElementById("ruling-type").value;
    item.dateStart = document.getElementById("dtp_input1").value;
    item.dateEnd = document.getElementById("dtp_input2").value;
    jsonData.push(item);
    ddRow(item);
}

method for add row to table

function ddRow(item) {
    const newRow = tbody.insertRow(tbody.rows.length);
    newRow.insertCell(0).appendChild(document.createTextNode(item.name));
    newRow.insertCell(1).appendChild(document.createTextNode(item.pathToImage));        
    newRow.insertCell(2).appendChild(document.createTextNode(item.rulingValue));
    newRow.insertCell(3).appendChild(document.createTextNode(item.rulingType));
    newRow.insertCell(4).appendChild(document.createTextNode(item.dateStart));
    newRow.insertCell(5).appendChild(document.createTextNode(item.dateEnd));
    var btn = document.createElement("BUTTON");
    btn.setAttribute('type', 'button');
    btn.onclick = function () {
        $(this).closest('tr').remove();
    };
    var t = document.createTextNode("delete");
    btn.appendChild(t);
    newRow.insertCell(6).appendChild(btn);
}

All work fine But I can not remove current row from array

$(this).closest('tr').remove(); // removed row from table

But how can I removed this row from array?

EDIT

Yes I see this solution

var array = [2, 5, 9];
var index = array.indexOf(5);
if (index > -1) {
    array.splice(index, 1);
}

But How can I make this?

var index = jsonData .indexOf(??????????????????????);

I tried

    var row = this.parentNode.parentNode;
    var index = jsonData.indexOf(row)
    alert(index);

and I have "udefined"

my array in json enter image description here

user5620472
  • 2,352
  • 5
  • 36
  • 82

1 Answers1

1

To find index of item based on your condition you can use findIndex function on the array.

jsonData.findIndex(function(item){ return item.name === "My name" })

The function is not implemented in IE, but you can use polyfill from the link above.

Aik
  • 2,723
  • 2
  • 15
  • 17
  • names in array maybe equals – user5620472 Nov 01 '16 at 08:07
  • 1
    Since you are already using ES6, you can use arrow function to make this even more elegant. `jsonData.findIndex(item => item.name === "My name")` – Tomas Nikodym Nov 01 '16 at 08:08
  • I tell you that the array can be elements of the same name. where does the arrow? how do I know the index of an element? – user5620472 Nov 01 '16 at 08:10
  • function(item){ return item.name === "My name" } inside an findIndex helps you to find correct element. In this example it try to find index of item which name is "My name" – Aik Nov 01 '16 at 08:17
  • and if I have an array of such ["pavel","pavel","pavel","pavel","pavel"] – user5620472 Nov 01 '16 at 08:19
  • jsonData.indexOf("pavel") is good enough in this case. The difference is that `indexOf` search by the item value and `findIndex` by the condition. This is why for your complex structure is better `findIndex` – Aik Nov 01 '16 at 08:23
  • or do you want to remove all "pavel" occurrence? in this case you have to do: let index = jsonData.indexOf("pavel") while (index!==-1 ){ // do what you want with index index = jsonData.indexOf("pavel") } – Aik Nov 01 '16 at 08:24
  • I want pres to row in table and delete this row from this table and from array becouse when I press add I add row to table and to array!!! – user5620472 Nov 01 '16 at 08:34