gpt4 book ai didi

javascript - 如何获取数组中的公共(public)值

转载 作者:行者123 更新时间:2023-12-05 02:48:17 25 4
gpt4 key购买 nike

例如我有一个数据数组如下

var arrData = ["40-25",null,null,"40-25","50-48",null,"30-25","40-23","50-48","30-25",null,"50-48","40-45","40-45","40-45","40-50","40-50",null,null,null,null,null,"50-48"]

我需要在 javascript 中列出与下面相同的数据

var arrDataSorted = ["40-25","50-48","30-25","40-23","40-45","40-50","40-50"]

只需要删除复制空值的公共(public)数据。

解决这个问题的最佳方案是什么。

最佳答案

您可以尝试使用 Array.prototype.filter()删除 null 值和 Set获得独特的值(value)。最后使用Spread syntax (...)将集合结果转换为数组。

尝试以下方式:

var arrData = ["40-25",null,null,"40-25","50-48",null,"30-25","40-23","50-48","30-25",null,"50-48","40-45","40-45","40-45","40-50","40-50",null,null,null,null,null,"50-48"];

var arrDataSorted = [...new Set(arrData.filter(i => i))];
console.log(arrDataSorted);

关于javascript - 如何获取数组中的公共(public)值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64629315/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com