【问题标题】:find object with greatest number of elements找到元素数量最多的对象
【发布时间】:2021-10-05 14:22:47
【问题描述】:

我编写了一个函数,它将从联系人列表(对象数组)中返回具有最多孩子的名称,但它仅限于我在 if 语句中包含的元素数量。无论大小如何,如何让它遍历此联系人列表中的所有元素?我尝试创建一个 for of 循环,但它不允许我使用计数器“I”对元素进行排序,返回错误。

提前致谢!

let contacts = [
  {
    name: 'John',
    children:
      [
        { name: 'Mary', age: 11 }
      ]
  }, {
    name: 'Franklin',
    children:
      [
        { name: 'Susy', age: 7 },
        { name: 'Tom', age: 5 }
      ]
  }
];

function p_w_m_children(arr){
  let person_w_most_children = "";
  arr.sort((elem_1, elem_2) => {
    if (elem_1.children > elem_2.children){
      person_w_most_children = elem_1.name;
    }else{
      person_w_most_children = elem_2.name;
    }
  });
  return person_w_most_children;
}

console.log("The person who has most children is " + p_w_m_children(contacts)+ ".");

【问题讨论】:

  • 首先,您的比较 (elem_1.children > elem_2.children) 无效。使用该 sn-p 不会比较两个 children 数组的长度。相反,您需要使用elem_1.children.length > elem_2.children.length。我不确定 100%,但我相信您当前的解决方案会将数组隐式转换为字符串并按字母顺序排序。

标签: javascript arrays object javascript-objects


【解决方案1】:

您可以遍历数组并记录拥有最多孩子的联系人。如果孩子的数量更多,则将该记录替换为循环中的当前记录。

然后在循环结束时返回该联系人。

let contacts = [{
  name: 'John',
  children: [{
    name: 'Mary',
    age: 11
  }]
}, {
  name: 'Franklin',
  children: [{
      name: 'Susy',
      age: 7
    },
    {
      name: 'Tom',
      age: 5
    }
  ]
}];

function p_w_m_children(arr) {
  let person_w_most_children;

  contacts.forEach(contact => {
    if (!person_w_most_children || contact.children.length > person_w_most_children.children.length) {
      person_w_most_children = contact;
    }

  })


  return person_w_most_children;
}

console.log("The person who has most children is " + p_w_m_children(contacts).name + ".");

同上,不使用forEach

let contacts = [{
  name: 'John',
  children: [{
    name: 'Mary',
    age: 11
  }]
}, {
  name: 'Franklin',
  children: [{
      name: 'Susy',
      age: 7
    },
    {
      name: 'Tom',
      age: 5
    }
  ]
}];

function p_w_m_children(arr) {
  let person_w_most_children;

  for (let i = 0; i < contacts.length; i++) {
    if (!person_w_most_children || contacts[i].children.length > person_w_most_children.children.length) {
      person_w_most_children = contacts[i];
    }
  }


  return person_w_most_children;
}

console.log("The person who has most children is " + p_w_m_children(contacts).name + ".");

【讨论】:

  • 非常感谢!有没有办法在不使用 forEach 的情况下做到这一点?我不能用它来做这个练习。
  • 我添加了一个不使用forEach的示例
【解决方案2】:

要使用 arr.sort 查找拥有最多孩子的人,可以使用以下命令:

arr.sort((elem_1, elem_2) => {
    return elem_1.children.length - elem_2.children.length
}

关键点在于您比较数组的.length(而不是仅比较数组本身)并且.sort 回调返回一个可以排序的值。 elem_1.children.length &gt; elem_2.children.length 时返回正数,elem_1.children.length &lt; elem_2.children.length 时返回负数,相等时返回 0。这意味着 sort 函数可以正确地对数组进行排序。

然后,一旦对数组进行排序,您就可以简单地获取排序数组中的最后一个元素(具有最大值的元素)。

编辑澄清

let sortedArr = arr.sort((elem_1, elem_2) => {
    return elem_1.children.length - elem_2.children.length;
};
return sortedArr[sortedArr.length - 1].name;

【讨论】:

  • 非常感谢!但是如何从联系人列表中获取姓名?显然这仍然只适用于两个元素,我想要的是它可以与联系人列表中的任意数量的元素一起使用。
  • 这适用于任意数量的联系人。首先它对联系人列表进行排序,所以现在你有一个任意数量的联系人列表,按子项的数量排序,你只需要访问排序中的最后一个元素的名称数组.
  • 我添加了一个示例以澄清我的答案,它显示了如何从排序列表中获取具有最多子元素的元素的名称。
猜你喜欢
  • 2019-04-16
  • 2023-04-04
  • 2014-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-12
  • 1970-01-01
  • 2019-08-13
相关资源
最近更新 更多