I have to iterate through this JSON:
{
"data": 321563,
"group": [
{
"added": 42421,
"normal": {
"x": 39,
"y": "0.1300",
"b": "0.4326",
"c": "0.0552",
"f": 166833
},
"j": "240313",
"b": "0.2251",
"a": "dda",
"b": "0.101",
"a": 922,
"f": {
"c": 39,
"d": "0.263",
"a": "2.8955",
"h": "0.3211",
"d": 274
},
"a": false,
"k": 5,
"w": "0.072",
"d": "0.045",
"e": 3
},
I only want the j and k stored like a key value pair eg "j":k
I need to loop all of it, and store it to a file.
You can use a map to get a new array of items, this will not affect the old array.
const data = { "game_count": 8750, "sets": [ { "appid": "221540", "true_count": 9, "bgs_avg": "0.10", // Other data here }, { "appid": "123456", "true_count": 9, "bgs_avg": "0.20", // Other data here } ] } // Use "data.sets = data.sets.map(...)" to replace the data // The following will only assign to a new variable const newArray = data.sets.map(itm => { return {appid: itm.appid, true_count: itm.true_count} }) console.log(newArray)
We can also take the data and assign it directly back to the original overwriting it just by using data.sets = data.sets.map(...)
as seen here:
const data = { "game_count": 8750, "sets": [ { "appid": "221540", "true_count": 9, "bgs_avg": "0.10", // Other data here }, { "appid": "123456", "true_count": 9, "bgs_avg": "0.20", // Other data here } ] } data.sets = data.sets.map(itm => { return {appid: itm.appid, true_count: itm.true_count} }) console.log(data)
In simple javascript this should work -
let newObj = {}
for(let i=0; i<obj.group.length; i++){
newObj[obj.group[i].j] = obj.group[i].k
}
Where 'obj' is your object
newObj will be you new Object which will contain all the key value pair
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.