Home>

// Show "I have a child" if there is an eggplant

I want to do this, but the array comparison doesn't work and it doesn't appear as expected.

Please see the sample because the code is long.
Sample: https://jsfiddle.net/psn5849w/

The sample code above is below.

<p>your number</p><p>ancestral number</p>
<div>
  <ul>
    <li>
      <div parent = "0" ancestor = "">X family
            <span>4</span><span></span>
      </div>
      <ul>
        <li>
          <div parent = "7" ancestor = "">A
            <span>4</span><span></span>
          </div>
        </li>
       </ul>
    </li>
    <li>
      <div parent = "0" ancestor = "">Y
        <span>4</span><span></span>
      </div>
      <ul>
        <li>
          <div parent = "151" ancestor = "">B
            <span>4</span><span></span>
          </div>
        </li>
        <li>
          <div parent = "151" ancestor = "">C
            <span>4</span><span></span>
          </div>
        </li>
        <li>
          <div parent = "354" ancestor = "">D (child of C)
            <span>4</span><span></span>
          </div>
        </li>
        <li>
          <div parent = "365" ancestor = "">E (child of D)
            <span>4</span><span></span>
          </div>
        </li>
        <li>
          <div parent = "503" ancestor = "">F (child of E)
            <span>4</span><span></span>
          </div>
        </li>
      </ul>
    </li>
    <li>
      <div parent = "0" ancestor = "">Z house
        <span>4</span><span></span>
      </div>
    </li>
  </ul>
</div>
// Concatenation of ➀ ancestor numbers
function getParentNums (list) {
  var parent_num = list.attr ('parent');
  if (parent_num == "0") {
  return ["0"];
  }
  var parent = $("# post-" + parent_num);
  var parent_nums = getParentNums (parent);
  parent_nums.push (parent_num);
  return parent_nums;
}
$('. list'). each (function () {
  var my_num = $(this) .attr ('id'). split ('-') [1];
  var list = $(this);
  var parents = getParentNums (list);
  var ancestor_num = parents.join ('-');
  list.attr ('ancestor', ancestor_num);
  // Display for confirmation
  $(this) .find ('. my_num'). text (my_num);
  $(this) .find ('. ancestor_num'). text (ancestor_num);
});

// Displays "I have a child" when there is an eggplant
$(". list"). each (function () {
  var my_num = $(this) .attr ('id'). split ('-') [1];
  var parent_num = $(this) .attr ('parent');
  var child_num = parent_num + '-' + my_num;
  var arr = [];
  arr.push (child_num);
  if (arr.indexOf (my_num)>= 0) {
    $(this) .append ('I have children');
  }
});


Like the comment out above

// Show "I have a child" if there is an eggplant

is not possible.

I would be happy if you could give me a brief explanation, and I asked you a question here.

  • Answer # 1

      

    // If you have a lion, display "I have a child"
      $(".list"). each (function () {
        var my_num = $(this) .attr ('id'). split ('-') [1];
        var parent_num = $(this) .attr ('parent');
        var child_num = parent_num +'-'+ my_num;
        var arr = [];
        arr.push (child_num);
        if (arr.indexOf (my_num)>= 0) {
          $(this) .append ('Children');
        }
      });

    In this code, there is only one child_num in the array because a new array is declared for each lap of each.

    array.indexOf is a method that returns the array number if there is a match with the argument given in the array

    If there is only one lap, an additional decision will be made that "I have a child" before the list of child_num is complete

    So if you write as follows, I think it will do what you want.

    // Initial declaration of parent num list
    var parents = [];
    // Create parent num list
    $(". list"). each (function () {
      var parent_num = $(this) .attr ('parent');
      parents.push (parent_num);
    });
    // If you have your num in the parent num list, show children
    $(". list"). each (function () {
      var my_num = $(this) .attr ('id'). split ('-') [1];
      if (parents.indexOf (my_num)>= 0) {
        $(this) .append ('I have children');
      }
    });