从一个对象获取一个键值对到一个新的对象 jq

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

我得到这个对象

{
  "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"' 时,它只给我值,有没有办法从对象中获取键值到对象中,如示例

{
 "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
    }
});

现在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);

对了,请问你为什么要这样做?

答案在 manual 的对象构造部分。

jq '{"59.79.71.205:26969"}'