Javascript 基于键的动态数组从数组返回值
我有一个键数组,我想迭代其中的一个键并返回它们相应的值 一般来说,我知道,我可以只做一个Javascript 基于键的动态数组从数组返回值,javascript,jquery,arrays,json,ecmascript-6,Javascript,Jquery,Arrays,Json,Ecmascript 6,我有一个键数组,我想迭代其中的一个键并返回它们相应的值 一般来说,我知道,我可以只做一个对象。key\u name或对象[key\u name],但我有一个动态数组,将填充不同的键 数据: 数组格式:arr=['asd','fgh'] 我正在尝试这样做:letx=data.map(o=>arr.map(strs=>o[strs]) 结果: ["123", "345"] ["456", "df"] ["789", "345675"] ["123", "987"] 我有没有办法得到: ["123"
对象。key\u name
或对象[key\u name]
,但我有一个动态数组,将填充不同的键
数据:
数组格式:arr=['asd','fgh']
我正在尝试这样做:letx=data.map(o=>arr.map(strs=>o[strs])代码>
结果:
["123", "345"]
["456", "df"]
["789", "345675"]
["123", "987"]
我有没有办法得到:
["123", "456", "789", "123"] <= array for asd
["345", "df", "345675", "987"] <= array for fgh
[“123”、“456”、“789”、“123”]更改映射调用的顺序-首先迭代格式
数组,然后从数据中提取值
:
const data=[{“你好”:“abc”,“asd”:“123”,“fgh”:“345”},{“你好”:“sdf”,“asd”:“456”,“fgh”:“df”},{“你好”:“ty”,“asd”:“789”,“fgh”:“345675”},{“你好”:“qwe”,“asd”:“123”,“fgh”:“987”}]
常量格式=['asd','fgh']
const result=format.map(f=>data.map(o=>o[f]))
console.log(result)
更改映射调用的顺序-首先迭代格式
数组,然后从数据中提取值
:
const data=[{“你好”:“abc”,“asd”:“123”,“fgh”:“345”},{“你好”:“sdf”,“asd”:“456”,“fgh”:“df”},{“你好”:“ty”,“asd”:“789”,“fgh”:“345675”},{“你好”:“qwe”,“asd”:“123”,“fgh”:“987”}]
常量格式=['asd','fgh']
const result=format.map(f=>data.map(o=>o[f]))
console.log(result)
你几乎到了哪里!
您只需要从密钥数组开始:
arr.map(key => data.map(o => o[key]));
你快到了!
您只需要从密钥数组开始:
arr.map(key => data.map(o => o[key]));