Javascript 字符串中带点的字符串数组(嵌套)到字符串数组

Javascript 字符串中带点的字符串数组(嵌套)到字符串数组,javascript,arrays,Javascript,Arrays,我有以下数组: "country": ["name", "units.size", "units.weight", "formats.date", "formats.number"] 我希望在结果中获得以下数组: "country": ["name", "units", "formats"] 我只想得到对象名,不需要嵌套属性。 let country=[“名称”、“单位.大小”、“单位.重量”、“格式.日期”、“格式.数字”]; country=[…新集合(country.map(a=>a.

我有以下数组:

"country": ["name", "units.size", "units.weight", "formats.date", "formats.number"]
我希望在结果中获得以下数组:

"country": ["name", "units", "formats"]
我只想得到对象名,不需要嵌套属性。

let country=[“名称”、“单位.大小”、“单位.重量”、“格式.日期”、“格式.数字”];
country=[…新集合(country.map(a=>a.match(/\w+/)[0]));
控制台日志(国家)您可以使用
forEach()
在数组上迭代,并将每个类别推送到一个新数组中

在您的
forEach()
中,您可以使用
indexOf()
检查类别是否已经存在。

const countries={“country”:[“name”、“units.size”、“units.weight”、“forms.date”、“forms.number”];
countries.country=[…新集合(countries.country.map((项目)=>item.includes('.')?item.split('.')[0]:item))]

控制台日志(国家)
您也可以通过在数组中映射并拆分第一个
上的条目来实现这一点。之后,使用删除重复项:

let country=[“名称”、“单位.大小”、“单位.重量”、“格式.日期”、“格式.数字”];
让结果=[…新集合(country.map(x=>x.split('.')[0]))

console.log(结果)
您尝试了什么?