簡體   English   中英

從 object 到新的 object 獲取鍵值對 jq

[英]get a key-value pair from an object to a new object jq

我得到這個 object

{
  "138.68.226.120:26969": 1,
  "178.128.50.37:26969": 1,
  "207.180.218.133:26969": 1,
  "66.42.67.157:26969": 1,
  "140.82.14.193:26969": 1,
  "51.15.39.62:26969": 1,
  "144.217.91.232:26969": 1,
  "144.217.81.95:26969": 1,
  "68.183.105.143:26969": 1,
  "192.99.246.177:26969": 1,
  "167.99.98.151:26969": 1,
  "59.79.71.205:26969": 1
}

當我使用 jq '."59.79.71.205:26969"' 它只給我值時,有沒有辦法將鍵值從 object 獲取到 object 中,例如示例

{
 "59.79.71.205:26969": 1
}

答案在手冊的 Object 構造部分。

jq '{"59.79.71.205:26969"}'
const splittedObject = Object.keys( // get all the keys
    yourObject
).map((key) => { // then for each key, turn the key into an object with the key-value pair
    return {
        [key]: yourObject[key] // assign the value to the key and voila
    }
});

現在splittedObject是一個帶有一個鍵的對象的數組,最好用這個片段來演示:

 const yourObject = { "138.68.226.120:26969": 1, "178.128.50.37:26969": 1, "207.180.218.133:26969": 1, "66.42.67.157:26969": 1, "140.82.14.193:26969": 1, "51.15.39.62:26969": 1, "144.217.91.232:26969": 1, "144.217.81.95:26969": 1, "68.183.105.143:26969": 1, "192.99.246.177:26969": 1, "167.99.98.151:26969": 1, "59.79.71.205:26969": 1 }; const splittedObject = Object.keys( // get all the keys yourObject ).map((key) => { // then for each key, turn the key into an object with the key-value pair return { [key]: yourObject[key] // assign the value to the key and voila } }); console.log(splittedObject);

順便問一下,我能問你為什么需要這樣做嗎?

暫無
暫無

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

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