[英]Javascript merge array of objects based on incrementing key
這就是我所擁有的:我想合並 object 哪個鍵以“path-”+i開頭。 並在最終結果中從鍵中刪除“path-i”。
var arr = [
{
"key": "path-0-mp4",
"value": [
"media/video/01.mp4",
"media/video/01_hd.mp4"
]
},
{
"key": "path-0-quality",
"value": [
"720p",
"1080p"
]
},
{
"key": "path-1-mp4",
"value": [
"media/video/02.mp4",
"media/video/02_hd.mp4"
]
},
{
"key": "path-1-quality",
"value": [
"SD",
"HD"
]
}
]
這是期望的結果:
var arr = [
[
{
"mp4": "media/video/01.mp4",
"quality": "720p"
},
{
"mp4": "media/video/01_hd.mp4",
"quality": "1080p"
},
],
[
{
"mp4": "media/video/02.mp4",
"quality": "SD"
},
{
"mp4": "media/video/02_hd.mp4",
"quality": "HD"
},
],
]
我開始做某事,但還沒有接近:
var key, new_key, value,j=0, z=0, parr = [], obj;
for(var i = 0;i<a.length;i++){
console.log('item:' ,a[i])
key = a[i].key, value = a[i].value
if(key.indexOf('path-'+j.toString()) > -1){
new_key = key.substr(key.lastIndexOf('-')+1)
console.log(key, new_key, value)
for(var z = 0;z<value.length;z++){
parr.push({[new_key]: value[z] })
}
}
}
console.log(parr)
[
{
"mp4": "media/video/01.mp4"
},
{
"mp4": "media/video/01_hd.mp4"
},
{
"quality": "720p"
},
{
"quality": "1080p"
}
]
編輯:
Array 可以保留需要以相同方式分組的不同鍵,例如:
var arr = [
{
"key": "path-0-mp4",
"value": [
"media/video/01.mp4",
"media/video/01_hd.mp4"
]
},
{
"key": "path-0-quality",
"value": [
"720p",
"1080p"
]
},
{
"key": "path-1-mp4",
"value": [
"media/video/02.mp4",
"media/video/02_hd.mp4"
]
},
{
"key": "path-1-quality",
"value": [
"SD",
"HD"
]
},
{
"key": "subtitle-0-label",
"value": [
"English",
"German",
"Spanish"
]
},
{
"key": "subtitle-0-src",
"value": [
"data/subtitles/sintel-en.vtt",
"data/subtitles/sintel-de.vtt",
"data/subtitles/sintel-es.vtt"
]
},
{
"key": "subtitle-1-label",
"value": [
"German",
"Spanish"
]
},
{
"key": "subtitle-1-src",
"value": [
"data/subtitles/tumblr-de.vtt",
"data/subtitles/tumblr-es.vtt"
]
}
]
這是期望的結果(為每個不同的鍵創建新數組):
var arr = [
[
{
"mp4": "media/video/01.mp4",
"quality": "720p"
},
{
"mp4": "media/video/01_hd.mp4",
"quality": "1080p"
},
],
[
{
"mp4": "media/video/02.mp4",
"quality": "SD"
},
{
"mp4": "media/video/02_hd.mp4",
"quality": "HD"
},
],
],
arr2 = [
[
{
"label": "English",
"src": "data/subtitles/sintel-en.vtt",
},
{
"label": "German",
"src": "data/subtitles/sintel-de.vtt"
},
{
"label": "Spanish",
"src": "data/subtitles/sintel-es.vtt"
}
],
[
{
"label": "Spanish",
"src": "data/subtitles/tumblr-es.vtt",
},
{
"label": "German",
"src": "data/subtitles/tumblr-de.vtt"
}
]
]
您可以拆分key
屬性,省略第一個path
並將 rest 作為索引和鍵。 然后創建一個新數組,如果不存在並分配值。
var data = [{ key: "path-0-mp4", value: ["media/video/01.mp4", "media/video/01_hd.mp4"] }, { key: "path-0-quality", value: ["720p", "1080p"] }, { key: "path-1-mp4", value: ["media/video/02.mp4", "media/video/02_hd.mp4"] }, { key: "path-1-quality", value: ["SD", "HD"] }], result = data.reduce((r, { key, value }) => { let [, i, k] = key.split('-'); r[i] = r[i] || []; value.forEach((v, j) => (r[i][j] = r[i][j] || {})[k] = v); return r; }, []); console.log(result);
.as-console-wrapper { max-height: 100%;important: top; 0; }
如果您想按key
的第一部分進行分組,您可以將 object 與該組作為 key 並按上述方式分配 rest。
var data = [{ key: "path-0-mp4", value: ["media/video/01.mp4", "media/video/01_hd.mp4"] }, { key: "path-0-quality", value: ["720p", "1080p"] }, { key: "path-1-mp4", value: ["media/video/02.mp4", "media/video/02_hd.mp4"] }, { key: "path-1-quality", value: ["SD", "HD"] }, { key: "subtitle-0-label", value: ["English", "German", "Spanish"] }, { key: "subtitle-0-src", value: ["data/subtitles/sintel-en.vtt", "data/subtitles/sintel-de.vtt", "data/subtitles/sintel-es.vtt"] }, { key: "subtitle-1-label", value: ["German", "Spanish"] }, { key: "subtitle-1-src", value: ["data/subtitles/tumblr-de.vtt", "data/subtitles/tumblr-es.vtt"] }], result = data.reduce((r, { key, value }) => { let [group, i, k] = key.split('-'); if (;r[group]) r[group] = []; if (.r[group][i]) r[group][i] = [], value;forEach((v; j) => { if (;r[group][i][j]) r[group][i][j] = {}; r[group][i][j][k] = v, }); return r. }; {}); console.log(result);
.as-console-wrapper { max-height: 100%;important: top; 0; }
我是新手和初學者,這是正確的方法嗎?
const a = [{ "key": "path-0-mp4", "value": [ "media/video/01.mp4", "media/video/01_hd.mp4" ] }, { "key": "path-0-quality", "value": [ "720p", "1080p" ] }, { "key": "path-1-mp4", "value": [ "media/video/02.mp4", "media/video/02_hd.mp4" ] }, { "key": "path-1-quality", "value": [ "SD", "HD" ] } ]; var resp = []; for (let i = 0; i < a.length; i++) { var inst = a[i]; var key = inst["key"]; for (let j = 0; j < inst.value.length; j++) { var index = key.split("-")[1]; var keyinst = key.split("-")[2]; if (;resp[index]) { resp[index] = []; } if (.resp[index][j]) { resp[index][j] = {}; } resp[index][j][keyinst] = inst.value[j]; } } console.log(resp);
我發現這更容易閱讀和掌握
如果您使用reduce,您可以保存作業
const arr = [{ "key": "path-0-mp4", "value": [ "media/video/01.mp4", "media/video/01_hd.mp4" ] }, { "key": "path-0-quality", "value": [ "720p", "1080p" ] }, { "key": "path-1-mp4", "value": [ "media/video/02.mp4", "media/video/02_hd.mp4" ] }, { "key": "path-1-quality", "value": [ "SD", "HD" ] } ]; newArr = []; arr.filter(item => item.key.endsWith("mp4")).forEach(item => item.value.forEach((val, i) => newArr.push({ "mp4": val, "quality": arr.find(qItem => qItem.key === item.key.replace("mp4", "quality")).value[i]} ) ) ) console.log(newArr)
這是 Nina 的未混淆版本
var data = [{ key: "path-0-mp4", value: ["media/video/01.mp4", "media/video/01_hd.mp4"] }, { key: "path-0-quality", value: ["720p", "1080p"] }, { key: "path-1-mp4", value: ["media/video/02.mp4", "media/video/02_hd.mp4"] }, { key: "path-1-quality", value: ["SD", "HD"] }], result = data.reduce((resultArray, { key, value }) => { let [, idx, suffix] = key.split('-'); resultArray[idx] = resultArray[idx] || []; value.forEach((val, i) => (resultArray[idx][i] = resultArray[idx][i] || {})[suffix] = val); return resultArray; }, []); console.log(result);
我在這里做的唯一奇怪的事情是使用 object 作為查找表來幫助提高速度復雜性。 如果您有任何問題,請告訴我。
const arr = [{ "key": "path-0-mp4", "value": [ "media/video/01.mp4", "media/video/01_hd.mp4" ] }, { "key": "path-0-quality", "value": [ "720p", "1080p" ] }, { "key": "path-1-mp4", "value": [ "media/video/02.mp4", "media/video/02_hd.mp4" ] }, { "key": "path-1-quality", "value": [ "SD", "HD" ] } ]; const result = arr.reduce((table, item) => { // Getting "path-1" from "path-1-quality" const pathValues = item.key.split('-'); const pathValue = pathValues[0] + '-' + pathValues[1]; // Getting "quality" from "path-1-quality" const key = pathValues[2]; // Get Index from table if already registered paths let tIndex = table.indexLookup[pathValue]; // If there is no registered index register one if (tIndex === undefined) { // reassign index to new location tIndex = table.result.length; // register the index table.indexLookup[pathValue] = tIndex; table.result.push([]); } // Assign values item.value.forEach((value, i) => { const arr = table.result[tIndex] || []; arr[i] = arr[i] || {} arr[i][key] = value; table.result[tIndex] = arr; }) return table }, { indexLookup: {}, result: [] }).result console.log(result)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.