Reputation: 15
I have some json files that have an array structure like this:
{
"00_85_03.g1t": {
"hash": "a05dc5727ee4a3d80202e7ce5c89168d",
"file_name": "00_85_03.g1t",
"version": "0",
"cacheclear_flag": 1,
"bundle_flag": 0,
"directory": "0000",
"firstflag": 0,
"size": 476380
},
"00_85_03_36th.g1t": {
"hash": "5d28224aa1f594c249da69a1976cc5b6",
"file_name": "00_85_03_36th.g1t",
"version": "0",
"cacheclear_flag": 1,
"bundle_flag": 0,
"directory": "0000",
"firstflag": 0,
"size": 831960
},
"00_85_03_all_battle.g1t": {
"hash": "ca654ed300998d97e96480cabcac4035",
"file_name": "00_85_03_all_battle.g1t",
"version": "0",
"cacheclear_flag": 1,
"bundle_flag": 0,
"directory": "0000",
"firstflag": 0,
"size": 700872
}
}
I would like to make a single variable with all "hash" values. I'm using Google App Scripts to insert them into a spreadsheet. So far, I'm able to get a list of the keys but struggling to pull the "hash" value from them. I'm sure it's easier than I'm making it but just learning JS. Here is as far as I was able to make it my result output.
function chunkArray(myArray, chunk_size){
var index = 0;
var arrayLength = myArray.length;
var tempArray = [];
for (index = 0; index < arrayLength; index += chunk_size) {
myChunk = myArray.slice(index, index+chunk_size);
// Do something if you want with the group
tempArray.push(myChunk);
}
return tempArray;
}
function flatten(arrayOfArrays){
return [].concat.apply([], arrayOfArrays);
}
function showPickerJP() {
var rows = [Object.keys(json)];
var toRows = rows[0].length;
var rowsflate = flatten(rows);
var rowstocols = chunkArray(rowsflate, 1);
var results = [];
for(var i=0; i<toRows; i++){
results[i] = (rowstocols[i]);
}
var hashvalues = chunkArray(results,1);
Logger.log(hashvalues);
}
Thank you!
Upvotes: 0
Views: 54
Reputation: 2614
To get all the hash
values from the json data you can use map, ass follows:
const hashList = Object.values(json).map(item => item.hash);
const json = {
"00_85_03.g1t": {
"hash": "a05dc5727ee4a3d80202e7ce5c89168d",
"file_name": "00_85_03.g1t",
"version": "0",
"cacheclear_flag": 1,
"bundle_flag": 0,
"directory": "0000",
"firstflag": 0,
"size": 476380
},
"00_85_03_36th.g1t": {
"hash": "5d28224aa1f594c249da69a1976cc5b6",
"file_name": "00_85_03_36th.g1t",
"version": "0",
"cacheclear_flag": 1,
"bundle_flag": 0,
"directory": "0000",
"firstflag": 0,
"size": 831960
},
"00_85_03_all_battle.g1t": {
"hash": "ca654ed300998d97e96480cabcac4035",
"file_name": "00_85_03_all_battle.g1t",
"version": "0",
"cacheclear_flag": 1,
"bundle_flag": 0,
"directory": "0000",
"firstflag": 0,
"size": 700872
}
};
const hashList = Object.values(json).map(item => item.hash);
console.log({ hashList })
Upvotes: 2