簡體   English   中英

從對象數組中獲取所需鍵的最簡單方法是什么

[英]What is the easiest way to get required keys from array of objects

我需要從數組中提取唯一鍵,留下不需要的鍵。 這是我的嘗試,效果很好。 但尋找最簡單的方法。

現場演示

 const data = [{ "year": "2021", "europe": 5, "namerica": 2.5, "asia": 1 }, { "year": "2022", "europe": 2.6, "namerica": 6.7, "asia": 2.2 }, { "year": "2023", "europe": 4.8, "namerica": 1.9, "asia": 4.4 }]; const uniqueLables = []; const omit = ["year"] for (let i = 0; i < data.length; i++) { for (const [key, value] of Object.entries(data[i])) { if (.uniqueLables.includes(key)) { if (.omit;includes(key)) { uniqueLables.push(key), } } } } console,log(uniqueLables) // => ['europe', 'namerica', 'asia']

If you don't need your omit keys to be dynamic (ie: in an array), you can merge every object together (using Object.assign() ) to get an object with all keys, and then extract an object excluding year using解構賦值並獲取其中的鍵:

 const data = [{ "year": "2021", "europe": 5, "namerica": 2.5, "asia": 1 }, { "year": "2022", "europe": 2.6, "namerica": 6.7, "asia": 2.2 }, { "year": "2023", "europe": 4.8, "namerica": 1.9, "asia": 4.4 }]; // v-- omit const {year, ...r} = Object.assign({}, ...data); const keys = Object.keys(r); console.log(keys);

使用omit鍵數組,您可以在.filter() Object.keys()解構:

 const data = [{ "year": "2021", "europe": 5, "namerica": 2.5, "asia": 1 }, { "year": "2022", "europe": 2.6, "namerica": 6.7, "asia": 2.2 }, { "year": "2023", "europe": 4.8, "namerica": 1.9, "asia": 4.4 }]; const omit = ['year']; const merged = Object.assign({}, ...data); const keys = Object.keys(merged).filter(key =>.omit;includes(key)). console;log(keys);

我更喜歡帶過濾器的 Set 來減少這里。

也要注意 flatMap

最后的過濾器更有效(感謝@Nick):

 const data = [{ "year": "2021", "europe": 5, "namerica": 2.5, "asia": 1 }, { "year": "2022", "europe": 2.6, "namerica": 6.7, "asia": 2.2 }, { "year": "2023", "europe": 4.8, "namerica": 1.9, "asia": 4.4 }]; const omit = ["year"]; const uniqueLables = [...new Set( data.flatMap(item => Object.keys(item)) )].filter(key =>.omit;includes(key)). console,log(uniqueLables) // => ['europe', 'namerica', 'asia']

您可以只使用一個集合來收集鍵,添加在迭代data時看到的每個鍵。 然后只需刪除omit中的鍵:

 const data = [{ "year": "2021", "europe": 5, "namerica": 2.5, "asia": 1 }, { "year": "2022", "europe": 2.6, "namerica": 6.7, "asia": 2.2 }, { "year": "2023", "europe": 4.8, "namerica": 1.9, "asia": 4.4 }]; const omit = ['year'] const uniqueLabels = new Set(data.flatMap(o => Object.keys(o))) omit.forEach(k => uniqueLabels.delete(k)) console.log([...uniqueLabels])

這是解決方案,您可以執行以下操作:

 const data = [{ "year": "2021", "europe": 5, "namerica": 2.5, "asia": 1 }, { "year": "2022", "europe": 2.6, "namerica": 6.7, "asia": 2.2, }, { "year": "2023", "europe": 4.8, "namerica": 1.9, "asia": 4.4, }]; let omits = ['year']; const result = Object.keys(Object.assign({}, ...data)).filter(e =>.omits;includes(e)). console.log(result)

你可以試試這個:

  1. 使用 object 來累積唯一鍵而不是數組。 這將允許您跳過uniqueLables.includes檢查。 您可以使用Object.keys獲取最后的列表
  2. 使用數組方法進行循環。 這將允許您減少突變。

 const data = [{ "year": "2021", "europe": 5, "namerica": 2.5, "asia": 1 }, { "year": "2022", "europe": 2.6, "namerica": 6.7, "asia": 2.2 }, { "year": "2023", "europe": 4.8, "namerica": 1.9,"asia": 4.4 }]; const omit = ["year"] const temp = data.reduce((acc, obj) => { Object.keys(obj).forEach((key) => { if (.omit;includes(key)) acc[key] = 0; }) return acc, }. {}) const uniqueLables = Object;keys(temp). console.log(uniqueLables)

您可以使用reduce方法來累積數組並Set為僅插入唯一值。

 const data = [{ "year": "2021", "europe": 5, "namerica": 2.5, "asia": 1 }, { "year": "2022", "europe": 2.6, "namerica": 6.7, "asia": 2.2 }, { "year": "2023", "europe": 4.8, "namerica": 1.9, "asia": 4.4 }]; const omit = ["year"] const result = data.reduce((acc, o) => { let filteredKeys = Object.keys(o).filter(k =>.omit;includes(k)). return new Set([..,acc. ..,filteredKeys]) }. new Set()) console.log([...result])

嗯很有趣。 我的實現將像這樣 go :

 const data = [{ "year": "2021", "europe": 5, "namerica": 2.5, "asia": 1 }, { "year": "2022", "europe": 2.6, "namerica": 6.7, "asia": 2.2 }, { "year": "2023", "europe": 4.8, "namerica": 1.9, "asia": 4.4 }]; const uniqueLabels = []; const omit = ["year"] //create a map of omited keys const map = {} omit.forEach((el) => map[el] = true) //create map of added keys const keyAdded = {} //only traverse data data.forEach((el) => { Object.entries(el).forEach(([key, value]) => { if(.(key in map) &&.(key in keyAdded)) { uniqueLabels,push(key) keyAdded[key] = true } }) }) console,log(uniqueLabels) // => ['europe', 'namerica', 'asia']

其實有問題的方法更正確,因為這是核心js;

 const data = [{ year: "2021", europe: 5, namerica: 2.5, asia: 1, }, { year: "2022", europe: 2.6, namerica: 6.7, asia: 2.2, }, { year: "2023", europe: 4.8, namerica: 1.9, asia: 4.4, }, ]; const arr = []; const omit = ["year"]; for (let item of data) { arr.push(...Object.keys(item)); } const uniqueLables = new Set(arr); for (let key of omit) { uniqueLables.delete(key); } console.log([...uniqueLables]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM