获取一个从一个对象到一个新对象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
}

答案在手册的对象构造部分。

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

顺便问一下,我能问你为什么要这么做吗?

最新更新