如何解决基于 foreach 循环索引的动态数组名称 forEach((key) => { const temp[{ key }] }
我正在尝试格式化从 JSON 对象获得的一些数据。
该对象有一个视频源:video.videodata
以及视频转码的分辨率:video.transcoded
资料:
IObjectReference.GetRealObject
我想将数据格式化为:
"video": [
{
"videodata": "https://example.com/link/to/firstTestvideo.mp4","transcoded": [
"-360","-480"
]
},{
"videodata": "https://example.com/link/to/firstTestvideo.mp4","-480","-720"
]
}
],
到目前为止我得到了:
videos: [ //global
video: [
0: [
{
src: "https://example.com/link/to/firstTestvideo-360.mp4",resolution: "-360p",},{
src: "https://example.com/link/to/firstTestvideo-480.mp4",resolution: "-480p",{
src: "https://example.com/link/to/firstTestvideo.mp4",resolution: "full",],1: [
{
src: "https://example.com/link/to/secondTestvideo-360.mp4",{
src: "https://example.com/link/to/secondTestvideo-480.mp4",{
src: "https://example.com/link/to/secondTestvideo-720.mp4",resolution: "-720",{
src: "https://example.com/link/to/secondTestvideo.mp4",
任何想法如何创建视频的“子阵列”?
由于视频的数量是动态的,我不能做视频[0]、视频[1]等
有没有更好的方法来格式化数据然后是字符串连接?
不幸的是,我无法更改被调用的 JSON
的布局
非常感谢任何想法!
解决方法
只需检查这个小提琴并更改它:
const fnl = []
vidoes.forEach(index => fnl.push( ...index.transcoded.map(vd => ({
src: index.videodata,resolution: vd + 'p'
})))
,
显式的 0:
,1:
东西似乎没有增加任何好处,所以这用一个简单的数组来实现。所需的示例访问 videos.video[0][0]
也以这种方式工作,如末尾所示:
const whatever = {
"video": [{
"videodata": "https://example.com/link/to/firstTestvideo.mp4","transcoded": [
"-360","-480"
]
},{
"videodata": "https://example.com/link/to/firstTestvideo.mp4","-480","-720"
]
}
]
};
const videos = {
video: whatever.video.map(item => [...item.transcoded,"full"].map(resolution => ({
src: item.videodata,resolution
})))
};
console.log("videos:");
console.log(videos);
console.log("---");
console.log("videos.video[0][0]:");
console.log(videos.video[0][0]);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。