javascript - find the length of array using map function ES6
问题描述
I'm trying to find the length of an array using ES6 using the below code but it does not work.
a=[[1,2,3,4],[4,5,6]]
result = a.map(d=>({d[0]:d.length}))
console.log(result)
This works:-
a=[[1,2,3,4],[4,5,6]]
result = a.map(d=>({name:d[0], length:d.length}))
console.log(result)
解决方案
使用 ES6,要从变量创建对象的键,您必须使用[]
.
例如:
const a = 'first name';
const name = {
[a]: 'john',
};
console.log(name);
所以这就是你要找的:
a=[[1,2,3,4],[4,5,6]]
result = a.map(d=>({[d[0]]:d.length}))
console.log(result)
推荐阅读
- c# - Swashbuckle / swagger 文档缺失信息
- sql - SQL Server,将 CASE 语句聚合到一行中
- php - 类型转换雄辩的关系 laravel
- java - 如何创建一串文本并将位置传递给 ListView 适配器?
- c++-cli - 无法将参数 1 从 'System::IO::StreamWriter ^' 转换为 Windows 形式的 'System::IO::Stream ^
- python - 我的结果被截断为一行。我如何让它打印所有内容?
- xcode - Xcode 11 添加新的约束集零:使用设置值而不是默认/标准
- php - 在插入符号处插入而不是替换整个文本区域
- c# - 使用 RestSharp 调用 OAuth1 api,使用的参数范围不起作用
- mysql - 带有字符串变量的mysql查询